2014-03-13 23 views
0

我正在寫一個windows服務,它將觸發電子郵件。該功能也可以使用相同的代碼從Windows應用程序觸發。電子郵件與資源文件中提取的簽名標誌

我們觸發的電子郵件應該有一個帶有徽標的簽名,該徽標位於資源文件中。電子郵件被觸發得很好,但圖像沒有顯示,但圖像的佔位符是。這是我使用的代碼:

![try 
{ 
    Bitmap imgAttachment = Project.Shared.Properties.Resources.Image1; 
    using (MemoryStream mstream = new MemoryStream()) 
    { 
    ContentType type = new ContentType() 
    { 
    MediaType = MediaTypeNames.Image.Jpeg, 
    Name = "attachment" 
    }; 
    imgAttachment.Save(mstream, ImageFormat.Jpeg); 
    Attachment sigAttachment = new Attachment(mstream, type); 
    string ContentId = "imagelogo"; 
    sigAttachment.ContentId = ContentId; 
    sigAttachment.ContentDisposition.Inline = true; 
    sigAttachment.ContentDisposition.DispositionType = DispositionTypeNames.Inline; 
    message.Attachments.Add(sigAttachment); 

    message.Body = Body + "<htm><body> <br/><br/><table border='0' cellpadding='0'  
        cellspacing='0' width='1000'> <tr><td>&nbsp;</td></tr> <tr> <td 
        rowspan='8' valign='top' align='left'><img alt = 'Logo' src=\"cid:" + 
        ContentId + "\"></td> <td width='1000' height='46' valign='top'> " + 
        SignatureLine1 + " </td> </tr> <tr> " + SignatureLine2 + " </tr> <tr> " + 
        SignatureLine3 + " </tr> <tr> " + SignatureLine4 + " </tr> </table> 
        </body></html>"; 
    client.Send(message); 
    } 
    } 
    catch {}] 

1

有人能幫助我嗎?我錯過了什麼嗎?

+0

我懷疑附件和嵌入式資源之間有細微的差別,這可能被認爲是這樣的重複:http://stackoverflow.com/questions/18358534/send-inline-image-in-email – David

+0

我試過了也是如此,但我認爲我的問題與你提到的帖子有點不同,給出了相同的結果。 (請參閱附件)。該圖像沒有收到罰款。 – Jinith

+0

如果在保存圖像後將mstream.Position設置爲0會怎樣? – jstedfast

回答

0

問題是您沒有生成正確的MIME結構。 HTML主體和圖像附件需要成爲multipart/related的一部分,你寫的代碼可能會創建一個multipart/mixed(我沒有真正測試過你的代碼,而是在Mono的System.Net.Mail實現中挖掘出來的,看起來你的代碼會生成一個multipart/mixed而不是multipart/related,這就是你想要的)。

我對System.Net.Mail沒有太多的經驗(它太糟糕了,所以我寫了自己的庫),但是你需要做的是使用AlternateView作爲HTML郵件正文,並添加圖像作爲LinkedResourceAlternateView(請參閱AlternateView.LinkedResources屬性)。