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
精彩图集
精彩文章
热门标签
设置
双击事件
学习过程
CreatThread
SQL语句优化
日期插件
cpu占用
汉字首字母
自学
C语言特性
接口和抽象类
php服务器环境
decode
卸载MySQ
c++函数
FTP自动备份
length
php连接mysql
顺序容器
远程拷贝
纵向表
pdf生成
平滑处理
复合主键
上网设置
Assert
windows命令行
scrapy
serialize()
SQL-UPDATE
php php在服务
无人职守
ip地址库
before触发器
RegExp
升级
del
全排列算法
Content-Loca
LCL.VBS
位运算
__setattr__
仿函数
自动恢复
比较运算符
jsonp
j
linux远程登录
var_export
非类型安全
缓存机制
with
头尾
ZOJ
7.5
当前方法名
系统补丁
的新增功
fga
skip-name-re
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229