我想通过C#代码和多张图片发送电子邮件。当我发送没有任何图像的电子邮件时,它运行良好但是当我附加图像时,电子邮件中的内容不会显示。有一个名为EmailTemplate.html的模板,其中包含要在e中发送的布局-mail和相应的内容在SendMail()中提供。
SendMail代码()
protected void sendMail()
{
string body;
string blogTitle="Demo Title";
string blogCategory="Demo Category";
string blogHeadline="Demo Headline";
DateTime blogDate = DateTime.Now;
string registeredEmail="example@gmail.com";
using (StreamReader reader = new StreamReader(Server.MapPath(("~/EmailTemplate.html"))))
{
body = reader.ReadToEnd();
}
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("example@gmail.com");
mail.To.Add(registeredEmail);
mail.Subject = blogTitle;
//code to send multiple images in mail
AlternateView av1=AlternateView.CreateAlternateViewFromString(body,null,MediaTypeNames.Text.Html);
LinkedResource myPhoto=new LinkedResource("D:\\asp.net\\Learning Dot Net\\myPhoto.jpg");
myPhoto.ContentId="myImage";
av1.LinkedResources.Add(myPhoto);
myPhoto=new LinkedResource("D:\\asp.net\\Learning Dot Net\\books.jpg");
myPhoto.ContentId="blogImage";
av1.LinkedResources.Add(myPhoto);
mail.AlternateViews.Add(av1);
//code ends
mail.IsBodyHtml = true;
body = body.Replace("#Title", blogTitle);
body = body.Replace("#BlogDate", blogDate.ToString());
body = body.Replace("#BlogCategory", blogCategory);
body = body.Replace("#BlogHeadline", blogHeadline + ".........");
mail.Body = body;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("example@gmail.com", "12345678");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
EmailTemplate.html代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div>
<table border="0" cellpadding="15" style="width:550px; margin:0 auto;border: solid 7px blue;">
<tr>
<td colspan="3" style="text-align:center">
<label id="title" style="color:blue;font-size:15pt;">#Title</label>
</td>
</tr>
<tr>
<td style="width:130px;">
<label style="color:blue;font-size:15pt;">#BlogDate</label>
</td>
<td style="width:150px;">
<label style="color:blue;font-size:15pt;">#BlogCategory</label>
</td>
<td align="right">
<img src="cid:myImage" alt="abc" style="width:90px; height: 80px; border-radius:445px;"/>
</td>
</tr>
<tr>
<td colspan="3">
<img src="cid:blogImage" alt="blogImage" width="500" height="140" />
</td>
</tr>
<tr>
<td colspan="3">
#BlogHeadline
</td>
</tr>
<tr>
<td colspan="3">
<a href="ReadMoreUrl" style="text-decoration:none;" > Read More</a>
<a href="ReadMoreUrl" style="text-decoration:none;" > Add Comment</a>
<a href="ReadMoreUrl" style="text-decoration:none;" > See all comments</a>
<a href="http://localhost:1847/blog.aspx" style="text-decoration:none;" > All Blogs</a>
</td>
</tr>
<tr>
<td colspan="3">
<label>Reference </label> <a href="http://example.com" style="text-decoration:none;">http://example.com</a>
</td>
</tr>
<tr>
<td colspan="3">
<a href="UnsubscribeEmail">Unsubscribe</a><label> to no longer receive posts from <a href="http://example.com" style="text-decoration:none;">http://example.com</a>.</label>
</td>
</tr>
</table>
</div>
</body>
</html>
此代码发送的电子邮件显示#Title,#BlogDate,#BlogCategory和#BlogHeadline。代替这些内容,内容应在SendMail()中显示为已替换。
答案 0 :(得分:0)
您只需要更改代码的位置即可。首先,您必须添加邮件内容,之后您需要添加电子邮件代码。我已经尝试过我的系统并且它正常工作。
我无法解释这个的实际逻辑,但它在我的系统上工作,当我检查电子邮件时,它显示所有电子邮件内容以及图像。检查下面的快照。如果你再次遇到这个问题,请告诉我。
body = body.Replace("#Title", blogTitle);
body = body.Replace("#BlogDate", blogDate.ToString());
body = body.Replace("#BlogCategory", blogCategory);
body = body.Replace("#BlogHeadline", blogHeadline + ".........");
mail.Body = body;
AlternateView av1=AlternateView.CreateAlternateViewFromString(body,null,MediaTypeNames.Text.Html);
LinkedResource myPhoto=new LinkedResource("D:\\asp.net\\Learning Dot Net\\myPhoto.jpg");
myPhoto.ContentId="myImage";
av1.LinkedResources.Add(myPhoto);
myPhoto=new LinkedResource("D:\\asp.net\\Learning Dot Net\\books.jpg");
myPhoto.ContentId="blogImage";
av1.LinkedResources.Add(myPhoto);
mail.AlternateViews.Add(av1);
//code ends
mail.IsBodyHtml = true;