本文實驗用QQ郵箱發不了,網易163能發文字不能發img標籤的內容html
在新浪郵箱實驗成功:服務器
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;
}this
}編碼
/// <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;
}url
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);htm
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;
} 圖片