龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

ASP.NET通过SMTP和ESMTP实现邮件发送带图片内容的邮件(2)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
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


精彩图集

赞助商链接