.net中 发送邮件内容嵌入图片的具体实例
例程一
邮件内容调用图片格式为:<img src=\"cid:email001\">
发送邮件的服务端代码为:
smtpclient 发送邮件的对象 //代码省略
system.net.mail.mailmessage mailmessage = new system.net.mail.mailmessage();
mailmessage.from="发送者邮箱";
mailmessage.to.add("收件人邮件列表");
mailmessage.cc.add("抄送人邮件列表");
mailmessage.subject = subject;
alternateview htmlbody = alternateview.createalternateviewfromstring(content,null,"text/html");
linkedresource lrimage = new linkedresource("a.jpg","image/gif");
lrimage.contentid = "email001";
htmlbody.linkedresources.add(lrimage);
mailmessage.alternateviews.add(htmlbody);
smtpclient.send(mailmessage);
例程二
smtpclient smtp = new smtpclient();
smtp.deliverymethod = smtpdeliverymethod.network;
smtp.host = "smtp.163.com";
smtp.credentials = new networkcredential("renzhijie1111", "**");
mailmessage mm = new mailmessage();
mm.from = new mailaddress("renzhijie1111@163.com", "无敌任志杰测试");
mm.to.add("renzhijie1990@vip.qq.com");
mm.subject = "发送带图片邮件";
string plaintextbody = "如果你邮件客户端不支持html格式,或者你切换到“普通文本”视图,将看到此内容";
mm.alternateviews.add(alternateview.createalternateviewfromstring(plaintextbody, null, "text/plain"));
////html格式邮件的内容
string htmlbodycontent = "如果你的看到<b>这个</b>, 说明你是在以 <span style=\"color:red\">html</span> 格式查看邮件<br><br>";
htmlbodycontent += "<a href=\"//www.jb51.net//%22%3eva娱乐网</a> <img src=\"cid:weblogo\">"; //注意此处嵌入的图片资源
alternateview htmlbody = alternateview.createalternateviewfromstring(htmlbodycontent, null, "text/html");
linkedresource lrimage = new linkedresource(@"d:\1.jpg", "image/gif");
lrimage.contentid = "weblogo"; //此处的contentid 对应 htmlbodycontent 内容中的 cid: ,如果设置不正确,请不会显示图片
htmlbody.linkedresources.add(lrimage);
mm.alternateviews.add(htmlbody);
////要求回执的标志
mm.headers.add("disposition-notification-to", "renzhijie1111@163.com");
////自定义邮件头
mm.headers.add("x-website", "//www.jb51.net/");
////针对 lotus domino server,插入回执头
mm.headers.add("returnreceipt", "1");
mm.priority = mailpriority.normal; //优先级
mm.replyto = new mailaddress("renzhijie1111@163.com", "我自己");
////如果发送失败,smtp 服务器将发送 失败邮件告诉我
mm.deliverynotificationoptions = deliverynotificationoptions.onfailure;
////异步发送完成时的处理事件
smtp.sendcompleted += new sendcompletedeventhandler(smtp_sendcompleted);
////开始异步发送
smtp.sendasync(mm, null);