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
精彩图集
精彩文章
热门标签
设置宽度
加强版
php PHP计算指
作用域
AJAX应用
查询计划
MERGE
ActiveXObjec
文件夹权限
最小
auto_increme
Cookie cooki
php查询whois信息
常对象成员
自动清理
有三种
打开很慢
强制下载
联接字符串
调用windowsapi
Singleton
mysql服务
添加a标签
数据类型
C语言程序
次数
navicat
分割字符串
循环链表
sql命令
硬盘
遍历子节点
释放内存
大数据
UpdateDaset
CONVERT
PHP扩展配置
自动删除
php 一个用于
asp.net2.0
201
选择排
解压缩zip
抄表机
CreateProces
无限级分类
jpg
php php取得字
预览Office文件
Pecl
php 检测
inline-block
推荐
字符串比较
提取文件
生成字母
python算法
用户密码
建造者模式
大小写转换
赞助商链接
@CopyRight 2002-2008, 1SOHU.COM, Inc. All Rights Reserved QQ:1010969229

