Search code examples
vb.netmailmessageopenpop

Converting OpenPop.NET HTML message to System.Net.Mail.MailMessage becomes text/plain


I have a raw email (MIME text) that I parse with OpenPop.Net and then convert to a System.Net.Mail.MailMessage

Dim message As MailMessage
Dim openPopMsg As New OpenPop.Mime.Message(System.Text.Encoding.ASCII.GetBytes(mimeText))
message = openPopMsg.ToMailMessage()

Dim Client As SmtpClient
Client = New SmtpClient(account.Server, account.Port)
Client.Credentials = New System.Net.NetworkCredential(account.User, account.Password)
Client.EnableSsl = account.UseSSL
Client.DeliveryMethod = SmtpDeliveryMethod.Network
Client.Send(message)
Client.Dispose()

The email is sent, attachments are delivered OK, etc. The only problem is the content of the email should be text/html but I receive the HTML as text/plain.

message.IsBodyHtml is set to True

How can I change the content type of a particular section or better yet do the conversion correctly?

The original mime text looks like this (this is just a part of the multipart email):

----boundary_27_d254c44c-5b54-4df3-ac69-ef73c8b0158b
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; charset=3D=
utf-8"><html><header><style>p{margin-top: 0;margin-bottom: 0;line=
-height: 100%}ul,li{padding: 0px;margin: 0px;margin-top: 0;margin=
-bottom: 0}</style></header><body><p style=3D"text-align:left;"><=
span style=3D"font-bold:false;color:rgb(0,0,0);"><font face=3D"Ar=
ial" style=3D"font-size:12pt;" >Mira este es el subjetct: This is=
 an email test.</font></span></p><BR><BR><BR></body></html>

The email sent has the following mime text:

----boundary_3_d722cdce-a270-41b5-aa4e-33aa9131512a
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; charset=3D=
utf-8"><html><header><style>p{margin-top: 0;margin-bottom: 0;line=
-height: 100%}ul,li{padding: 0px;margin: 0px;margin-top: 0;margin=
-bottom: 0}</style></header><body><p style=3D"text-align:left;"><=
span style=3D"font-bold:false;color:rgb(0,0,0);"><font face=3D"Ar=
ial" style=3D"font-size:12pt;" >Mira este es el subjetct: This is=
 an email test.</font></span></p><BR><BR><BR></body></html>

Solution

  • After converting the message I did the following:

    message.AlternateViews(0).ContentType.MediaType = "text/html"
    

    This works fine for this particular case but...

    • Will it always be message.AlternateViews(0)?
    • I would prefer ToMailMessage() would do the conversion directly