使用内嵌图像将Outlook嵌入到HTML中

时间:2012-04-10 14:25:28

标签: c# email

我们有一个论坛解决方案,允许人们通过电子邮件提交帖子。目前,在解析邮件时,图像仅作为附件添加到帖子中。我们想要做的是解析电子邮件并从邮件中获取嵌入的图像,并将它们转换为输出HTML中的内嵌图像。我们需要支持任何电子邮件客户端Outlook,Hotmail,Gmail等

Outlook原创电子邮件:

<img id="Picture_x0020_1" src="cid:image001.jpg@01CD172C.038D3C80">

期望的结果是我们保存附件并将src设为

<img id="Picture_x0020_1" src="http://www.site.com/default.aspx?action=ViewAttachment&paid=594">

我知道我们可以通过以下内容获取图像:.NET How to extract embedded image from email message?

我们是否需要破解RegEx或是否有简化此操作的库?我确信我们不是唯一想要将电子邮件呈现为HTML的人

1 个答案:

答案 0 :(得分:1)

你当然可以编写代码来提取嵌入的图像,并自己修改身体。它可能最终会成为很多工作。

我们使用http://www.quiksoft.com/

中的EasyMail.net

这足以让你入门:

private POP3 pop3 = new POP3();
pop3.Connect(pop3Address, port);
var memoryStream = new MemoryStream();
pop3.DownloadMessage(position, memoryStream);
var email = new EmailMessage(memoryStream)
var streamView = new HTMLStreamView(email, urlPrefix);
string body = streamView.Body;
int counter = 0;

foreach (Stream stream in streamView.Streams)
{
    if (counter != 0)
    {
        stream.Position = 0;
        byte[] embeddedObjectBytes = new byte[(int)stream.Length];
        stream.Read(embeddedObjectBytes, 0, (int)stream.Length);

        string urlAndCounter = urlPrefix + counter.ToString();
        string uniqueUrlAndCounter = GetUniqueUrl(urlAndCounter);
        if (body.Contains(urlAndCounter + "\""))
        {
          body = body.Replace(urlAndCounter + "\"", uniqueUrlAndCounter + "\"");
        }
        else if (body.Contains(urlAndCounter + " "))
        {
          body = body.Replace(urlAndCounter + " ", uniqueUrlAndCounter + " ");
        }

        SaveEmbeddedObject(embeddedObjectBytes,uniqueUrlAndCounter);
    }

    counter++;
}