HTML正文电子邮件发送
问题描述:
大家好,
我遇到了问题。
当我运行代码在我的机器上发送电子邮件时,它运行良好。它以HTML格式发送邮件,但是当我把exe放在服务器上时它并没有以HTML格式发送。
在邮件中我得到了所有的HTML标签。
我做的代码是:
Hello everyone,
I am facing a problem.
When I run the code to send email on my machine it works well. It sends mail in HTML Format, but when I put the exe on server it didn''t sends in HTML format.
In the mail I get all the HTML tags.
I did the code is:
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
try
{
message.From = new MailAddress(fromemail, fromname);
message.Subject = subject;
//message.Body = body;
//message.IsBodyHtml = true;
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(body, null, "text/html"));
message.To.Add(new MailAddress(to));
if (cc != "")
{
foreach (string str in cc.Split(new char[] { ',' }))
{
message.CC.Add(new MailAddress(str.Trim()));
}
}
if (bcc != "")
{
foreach (string str2 in bcc.Split(new char[] { ',' }))
{
message.Bcc.Add(new MailAddress(str2.Trim()));
}
}
client.Host = server;
client.EnableSsl = true;
NetworkCredential credential = new NetworkCredential
{
UserName = new MailAddress(fromemail, fromname).Address,
Password = frompwd
};
client.UseDefaultCredentials = true;
client.Credentials = credential;
client.Port = 0x24b;
client.Send(message);
答
这可能是因为你已经注释了这一行:
That could be because you have commented out the line:
//message.IsBodyHtml = true;
由于默认为false,因此HTML元素在收到时将被忽略。尝试取消注释,然后重新编译调试版和发行版。
Since this defaults to false, the HTML elements will be ignored when they are received. Try uncommenting it, and re-compile both debug and release versions.
Griff,
我试过这个。
//message.IsBodyHtml = true; -------- 2
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(body,null," text / html")); ------------ 3
我逐行使用第2行和第3行来获取html格式但仍然没有得到结果
Griff,
I have tried with this .
//message.IsBodyHtml = true;--------2
message.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(body, null, "text/html"));------------3
I used line 2 & 3 one by one to get html format but still not getting the result