VB.Net-以html和纯文本形式发送邮件
我的VB应用程序中有一个邮件功能“Sendmail”,所以VB.Net-以html和纯文本形式发送邮件,vb.net,visual-studio,email,smtp,Vb.net,Visual Studio,Email,Smtp,我的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()
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邮件发送出去。。。太棒了
我需要的是,在那封邮件中包括一个纯文本版本,以及发出的邮件
有什么简单的方法可以做到这一点吗?使用alterativeViews
'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)
显然,您需要将纯文本和HTML文本作为参数传递给例程。我是否保留'mail.IsBodyHtml=True'和'mail.Body=mailbody'位?IsBodyHtml应该设置默认值。如果我没记错的话,您不需要.Body