ASP.NET通过SMTP和ESMTP实现邮件发送带图片内容的邮件(2)
Code [http://www.xueit.com] private Hashtable EmbedList = new Hashtable(); // widened scope for MatchEvaluator private string FixupReferences( string rawPayload, ref StringBuilder extras, string bound

private Hashtable EmbedList = new Hashtable(); //widened scope for MatchEvaluator private string FixupReferences(string rawPayload, ref StringBuilder extras, string boundaryString) { //Build a symbol table to avoid redundant embedding. Regex imgRE, linkRE, hrefRE; MatchCollection imgMatches; 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); imgMatches = imgRE.Matches(rawPayload); //translation to a Hashtable weeds out redundant references foreach (Match m in imgMatches) { if (!EmbedList.ContainsKey(m.Groups[1].Value)) { EmbedList.Add(m.Groups[1].Value, Guid.NewGuid()); } } //Prepare embedded data extras.Length = 0; string contentType; ArrayList embeddees = new ArrayList(EmbedList.Keys); foreach (string embeddee in embeddees) { contentType = embeddee.Substring(embeddee.LastIndexOf(".") 1).ToLower(); extras.AppendFormat(boundaryString); if (contentType.Equals("jpg")) contentType = "jpeg"; switch (contentType) { case "jpeg": case "gif": case "png": case "bmp": extras.AppendFormat("Content-Type: image/{0}; charset=\"iso-8859-1\"\r\n", contentType); extras.Append("Content-Transfer-Encoding: base64\r\n"); extras.Append("Content-Disposition: inline\r\n"); extras.AppendFormat("Content-ID: <{0}>\r\n\r\n", EmbedList[embeddee]); extras.Append(GetDataAsBase64(embeddee)); extras.Append("\r\n"); break; } } //Fixups for references to items now embedded rawPayload = imgRE.Replace(rawPayload, new MatchEvaluator(_fixup)); return rawPayload; } private string _fixup(Match m) { string replaceThis = m.Groups[1].Value; string withThis = string.Format("cid:{0}", EmbedList[replaceThis]); return m.Value.Replace(replaceThis, withThis); }
然后你在和服务器交互的时候,就可以通过这个函数,获取解析后的HTML文件内容和图片流,对其进行操作即可。

//判断信件格式是否html if (Html) { SendBufferstr = "Content-Type: text/html;" enter; } else { SendBufferstr = "Content-Type: text/plain;" enter; } //编码信息 if (Charset == "") { SendBufferstr = " charset=\"iso-8859-1\"" enter; } else { SendBufferstr = " charset=\"" Charset.ToLower() "\"" enter; } SendBufferstr = "Content-Transfer-Encoding: base64" enter; StringBuilder extras = new StringBuilder(); string extrasBoundary = "--" boundary enter; string newBodyHtml = FixupReferences(this.Body, ref extras, extrasBoundary); SendBufferstr = enter enter; SendBufferstr = B64StrLine(Base64Encode(newBodyHtml)) enter; SendBufferstr = enter "--" boundary1 "--" enter enter; SendBufferstr = extras.ToString();
转载请注明出处!
撰写人:伍华聪 http://www.iqidi.com
精彩图集
精彩文章
热门标签
INSERT_INTO_
获取主机名称
js改变背景图片
值不同的列
AVL树
exls
分割大文件
Httperr.Log
广播
通信模
Banner
类型
多态
原因分析
文件夹大小
object转数组
-bash
SetLocale
用户管理
阿里云主机
证书加密
console.log
三元表达式
字符串位置
组件
sae
velocity
ARP攻击
无法复制文件
bat
常常出现的
淡入
perl
jquery解析json
Slic
马赛克
增加账户
生命周期
$符
主键
youtube视频
上传视频
通过Socket
无用
httpclient
temp
屏幕快照
所有颜色值
python线程
Linux集群
采集器
系统分享
高效随机
yii
类变量
python函数调用
集合运算符
c语言http请求
配置心得
变量的值
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229