如何使用vb.net使用mailkit发送电子邮件?

发布于 2025-02-03 20:10:09 字数 954 浏览 3 评论 0原文

我正在尝试使用MailKit和vb.net代码发送电子邮件。

电子邮件发送了电子邮件,除了电子邮件的正文始终为空白。

这是我的代码。我怀疑这一行的问题是:

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

我将代码从c#转换为vb.net,不知道这是否是vb.net中的正确语法来设置文本属性?

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泡沫很甜 2025-02-10 20:10:09

对于身体的设置,我将使用健美运动员:

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文