How do i send email with attachment in vb.net using Microsoft Graph?

windforce

New member
Joined
Dec 29, 2022
Messages
2
Programming Experience
10+
Hi
I need to send emails with attachments ( generally 2 or 3 pdf files per email ) using Microsoft Graph.
I have the following code working, but don't know how to add the attachment part.

send email microsoft graph:
Dim credentials = New ClientSecretCredential(tenantID, clientID, clientSecret, New TokenCredentialOptions With {.AuthorityHost = AzureAuthorityHosts.AzurePublicCloud})
Dim graphServiceClient As New GraphServiceClient(credentials)

Dim mailMessage = New Message With {
                    .Subject = subject,
                    .Body = New ItemBody With {
                        .ContentType = BodyType.Html,
                        .Content = message
                    },
                    .ToRecipients = toRecipients,
                    .CcRecipients = ccRecipients
                }

        ' Send mail as the given user.
        graphServiceClient.Users(fromAddress).SendMail(mailMessage, True).Request().PostAsync().Wait()

Thanks
Jaime
 
Thanks John. Saw it, seems too complicated. Found the correct code though, so i'm writing it here in case someone needs it. Code here is for pdf attachment:
VB.NET:
Dim pdfAttachment as New Attachment
pdfAttachment.ContentType = "application/pdf"
pdfAttachment.Name = "attachment.pdf"
dim pdfContents as Byte() = File.ReadAllBytes("c:\path\to\attachment.pdf")
pdfAttachment.ContentBytes = pdfContents
mailMessage.Attachments = New List(Of Attachment) From {
                    pdfAttachment
}
 

Latest posts

Back
Top