2011-05-20 76 views
1

我使用c#代碼將郵件發送給其他用戶。 它工作正常,當我發送文本.. 現在我想發送圖像,以便我發送的圖像應該在他的收件箱中打開。意味着圖像應該正文不走的附件.. 我用來發送圖像的代碼:在asp中發送郵件圖像

System.Net.Mail.Attachment attach = 
       new System.Net.Mail.Attachment(
        "C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpg"); 
      Random Rgen = new Random(); 
      attach.ContentId = Rgen.Next(100000, 9999999).ToString(); 
      attach.ContentDisposition.Inline = true; 
    MailMessage m = new MailMessage(); 
m.From = new MailAddress("[email protected]"); 
      m.To.Add(new MailAddress("[email protected]")); 
    m.IsBodyHtml = true; 

      m.Body = "<html><body><h1>Picture</h1><br><img src='cid:" + attach.ContentId + "'></body></html>"; 
      //m.Body = inline.ToString(); 
      // m.Body = "<img src='cid:" + attach.ContentId + "'>"; 
      SmtpClient client = new SmtpClient("smtp.gmail.com"); 
      client.Credentials = new System.Net.NetworkCredential("[email protected]", "swe"); 
      client.EnableSsl = true; 

      client.Send(m); 

但不發送圖像的消息..

請幫我出..

回答

0

這應該適用於你建立你的附件

attach.ContentDisposition.Inline = true; 
attach.ContentDisposition.DispositionType = DispositionTypeNames.Inline; 
attach.ContentId = contentID; 
attach.ContentType.MediaType = "image/png"; 

清除體內img標籤,只是做m.Attachments.Add(attach);

+0

@ V4Vendetta:它發出了我所賜的m.body文字......爲」

圖片


「 – kawade 2011-05-20 11:21:24

+0

然後,您應該爲'MailMessage'實例設置'IsBodyHtml'爲true – V4Vendetta 2011-05-20 11:24:54

+0

沒有..不工作..現在收到的郵件不包含任何東西... – kawade 2011-05-20 11:36:06

0

我認爲你所要做的就是指定你的郵件格式。即

m.BodyFormat = MailFormat.Html; 

也許這會給你一個不同的想法。我有一個類似的任務(編碼爲Intranet)。所以......我做的是 - 將圖像上傳到網絡服務器,以便我已經擁有了URI。在代碼隱藏中,我使用這些URI作爲圖像的src。

mail.Body = "<html><body><img src='" + img_uri + "'></body></html>"; 
+0

MailMessage.BodyFormat已被棄用,並且是System.Web.Mail.MailMessage的一部分。 這似乎是作者正在使用System.Net.Mail ... – mattezell 2011-07-08 22:28:51

0

請嘗試這個辦法:找到here

Public Sub EmbeddedImages() 

     'create the mail message 
     Dim mail As New MailMessage() 

     'set the addresses 
     mail.From = New MailAddress("[email protected]", " Display Name") 
     mail.To.Add("[email protected]") 

     'set the content 
     mail.Subject = "This is an embedded image mail" 

     'first we create the Plain Text part 
     Dim palinBody As String = "This is my plain text content, viewable by 
     those clients that don't support html" 
     Dim plainView As AlternateView = 
     AlternateView.CreateAlternateViewFromString(palinBody, Nothing, 
     "text/plain") 
     'then we create the Html part 
     'to embed images, we need to use the prefix 'cid' in the img src value 
     Dim htmlBody As String = "<b>This is the embedded image 
     file.</b><DIV>&nbsp;</DIV>" 
     htmlBody += "<img alt="""" hspace=0 src=""cid:uniqueId"" align=baseline 
     border=0 >" 
     htmlBody += "<DIV>&nbsp;</DIV><b>This is the end of Mail...</b>" 
     Dim htmlView As AlternateView = 
     AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, 
     "text/html") 

     'create the AlternateView for embedded image 
     Dim imageView As New AlternateView("c:\attachment\image1.jpg", 
     MediaTypeNames.Image.Jpeg) 
     imageView.ContentId = "uniqueId" 
     imageView.TransferEncoding = TransferEncoding.Base64 

     'add the views 
     mail.AlternateViews.Add(plainView) 
     mail.AlternateViews.Add(htmlView) 
     mail.AlternateViews.Add(imageView) 

     'send mail 
     SendMail(mail) 

End Sub ' End EmbedImages 

HTH