John
John

Reputation: 785

VB.Net - Sending out mail as html and plaintext

I have a mail function 'Sendmail' in my VB app, as so...

 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

This works great, passing the recipient, subject and body to it sends HTML mail out... fantastic.

What I need to is include with that email a plain text version with the mail that goes out.

Is there a simple way I can achieve this?

Upvotes: 0

Views: 979

Answers (1)

Trevor_G
Trevor_G

Reputation: 1331

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

Obviously you need to pass both the PLain_Text and HTML_Text as parameters to the routine.

Upvotes: 1

Related Questions