C# 邮件发送系统,内容夹带图片及附件
应用领域:可将生产报表形成文件,通过邮箱发送至领导,方遍领导查看,body中可以带图片。
public bool SendMail(string mailFromAddress, string name, string[] addr_tos, string subject, string body, MailPriority priority, bool ishtml)
{
if (SoftMailSendFailedCount > 10) { SoftMailSendFailedCount++; return false; }
using (MailMessage Message=new MailMessage())
{
Message.From = new MailAddress(mailFromAddress, name, Encoding.UTF8);
foreach(var item in addr_tos)
{
Message.To.Add(item);
}
Message.Subject = subject;
Message.IsBodyHtml = ishtml;
Message.SubjectEncoding = Encoding.UTF8;
Message.BodyEncoding = Encoding.UTF8;
Message.Priority = priority;
Message.Body = "邮件服务系统自动发送,请勿回复!"+Environment.NewLine;
Message.Body += body;
try
{
//邮件附件
if (ListAttachments.Count > 0)
{
foreach (Attachment item in ListAttachments)
{
Message.Attachments.Add(item);
}
}
if (!string.IsNullOrEmpty(ShowBodyImageID))
{
Message.Body += "<div style=\"position:relative; width: 300px; height: 500px;\">";
Message.Body += "<img src=\"cid:" + ShowBodyImageID + "\" width=\"300\" height=\"300\" alt=\"\"/>"; //这个写上附件就会变成文章中的图片
Message.Body += "<span style=\"position: absolute; top: 10; left: 10;\">" + "创作不易,酌情打赏(1~5元)" + "</span>";
Message.Body += "</div>";
}
SmtpClient.Send(Message);
//清空数据
SoftMailSendFailedCount = 0;
return true;
}
catch (Exception ex)
{
SoftMailSendFailedCount++;
Console.WriteLine(ex.ToString());
return false;
}
}
}