代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
发送模板邮件并嵌入N张图片
作者:
dezai
/ 发布于
2014/7/30
/
694
private string mailServer = ConfigurationManager.AppSettings["EmailServer"]; private string mailUserName = ConfigurationManager.AppSettings["EmailUserName"]; private string mailPassword = ConfigurationManager.AppSettings["EmailPassword"]; private bool ssl = bool.Parse(ConfigurationManager.AppSettings["EmailSsl"]); private int port = int.Parse(ConfigurationManager.AppSettings["EmailPort"]); //toEmail 接收邮箱 public ServiceResult SendMail_imgTest(string toEmail) { var result = new ServiceResult() { Success = false, Message = "" }; try { var smtp = new SmtpClient(); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Host = mailServer;// smtp.Port = port;// smtp.EnableSsl = ssl;// smtp.Credentials = new System.Net.NetworkCredential(mailUserName, mailPassword); NameValueCollection myCol = new NameValueCollection(); string timeNow = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); myCol.Add("TaskName", "测试图片邮件"); myCol.Add("NowTime", timeNow); string mailBody = BulidByFile(Server.MapPath("/template/test.htm"), myCol); AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(mailBody, Encoding.UTF8, "text/html"); StreamReader StrStream = new StreamReader(Server.MapPath("/template/email_img.xml")); XElement Root = XElement.Load(StrStream.BaseStream); var emailImgList = (from img in Root.Elements().Elements("img") select img).ToList(); foreach (var i in emailImgList) { var src = i.FirstAttribute.Value; var name = i.LastAttribute.Value; LinkedResource lrImage = new LinkedResource(Server.MapPath("/template/" + src + ""), "image/gif"); lrImage.ContentId = name; htmlBody.LinkedResources.Add(lrImage); } StrStream.Close(); StrStream.Dispose(); MailMessage msg = new MailMessage(); // sender msg.From = new MailAddress("test@test.cn"); msg.AlternateViews.Add(htmlBody); msg.To.Add(toEmail); // if error, smtp server will be send notification msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; // subject 主题 msg.Subject = "测试图片邮件"; // subject encoding 编码 msg.SubjectEncoding = Encoding.UTF8; // format is html msg.IsBodyHtml = true; // body encoding msg.BodyEncoding = Encoding.UTF8; // body msg.Body = mailBody; smtp.Send(msg); result.Success = true; result.Message = mailBody; return result; } catch (Exception e) { return result; } } email_img.xml <?xml version="1.0" encoding="utf-8" ?> <Root> <emalimg> <img src="img1.jpg" cid="img_1"></img> <img src="img2.jpg" cid="img_2"></img> <img src="img3.jpg" cid="img_3"></img> <img src="img4.jpg" cid="img_4"></img> </emalimg> </Root> <div class="dp-highlighter bg_csharp"><div class="bar"><div class="tools"><b>[csharp]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-c"><li class="alt"><span><span><pre name=</span><span class="string">"code"</span><span> </span><span class="keyword">class</span><span>=</span><span class="string">"html"</span><span>>src图片路径 cid是test.html<img src=</span><span class="string">"cid:img_1"</span><span> />对应就行了</pre> </span></span></li><li class=""><span><pre></pre> </span></li><li class="alt"><span>test.html 调用图片方法<img src=<span class="string">"cid:img_1"</span><span> /> </span></span></li><li class=""><span><pre></pre> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span><pre></pre> </span></li><li class=""><span><pre></pre> </span></li></ol></div><pre name="code" class="csharp" style="display: none;"><div class="dp-highlighter bg_html"><div class="bar"><div class="tools"><b>[html]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;">view plain</a><a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;">copy</a><a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;">print</a><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;">?</a><div style="position: absolute; left: 0px; top: 0px; width: 0px; height: 0px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="0" height="0" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=0&height=0" wmode="transparent"></div></div></div><ol start="1" class="dp-xml"><li class="alt"><span><span>src图片路径 cid是test.html</span><span class="tag"><</span><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">"cid:img_1"</span><span> </span><span class="tag">/></span><span>对应就行了 </span></span></li></ol></div><pre name="code" class="html" style="display: none;">src图片路径 cid是test.html<img src="cid:img_1" />对应就行了</pre> <pre></pre> test.html 调用图片方法<img src="cid:img_1" /> <pre></pre> <pre></pre> <pre></pre> </pre>
试试其它关键字
发送模板邮件
并嵌入N张图片
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3