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
精彩图集
精彩文章
热门标签
Iisftp.vbs
linux使用
360
见解
scp
使用指南
menu
测试
宽字节字符
字母数字组合
httpclient
判断编
SqlServer200
0day
JVM性能优化
nfc应用
引擎
开发环境
SWF动画截图
独立
goto函数
delegate
infile
LIFO
promise化
总记录
Parse
后台运
80004005
Nginx访问日志
细粒度审计
文件编码
SQL-INJECTIO
选项值
变量命名
linux安装mysql
MySQL索引
倒计时跳转
调整列宽
数字相加
触摸事件
元素移动
索引器
c异或运算
远程拷贝
应用程序开发
行政区划数据
强制不换行
编译python
$.post
文件目录操作
添加CSS类
ListActivity
开启curl
动态树
彩图
filter
essential
NGEN
双网关
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

