【问题标题】:VB.Net - Sending out mail as html and plaintextVB.Net - 以 html 和纯文本形式发送邮件
【发布时间】:2017-02-15 18:16:23
【问题描述】:

我的 VB 应用程序中有一个邮件功能“Sendmail”,因此...

 Public Function Sendmail(ByVal mailrecipient As String, ByVal mailsubject As String, ByVal mailbody As String)
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New _
        Net.NetworkCredential(internal_mail_server_username, internal_mail_server_password)
            SmtpServer.Port = 25
            SmtpServer.Host = internal_mail_server
            mail = New MailMessage()
            mail.From = New MailAddress(internal_email_sender)
            mail.To.Add(mailrecipient)
            mail.Subject = mailsubject
            mail.IsBodyHtml = True
            mail.Body = mailbody
            SmtpServer.Send(mail)
            MessageBox.Show("Mail successfully sent to " & mailrecipient)
            Return "Success"

        Catch ex As Exception

        End Try
    End If
End Function

这很好用,将收件人、主题和正文传递给它会发送 HTML 邮件...太棒了。

我需要在该电子邮件中包含一个纯文本版本,以及发出的邮件。

有没有一种简单的方法可以实现这一点?

【问题讨论】:

    标签: vb.net visual-studio email smtp


    【解决方案1】:

    使用替代视图

    'first create the Plain Text part
    Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(Plain_Text)
    'then  create the Html part
    Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(HTML_Text)
    mail.AlternateViews.Add(plainView)
    mail.AlternateViews.Add(htmlView)
    

    显然您需要将 PLAin_Text 和 HTML_Text 作为参数传递给例程。

    【讨论】:

    • 我是否保留 'mail.IsBodyHtml = True' 和 'mail.Body = mailbody' 位?
    • IsBOdyHTML 应该设置默认值,如果我没记错的话,你不需要 .body
    猜你喜欢
    • 2010-09-07
    • 2021-04-12
    • 1970-01-01
    • 2012-04-29
    • 2016-03-07
    • 2017-06-28
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    相关资源
    最近更新 更多