C# 如何使用asp.net 4.0和c发送包含多个嵌入图像的电子邮件#
我只想发送电子邮件与html正文中的多个图像。在这里,我想将每个图像文件附加到html正文中,html正文已经在我的web应用程序文件夹中 比如:C# 如何使用asp.net 4.0和c发送包含多个嵌入图像的电子邮件#,c#,asp.net,asp.net-4.0,C#,Asp.net,Asp.net 4.0,我只想发送电子邮件与html正文中的多个图像。在这里,我想将每个图像文件附加到html正文中,html正文已经在我的web应用程序文件夹中 比如: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Happy New Year - 4</title></head>
<body>
<table cellspacing="0" cellpadding="0" style="width: 100%;">
<tbody>
<tr>
<td style="text-align: center; vertical-align: top; background-color: #ffffff;"><table cellspacing="0" cellpadding="0" style="width: 688px; background-color: #ffffff; text-align: center; margin-left: auto; margin-right: auto;"> <tbody><tr> <td style="text-align: left; vertical-align: top; background-color: #ffffff;"> <table cellspacing="0" cellpadding="0" style="width: 100%;"> <tbody><tr> <td style="text-align: left; vertical-align: bottom; background-color: #ffffff;"><div><img src=""cid:30012015_152347.jpg" alt="" /></div></td> </tr> <tr> <td style="text-align: left; vertical-align: top;"> <table cellspacing="0" cellpadding="0" style="width: 100%;"> <tbody><tr> <td style="width: 269px; text-align: left; vertical-align: top;"><div><img src=""cid:30012015_152350.jpg" alt="" /></div></td> <td style="width: 357px; text-align: left; vertical-align: top; background-color: #a90100;"> <table cellspacing="0" cellpadding="0" style="width: 100%;"> <tbody><tr> <td style="text-align: left; vertical-align: top; background-color: #a90100;"> <div style="margin-left: 5px; margin-right: 5px;"> <div style="font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;"> <div style="margin-top: 0px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">Dear <b>##FirstName## ##LastName##,</b></div> <div style="margin-top: 4px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">Standing on the threshold of a New Year, We send you our warmest wishes. May this year shower you with everything you desire.</div> <div style="margin-top: 4px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">We wish you all the best for the year ahead.</div> <div style="margin-top: 4px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">Lots of love,</div> <div style="margin-top: 4px; font-family: verdana,"ms sans serif"; font-size: 11px; color: #ffffff; line-height: 13px; white-space: normal;">From your friends at:</div> <div style="width: 100%; font-family: trebuchet ms,verdana,"ms sans serif"; font-size: 14px; color: #ffffff; margin-top: 5px; white-space: normal;"><b>##UserOrganisation##</b></div> </div> </div> </td> </tr> </tbody></table> </td> <td style="width: 62px; text-align: left; vertical-align: top;"><div><img src=""cid:30012015_152352.jpg" alt="" /></div></td> </tr> </tbody></table> </td> </tr> <tr> <td style="text-align: left; vertical-align: top; background-color: #ffffff;"><div><img src=""cid:30012015_152354.jpg" alt="" /></div></td> </tr> </tbody></table> </td> </tr> <tr> <td style="text-align: center; vertical-align: top; padding-top: 10px;"> <table cellspacing="0" cellpadding="0" style="width: 688px;"> <tbody><tr> <td style="text-align: left; vertical-align: top; border-top: 1px solid #a7a7a7; border-bottom: 1px solid #a7a7a7; padding-top: 5px; padding-bottom: 5px;"> <table width="100%" cellspacing="0" cellpadding="0" style="width: 100%;"> <tbody><tr> <td width="50%" style="border-right: 1px solid #a7a7a7; width: 50%; text-align: left;"> <div style="margin: 5px 5px 5px 8px;"><span style="font-size: 16px; color: #e92f08; font-family: trebuchet ms,verdana,"ms sans serif"; font-weight: bold;">##UserOrganisation## or<br />Logo</span></div> </td> <td width="50%" style="width: 50%; text-align: left;"> <div style="margin: 5px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>Address : </b><br />##UserAddress1##, ##UserAddress2##,<br /></span><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px;">##UserCity## - ##UserPostalCode##.</span></div> <div style="margin: 6px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>Phone : </b><img width="12" height="1" src=""cid:" alt="" style="width: 12px; height: 1px;" />##UserPhone##</span></div> <div style="margin: 6px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>FAX : </b><img width="26" height="1" src=""cid:" alt="" style="width: 26px; height: 1px;" />##UserFax##</span></div> <div style="margin: 6px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>Email : </b><img width="18" height="1" src=""cid:" alt="" style="width: 18px; height: 1px;" /><a target="_blank" href="mailto:##UserEmailAddress##" style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px;">##UserEmailAddress##</a></span></div> <div style="margin: 6px 5px 5px 8px;"><span style="font-family: verdana,"ms sans serif"; color: #333333; font-size: 11px; white-space: normal;"><b>Visit us : </b><img width="7" height="1" src=""cid:" alt="" style="width: 7px; height: 1px;" /><Website Address></span></div> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </td> </tr> </tbody></table> </td> </tr> </tbody></table></body></html>
新年快乐-4
亲爱的,站在新年的门槛上,向你致以最热烈的祝愿。愿今年带给你你想要的一切。我们祝你在新的一年里一切顺利。很多爱,来自您的朋友:###用户组织####用户组织或
标识地址:
####用户地址1##,##用户地址2#,
###用户城市############。电话:#用户电话#传真:#用户传真#电子邮件:访问我们:网址
现在检查此电子邮件正文有多个src=“cid:DDMMYYYY\u HHMMSS.gif”此处所有文件都以唯一名称存储在我的本地web应用程序文件夹中。我想将每个文件附加到电子邮件正文。(不是电子邮件附件。电子邮件图像嵌入)
如何处理多个图像
请帮帮我。你可以使用下面这样的东西
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
你可以利用下面的东西
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
类程序
{
静态void Main(字符串[]参数)
{
SmtpMail-oMail=新的SmtpMail(“TryIt”);
SmtpClient oSmtp=新的SmtpClient();
//设置发件人电子邮件地址,请将其更改为您的
oMail.From=”test@emailarchitect.net";
//设置收件人电子邮件地址,请将其更改为您的
oMail.To=”support@emailarchitect.net";
//设置电子邮件主题
oMail.Subject=“测试带有附件的html电子邮件”;
//您的SMTP服务器地址
SmtpServer oServer=新的SmtpServer(“smtp.emailarchitect.net”);
//ESMTP身份验证的用户和密码(如果服务器不需要)
//用户身份验证,请删除以下代码。
oServer.User=”test@emailarchitect.net";
oServer.Password=“testpassword”;
//如果您的SMTP服务器需要SSL连接,请添加此行
//oServer.ConnectType=SmtpConnectType.ConnectSSLAuto;
尝试
{
//导入html正文,并将链接图像作为嵌入图像导入。
oMail.ImportHtml(“测试导入TML”,
“c:\\my picture”//test.gif位于c:\\my picture中
ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportCS);
Console.WriteLine(“开始发送带有嵌入图像的电子邮件…”);
SendMail(oServer,oMail);
Console.WriteLine(“电子邮件已成功发送!”);
}
捕获(异常ep)
{
Console.WriteLine(“发送电子邮件失败,错误如下:”);
控制台写入线(ep消息);
}
}
}
}课程计划
{
静态void Main(字符串[]参数)
{
SmtpMail-oMail=新的SmtpMail(“TryIt”);
SmtpClient oSmtp=新的SmtpClient();
//设置发件人电子邮件地址,请将其更改为您的
oMail.From=”test@emailarchitect.net";
//设置收件人电子邮件地址,请将其更改为您的
oMail.To=”support@emailarchitect.net";
//设置电子邮件主题
oMail.Subject=“测试带有附件的html电子邮件”;
//您的SMTP服务器地址
SmtpServer oServer=新的SmtpServer(“smtp.emailarchitect.net”);
//ESMTP身份验证的用户和密码(如果服务器不需要)
//用户身份验证,请删除以下代码。
oServer.User=”test@emailarchitect.net";
oServer.Password=“testpassword”;
//如果您的SMTP服务器需要SSL连接,请添加此行
//oServer.ConnectType=SmtpConnectType.ConnectSSLAuto;
尝试
{
//导入html正文,并将链接图像作为嵌入图像导入。
oMail.ImportHtml(“测试导入TML”,
“c:\\my picture”//test.gif位于c:\\my picture中
ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportCS);
Console.WriteLine(“开始发送带有嵌入图像的电子邮件…”);
SendMail(oServer,oMail);
Console.WriteLine(“电子邮件已成功发送!”);
}
捕获(异常ep)
{
Console.WriteLine(“发送电子邮件失败,错误如下:”);
控制台写入线(ep消息);
}
}
}
}下面是在邮件中发送多个图像的代码
string subject = "Subject";
string body = @"Image 1: <img src='src1'/> <br/> Image 2: <img src='src2'/> <br/> Some Other Content";
MailMessage mail = new MailMessage();
mail.From = new MailAddress("<FromAddress@mail.com>");
mail.To.Add(new MailAddress("<ToAddress@mail.com>"));
mail.Subject = subject;
mail.Body = body;
mail.Priority = MailPriority.Normal;
string contentID1 = Guid.NewGuid().ToString().Replace("-", "");
string contentID2 = Guid.NewGuid().ToString().Replace("-", "");
body = body.Replace("src1", "cid:" + contentID1);
body = body.Replace("src2", "cid:" + contentID2);
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
//path of image or stream
LinkedResource imagelink1 = new LinkedResource(@"F:\Images\020817045614.png", "image/png");
imagelink1.ContentId = contentID1;
imagelink1.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink1);
LinkedResource imagelink2 = new LinkedResource(@"F:\Images\020817045837.png", "image/png");
imagelink2.ContentId = contentID2;
imagelink2.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
htmlView.LinkedResources.Add(imagelink2);
mail.AlternateViews.Add(htmlView);
using (SmtpClient smtpClient = new SmtpClient("<SMTPHostAddress>"))
{
smtpClient.Send(mail);
}
string subject=“subject”;
字符串体=@“图像1:
图像2:
其他一些内容”;
MailMessage mail=新的MailMessage();
mail.From=新邮件地址(“”);
mail.To.Add(新邮件地址(“”);
mail.Subject=主题;
mail.Body=Body;
mail.Priority=MailPriority.Normal;
字符串contentID1=Guid.NewGuid().ToString().Replace(“-”,”);
字符串contentID2=Guid.NewGuid().ToString().Replace(“-”,”);
body=body.Replace(“src1”、“cid:+contentID1”);
body=body.Replace(“src2”、“cid:+contentID2”);
AlternateView htmlView=AlternateView.CreateAlternateView-FromString(正文,null,“文本/h