2014-02-07 88 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服务器之前正在测试该功能。 –