将图像附加到电子邮件

时间:2014-02-07 00:18:44

标签: c# asp.net email

我试图从我的本地开发机器发送带有图像的测试邮件。图像未作为附件发送。 可能是一个愚蠢的问题,但是有任何办法我可以在我的本地机器上运行电子邮件以进行测试,然后再将其托管在www。

public static void SendMail(string emailBody, string to, string from, string subject)
{
    MailMessage mailMessage = new MailMessage("to@to.com", "from@test.com");
    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);
}

2 个答案:

答案 0 :(得分:0)

看起来您可能没有设置图像的正确CID。这是我在我的一个应用程序中使用的方法:

// Construct the MailMessage object to be relayed to the SMTP server
message = new MailMessage("to@to.com", "from@test.com");

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);

);

注意:您必须照顾自己的身份验证等,因为我没有包含它。此外,这是我自己的生产代码的摘录,所以,请让我知道你如何去。我会非常乐意相应地修改我的答案。

答案 1 :(得分: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("test@test.com");
                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("test@test.com", "welcome");
tempsmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            tempsmtp.Send(MailMsg);