2014-02-07 244 views
0

我試圖從我的本地開發機器發送帶有圖像的測試電子郵件。該圖像未作爲附件發送。 可能是一個愚蠢的問題,但有沒有什麼辦法可以從我的本地機器上運行的網站發送電子郵件用於測試目的,然後再將其託管在www上。將圖像附加到電子郵件

public static void SendMail(string emailBody, string to, string from, string subject) 
{ 
    MailMessage mailMessage = new MailMessage("[email protected]", "[email protected]"); 
    mailMessage.Subject = subject; 
    mailMessage.Body = emailBody; 
    mailMessage.IsBodyHtml = true; 
    //mailMessage.Body += "<br /><br /> <asp:Image ID='Image1' runat='server' ImageUrl='~/images/banner.jpg' />"; 

    string path = System.Web.HttpContext.Current.Server.MapPath(@"~/images/banner.jpg"); 
    AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><br/><img src=cid:companylogo/><br></body></html>" + emailBody, null, MediaTypeNames.Text.Html);LinkedResource logo = new LinkedResource(path); 
    av1.LinkedResources.Add(logo); 
    mailMessage.AlternateViews.Add(av1); 

    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(cs)) 
    { 
     SqlCommand sc = new SqlCommand("SELECT EmailAdd FROM Volunteers where Country like 'United K%' and Race like 'Pak%' ", con); 
     con.Open(); 
     SqlDataReader reader = sc.ExecuteReader(); 
     if (reader.HasRows) 
     { 
      while (reader.Read()) 
      { 
       mailMessage.To.Add(reader[0].ToString()); 
      } 
     } 
     reader.Close(); 
    } 

    SmtpClient smtpClient = new SmtpClient(); 
    smtpClient.Send(mailMessage); 
} 
+1

''有沒有什麼辦法可以在我的本地機器上運行我的網站發送電子郵件用於測試目的?' - 是的,絕對。運行本地SMTP偵聽器進行測試:http://smtp4dev.codeplex.com – David

+1

你如何區分並且不會與像'cs'和'sc'這樣的變量混淆?我會更有意義地命名它們。 – abatishchev

回答

0

看起來好像您可能沒有設置圖像的正確CID。這是我用我的應用程序一個方法:

// Construct the MailMessage object to be relayed to the SMTP server 
message = new MailMessage("[email protected]", "[email protected]"); 

string path = System.Web.HttpContext.Current.Server.MapPath(@"~/images/banner.jpg"); 
byte[] image = LoadImageAsByteArray(path); 

// create a stream object from the file contents 
var stream = new MemoryStream(image); 

// create a mailAttachment object 
var mailAttachment = new System.Net.Mail.Attachment(stream, "bannerAttachment.jpg") 
        { 
         // set the content id of the attachment so our email src links are valid 
         ContentId = Guid.NewGuid().ToString("N") 
        }; 

// set the attachment inline so it does not appear in the mail client as an attachment 
mailAttachment.ContentDisposition.Inline = true; 

// and then add the newly created mailAttachment object to the Attachments collection 
message.Attachments.Add(mailAttachment); 

而且......有一兩件事,我發現「怪」是你AlternateView的使用 - 這似乎有點多餘。我建議像這樣創建機構:

// Construct the body 
var body = string.Format("<html><body><br/><img src=cid:{0} /><br />{1}</body></html>", mailAttachment.ContentId, emailBody); 

// Subject 
message.Subject = "Whatever" 

// Ensure this is set to true so images are embedded correctly 
message.IsBodyHtml = true; 

// finally, add the body 
message.Body = body; 

// Create an smtp client object to initiate a connection with the server 
var client = new SmtpClient(smtpServerAddress, smtpServerPort.Value); 

// tell the client that we will be sending via the network (as opposed to using a pickup directory) 
client.DeliveryMethod = SmtpDeliveryMethod.Network; 

// and finally send the message 
client.Send(message); 

);

注:你將不得不照顧自己的認證等,因爲我沒有包括在內。此外,這是我自己的生產代碼的摘錄,所以請讓我知道你如何去。我會很樂意相應地修改我的答案。

+0

這LoadImageAsByteArray函數來自哪裏,它沒有被宣佈不被識別,有沒有我需要引用使用此的任何DLL。 –

+0

對不起,我認爲這是自我解釋。我用這裏所描述的方法: http://stackoverflow.com/questions/1131116/pdf-to-byte-array-and-vice-versa 或者,你可以使用下面介紹的方法: http://stackoverflow.com/questions/7350679/convert-a-bitmap-into-a-byte-array-in-c – agAus

+0

這將很高興知道這是否適合你或不交配? – agAus

0

這是您的解決方案。

string ss = "<div style='background:#e0e1e3;width:800px; margin:20px auto;border:1px solid #d8d9db;'>"; 
       ss = ss + "<div style='background:#ffffff; padding:10px;'>"; 
       ss = ss + "<img src='http://example.com/images/myimage.jpg' /></div>"; 



    MailMessage MailMsg = new MailMessage(); 
       MailMsg.To.Add("[email protected]"); 
       MailMsg.From = new MailAddress("Test.co.in"); 
       MailMsg.Subject = "Your subject"; 
       MailMsg.BodyEncoding = System.Text.Encoding.UTF8; 
       MailMsg.IsBodyHtml = true; 
       MailMsg.Priority = MailPriority.High; 
       MailMsg.Body = ss; 
    SmtpClient tempsmtp = new SmtpClient(); 
       tempsmtp.Host = "smtp.gmail.com"; 
       tempsmtp.Port = 587; 
       tempsmtp.EnableSsl = true; 
       tempsmtp.Credentials = new System.Net.NetworkCredential("[email protected]", "welcome"); 
tempsmtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
      tempsmtp.Send(MailMsg); 
+0

看來您使用的映像已放置在網絡上,我的映像位於本地解決方案文件夾中,我想將它附加到電子郵件。我在將解決方案部署到Web服務器之前正在測試該功能。 –