2015-05-29 100 views
0

我正嘗試從my.net應用程序發送電子郵件。 我已經在其中包含一個圖像。我在電子郵件中獲得圖像。問題是圖像也作爲附件來臨。 我只需要內嵌圖像。不附件。任何刪除附件的選項? 我有包括從以下兩個選項中任一項下面C#。如何在發送郵件時避免附加圖像

body = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"; 
        body += "<HTML><HEAD><META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">"; 
        body += "</HEAD><BODY><DIV><FONT face=Arial color=#ff0000 size=2>this is some HTML text"; 
        body += "</FONT></DIV><DIV><img width=600 height=100 id=\"_x0000_i1028\" src=\"cid:cid1\" alt=\"KPMG LINK\"></DIV></BODY></HTML>"; 
        AlternateView alternate = AlternateView.CreateAlternateViewFromString(body, null, "text/plain"); 
        AlternateView alternateHtml = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); 
        LinkedResource resource = null; 
        resource = new LinkedResource(ImagePath, new ContentType("image/png")); 
        resource.ContentId = "cid"; 
        alternate.LinkedResources.Add(resource); 
        message.AlternateViews.Add(alternate); 
        message.AlternateViews.Add(alternateHtml);       

        smtp.Send(message); 

回答

0

嘗試的代碼:(Reference

選項1: -

System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(@"imagepath\filename.png"); 
inline.ContentDisposition.Inline = true; 

選項2: -

using (var client = new SmtpClient()) 
{ 
    MailMessage newMail = new MailMessage(); 
    newMail.To.Add(new MailAddress("[email protected]")); 
    newMail.Subject = "Test Subject"; 
    newMail.IsBodyHtml = true; 

    var inlineLogo = new LinkedResource(Server.MapPath("~/Path/To/YourImage.png")); 
    inlineLogo.ContentId = Guid.NewGuid().ToString(); 

    string body = string.Format(@" 
      <p>Lorum Ipsum Blah Blah</p> 
      <img src=""cid:{0}"" /> 
      <p>Lorum Ipsum Blah Blah</p> 
     ", inlineLogo.ContentId); 

    var view = AlternateView.CreateAlternateViewFromString(body, null, "text/html"); 
    view.LinkedResources.Add(inlineLogo); 
    newMail.AlternateViews.Add(view); 

    client.Send(newMail); 
} 
相關問題