简体   繁体   中英

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)

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM