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
Code [http://www.xueit.com]
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文件内容和图片流,对其进行操作即可。
Code [http://www.xueit.com]
//判断信件格式是否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
精彩图集
精彩文章
热门标签
Oracle数据库
ctype函数
mysql小技巧
MIME
II
超链接
js操作table
PHP框架
hashing
des加密
vspace
SessionFacto
print_r
抓取豆瓣
创建
o
squid
jsp操作MySQL
加减
则是
COLOR
Shell.Applic
jira
起止时间
当前url
c00ce56e
消息窗口
nagios飞信监控
in_array
RAC
http
提交数据
内存管理
计算机
手电筒
pytho
过滤排序
oracle下载
大类
数组查询
传输
APPPOOL
漫画
技能
python代码检查
硬件列表
访问远程文
基础 纯属疑问
之双
php程序员
验证
浅谈
GPS功能
多个定时器
我所
改变按钮
数据存储
数据库主键
除法运算
文件夹
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

