我已经编写了以下代码来发送邮件,其中邮件正文是一个html页面。 当我运行此代码但没有收到任何邮件时,我没有收到任何错误。我尝试将body作为简单的消息字符串,然后我收到了电子邮件,但没有像html页面那样作为消息体。 出了什么问题?请帮忙。
protected void btnSend_Click(object sender, EventArgs e)
{
SendHTMLMail();
}
public void SendHTMLMail()
{
//var path = Server.MapPath("~/test/HTMLPage.htm");
StreamReader reader = new StreamReader(Server.MapPath("~/expo_crm/test/HTMLPage.htm"));
string readFile = reader.ReadToEnd();
string myString = "";
myString = readFile;
SmtpClient smtp = new SmtpClient
{
Host = "mail.abc.com", // smtp server address here…
Port = 25,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential("abc@xyz.com", "xxxx"),
Timeout = 30000,
};
MailMessage message = new MailMessage("abc@xyz.com", "akshata@gmail.com", " html ", myString);
message.IsBodyHtml = true;
smtp.Send(message);
}
答案 0 :(得分:1)
大多数情况下,邮件服务器检查html内容,并根据邮件中的链接,图像等html标记标记为垃圾邮件。确保您的HTML标签数量较少,可能没有外部链接,图像并再次尝试发送邮件。
答案 1 :(得分:1)
这是工作代码,如果这有帮助
public void SendEmail(ListDictionary email)
{
try
{
var msg = new MailMessage {From = new MailAddress(_emailUsername, _emailFrom), BodyEncoding = Encoding.UTF8, Subject = Convert.ToString(email["SUBJECT"]), Priority = MailPriority.Normal};
//
var emailTo = (List<string>) email["TO"];
var emailCc = (List<string>) email["CC"];
var emailBcc = (List<string>) email["BCC"];
foreach (var to in emailTo.Where(to => to.Length > 1))
msg.To.Add(to);
foreach (var cc in emailCc.Where(cc => cc.Length > 1))
msg.CC.Add(cc);
foreach (var bcc in emailBcc.Where(bcc => bcc.Length > 1))
msg.Bcc.Add(bcc);
//
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(Convert.ToString(email["BODY_TEXT"]), Encoding.UTF8, "text/plain"));
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(Convert.ToString(email["BODY_HTML"]), Encoding.UTF8, "text/html"));
//
new SmtpClient
{
Credentials = new NetworkCredential(_emailUsername, _emailPassword),
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true,
Host = "smtp.gmail.com"
}.Send(msg);
}
catch (Exception e)
{
L.Get().Fatal("Failed", e);
}
}
答案 2 :(得分:1)
在将您的身体设置为Html之前,您是否正在设置身体?
值得尝试使用完整对象构造函数(new MailMessage(){IsBodyHtml = true,Body = myString})或一次设置一个属性以确保...