簡體   English   中英

發送HTML文檔(包含圖片)作為電子郵件附件C#

[英]Send Html document(contianing Images) as an email attachment C#

我正在使用C#在運行時將HTML文檔(包含圖片)作為電子郵件附件發送。
但是當我檢查收到的電子郵件時,發送的html文檔不包含任何圖像。

能否請您指導我如何確保HTML文檔與圖像一起發送。

        MailMessage objMailMessage = new MailMessage();
        objMailMessage.From = new MailAddress("aa@gmail.com");

        string[] emailIds = objReportRequest.EmailIds.Split(',');
        foreach (string emailId in emailIds)
        {
            objMailMessage.To.Add(new MailAddress(emailId));
        }

        objMailMessage.IsBodyHtml = true; 
        objMailMessage.Body = messageBody;
        objMailMessage.Subject = "Test Service";
        objMailMessage.Attachments.Add(new Attachment(filePath));

        SmtpClient objSmtpClient = new SmtpClient("smtp.gmail.com");
        objSmtpClient.Credentials = new NetworkCredential("aaa@gmail.com", "aaa");
        objSmtpClient.EnableSsl = true;
        objSmtpClient.Send(objMailMessage);

我收到的html文檔為電子郵件附件,但未顯示圖像。

謝謝!

AlternateView htmlView = AlternateView.CreateAlternateViewFromString(message, null, "text/html");
 LinkedResource image = new LinkedResource(@".\images\sample.jpg");
 image.ContentId = "Image";
 message = "<img src=cid:Image>" + message;
 htmlView = AlternateView.CreateAlternateViewFromString(message, null, "text/html");
 htmlView.LinkedResources.Add(image);

這就是您將圖像放入HTML電子郵件的方式,根據MSDN ,html附件使用相同的對象,但是我沒有專門執行此操作的代碼。

如果HTML作為附件發送,我認為您不能在其中嵌入圖像。 畢竟,HTML是文本。

如果圖像是靜態的,我認為最好的選擇是通過絕對路徑引用該圖像,並將該圖像托管在您的一台服務器上。

由於您將HTML 作為附件而不是作為正文發送,因此沒有一種簡單的方法。

以下是一些選擇:

  1. 最好的方法是實際構建MHT(或MHTML)文檔,並將其作為附件發送。

  2. 您可能需要使用HTML填充電子郵件正文,然后嵌入圖像,而不是將HTML添加為附件。

  3. 另一種選擇是簡單地構建電子郵件,將圖像嵌入該電子郵件中,然后將電子郵件添加為附件。

  4. 如果收件人將使用更新的瀏覽器,則可以使用數據URL方案將圖像直接嵌入標簽中。 http://en.wikipedia.org/wiki/Data_URI_scheme

  5. 就像建議的其他海報一樣,在標簽中使用絕對鏈接,並將圖像托管在某個位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM