在邮件正文中发送包含图像的电子邮件

时间:2014-07-13 16:19:12

标签: c# asp.net smtpclient

我想以这种格式发送电子邮件

mylogo_image

                                header(middle of page)

content
xyz
asd
abc

这是我的电子邮件代码:

string emailBody = "<html><body><img src=images/logo.png height=50px width=300px /><br><br><div style=text-align:center; font-size:x-large;>Letter Head</div>";
    SmtpClient client = new SmtpClient("mail.xyz.com", 2525);
            client.EnableSsl = false;
            client.Credentials = new System.Net.NetworkCredential("ab@xyz.com", "1");
            MailMessage msg = new MailMessage();
    msg.From = new MailAddress("a@xyz.com");
            msg.To.Add(new MailAddress(TextBox3.Text));
            msg.To.Add(new MailAddress("b@xyz.com"));
            msg.Subject = "Letter";
            String Body = emailBody + "Dear ";

            msg.Body = Body;

            msg.IsBodyHtml = true;
            try
            {
                client.Send(msg);
            }
            catch (Exception ex)
            {
                Response.Write("unable to send message:" + ex.Message);
            }

问题是它只显示图像区域但图像没有显示。 我正在使用asp.net c#。

1 个答案:

答案 0 :(得分:0)

为了控制电子邮件的格式,您应该用HTML编写电子邮件正文。如果该HTML中有图像,则需要从服务器下载图像,因此您需要使用完整的绝对URL。您的电子邮件正文应如下所示:

string emailBody = @"
<html>
<head></head>
<body>
<img src="http://www.mydomain.com/website/images/logo.png" alt="logo" />

<div style="text-align:center">
<img src="http://www.mydomain.com/website/images/banner.png" alt="banner" />
</div>

<p>
content
xyz
asd
abc
</p>
</body>
</html>
";

然后在创建MailMessage时,请务必将正文格式设置为HTML,如下所示:

System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.IsBodyHtml = true;
email.Body = emailBody;