本文实验用QQ邮箱发不了,网易163能发文字不能发img标签的内容
在新浪邮箱实验成功:
public bool sendEmailto(List<string> shoujianren,string biaoti,string neirong)
{
//简单邮件传输协议类
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.sina.com";//邮件服务器
client.Port = 25;//smtp主机上的端口号,默认是25.
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//邮件发送方式:通过网络发送到SMTP服务器
client.Credentials = new System.Net.NetworkCredential("caggsimida@sina.com", "xxxxxx");//凭证,发件人登录邮箱的用户名和密码
//电子邮件信息类
//System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("caggsimida@sina.com", "GG");//发件人Email,在邮箱是这样显示的,[发件人:小明<panthervic@163.com>;]
//System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress("741056536@qq.com");//收件人Email,在邮箱是这样显示的, [收件人:小红<43327681@163.com>;]
System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();//创建一个电子邮件类
mailMessage.From = new MailAddress("caggsimida@sina.com", "GG");
foreach (string item in shoujianren)
{
mailMessage.To.Add(item);
}
mailMessage.Subject = "邮件的主题";
if(neirong.Contains("img"))
{
var str_html = RegexHtml(neirong);
//处理所有img标签
var htmlBody = GetHtmlImageUrlList(neirong, str_html);
mailMessage.AlternateViews.Add(htmlBody);
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mailMessage.Body = str_html;//可为html格式文本
}
else
{
mailMessage.Body = neirong;//可为html格式文本
}
//mailMessage.Body = "邮件的内容";//可为html格式文本
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码
mailMessage.BodyEncoding = Encoding.UTF8; //邮件内容编码
mailMessage.IsBodyHtml = true;//邮件内容是否为html格式
mailMessage.Priority = System.Net.Mail.MailPriority.High;//邮件的优先级,有三个值:高(在邮件主题前有一个红色感叹号,表示紧急),低(在邮件主题前有一个蓝色向下箭头,表示缓慢),正常(无显示).
try
{
client.Send(mailMessage);//发送邮件
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 替换img标签的sec属性
/// </summary>
/// <param name="htmltext">html</param>
/// <returns></returns>
public string RegexHtml(string htmltext)
{
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r
;>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
MatchCollection matches = regImg.Matches(htmltext);
foreach (Match match in matches)
{
var src = match.Groups["imgUrl"].Value;
if (!src.Contains("http://spimg"))
{
var fileName_index = src.LastIndexOf('/');
var cid = src.Substring(fileName_index + 1, src.Length - fileName_index - 5);
htmltext = Regex.Replace(htmltext, src, "cid:" + cid, RegexOptions.None);
}
}
return htmltext;
}
public AlternateView GetHtmlImageUrlList(string sHtmlText, string str_html)
{
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(str_html, Encoding.UTF8, "text/html");
Regex imgRE, linkRE, hrefRE;
string imgMatchExpression = "(?:img[^>]+src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))|url\\(['\"](?<1>[^'\"]*)['\"]\\))";
imgRE = new Regex(imgMatchExpression, RegexOptions.IgnoreCase | RegexOptions.Compiled);
string linkMatchExpression = "<\\s*link[^>]+href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))[^>]*>";
linkRE = new Regex(linkMatchExpression, RegexOptions.IgnoreCase | RegexOptions.Compiled);
//this one's for fixup of relative urls in anchors
string refMatchExpression = "href\\s*=\\s*(?:['\"](?<1>[^\"]*)['\"]|(?<1>\\S+))";
hrefRE = new Regex(refMatchExpression, RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches = imgRE.Matches(sHtmlText);
foreach (Match match in matches)
{
var src = match.Groups[1].Value;
if (!src.Contains("http://www"))//绝对路径图片不处理
{
var fileName_index = src.LastIndexOf('/');
var cid = src.Substring(fileName_index + 1, src.Length - fileName_index -5);
LinkedResource lrImage = new LinkedResource(Server.MapPath(match.Groups[1].Value), "image/gif");
lrImage.ContentId = cid;
htmlBody.LinkedResources.Add(lrImage);
}
}
return htmlBody;
}