MatthewNYC
MatthewNYC

Reputation: 65

How To Send Email Using MailKit With VB.net?

I'm trying to send an email using MailKit and VB.net code.

The email is sending except the body of the email message is always blank.

Here is my code. I suspect the problem is with this line:

message.Body = New TextPart(TextFormat.Plain) With {
      .Text = "Test Message"
    }

I converted the code from C# to VB.net and don't know if this is the correct syntax in VB.net to set the Text property?

    'Create email message
    Dim email As MimeMessage = New MimeMessage()
    email.From.Add(MailboxAddress.Parse(MyFromEmail))
    email.To.Add(MailboxAddress.Parse(MyToEmail))
    email.Subject = "Test Email Subject"

    Dim message As MimeMessage = New MimeMessage()
    message.Body = New TextPart(TextFormat.Plain) With {
      .Text = "Test Message"
    }

    'Send email
    Dim smtp As MailKit.Net.Smtp.SmtpClient = New MailKit.Net.Smtp.SmtpClient()

    smtp.Connect(MySmtpServer, MySmtpServerPort, MailKit.Security.SecureSocketOptions.SslOnConnect)
    smtp.Authenticate(MySendingEmail, MySendingPassword)
    smtp.Send(email)
    smtp.Disconnect(True)

Upvotes: 1

Views: 3878

Answers (1)

CoolCoderSuper
CoolCoderSuper

Reputation: 118

For settings the body I would use the BodyBuilder class:

 Dim objBodyBuilder As New BodyBuilder
 objBodyBuilder.HtmlBody = "<b>HTML message</b>"
 objBodyBuilder.TextBody = "Plain text"
 objBodyBuilder.Attachments.Add("an attachment")
 message.Body = objBodyBuilder.ToMessageBody()

Upvotes: 2

Related Questions