Sending html emails from C#
I'm trying to send an ht开发者_运维百科ml email to a gmail account, but for some reason, Google is stripping away the html from my email. The Html is preserved when I send to other accounts (non-gmail accounts) so I know that my html is correct.
Here's how I'm going about it:
- I have an aspx page, that I use as an email template.
- I grab the html from the aspx page from within a web service (done in C#)
- Dynamically fill in the non-static content through c# code within the web service.
- Send that as the email body.
Does anyone happen to know why gmail is removing the html? Thanks in advance.
You need to be sure to set the IsBodyHtml property to true on your MailMessage:
var message = new MailMessage();
message.IsBodyHtml = true;
// Fill and send message here
Check out the MSDN reference for more info:
System.Net.Mail.MailMessage Members
The Html is preserved when I send to other accounts (non-gmail accounts) so I know that my html is correct.
It's not a programming issue. If it were, as you've observed, this would happen to all clients.
The issue is this: Most modern email clients allow users to choose to disallow html messages, or always view them as plain text. That could be what is happening here. You have to code to expect this, because you can't control user's preferences. If they have this enabled, and you send it as html only, it will look ugly to them.
However, for a solution to your issue, you should always be sending your mail as a Multi-Part Mime message to allow all clients to get a nice readable version.
精彩评论