Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a mail as both HTML and Plain Text in .net

Tags:

c#

.net

I'm sending mail from my C# Application, using the SmtpClient. Works great, but I have to decide if I want to send the mail as Plain Text or HTML. I wonder, is there a way to send both? I think that's called multipart.

I googled a bit, but most examples essentially did not use SmtpClient but composed the whole SMTP-Body themselves, which is a bit "scary", so I wonder if something is built in the .net Framework 3.0?

If not, is there any really well used/robust Third Party Library for sending e-Mails?

like image 475
Michael Stum Avatar asked Sep 04 '08 21:09

Michael Stum


People also ask

How do you send plain text and HTML email simultaneously?

GroupMail allows senders to send both an HTML and plain-text version of their email at the same time. This is done using a format called Multi-Part MIME. When a multi-part MIME email (read: an email that includes both an HTML and plain-text part) is created in GroupMail, they will be sent out together as one message.

Can you send an email to a .net address?

NET and . NET Core come with built-in support for sending emails through the System. Net.Mail namespace. While it might seem like the easy choice, you will need an SMTP server for this to work.

Can C# send an email?

The standard approach to send an email using C# is SMTP (Simple Mail Transfer Protocol). It is a network protocol used to send emails over the internet. Additionally, it allows you to relayemails across multiple networks.

How can you send an email message from an asp net web page?

First: Edit Your AppStart Page SmtpPort: The port the server will use to send SMTP transactions (emails). EnableSsl: True, if the server should use SSL (Secure Socket Layer) encryption. UserName: The name of the SMTP email account used to send the email. Password: The password of the SMTP email account.


1 Answers

The MSDN Documentation seems to miss one thing though, I had to set the content type manually, but otherwise, it works like a charm :-)

MailMessage msg = new MailMessage(username, nu.email, subject, body); msg.BodyEncoding = Encoding.UTF8; msg.SubjectEncoding = Encoding.UTF8;  AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlContent); htmlView.ContentType = new System.Net.Mime.ContentType("text/html"); msg.AlternateViews.Add(htmlView); 
like image 187
Michael Stum Avatar answered Sep 21 '22 04:09

Michael Stum