最近看到很多人在问这个问题.就是如何在Mail的正文中如何显示附件的图片?本人也不会就去网上搜索.可是网上竟然没有(可能是太简单,很多人不屑提供代码),于是本人就尝试.
最先想到的就是outLook可以显示附件中的图片.于是在OutLook的邮件正文:右键->ViewSource 就看到了
1"
"
这种代码所以产生的第一个想法就是在写正文的时候,自动根据附件去生成类似代码.说干就干,马上动手!
新建一个网站,拖几个FileUpload 上去.如下图
根据MicroSoft自带的System.Net.Mail 组件,完成发送方法,代码如下
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Net.Mail;
5using System.Net;
6using System.IO;
7namespace STS.MailSystem.Common
8{
9publicclass QMail
10{
11///
12/// 描述:Email发送通用函数
13///
14///
发件人
15///
收件人(多个收件人以逗号隔开)
16///
主题
17///
内容
18///
附件
19///
20publicstring MailSend(string from, string to, string cc, string subject, string text, Attachment attch, string priority)
21{
22 MailMessage message =new MailMessage(from, to);
23 message.CC.Add(cc);
24 message.Subject = subject;
25 message.Body = text;
26
27
28//message.CC.Add(new MailAddress(from)); //超送给自己
29//message.Bcc.Add(new MailAddress(""));
30
31if (attch !=null)
32{
33 Attachment data = attch;
34 message.Attachments.Add(data);
35 }
36
37 message.BodyEncoding = System.Text.Encoding.UTF8;//编码方式
38switch (priority.ToUpper())
39{
40case"HIGH":
41 message.Priority = MailPriority.High;//优先级
42break;
43case"NORMAL":
44 message.Priority = MailPriority.Normal;//优先级
45break;
46case"LOW":
47 message.Priority = MailPriority.Low;//优先级
48break;
49default:
50 message.Priority = MailPriority.Normal;//优先级
51break;
52 }
53
54 message.IsBodyHtml =true;//是否是html格式
55 SmtpClient client =new SmtpClient();//不同情况更改
56
57//client.Credentials = CredentialCache.DefaultNetworkCredentials;//匿名认证
58
59
60try
61{
62 client.Send(message);
63return"1";
64 }
65catch (Exception e)
66{
67
68return e.Message;
69 }
70
71 }
72
73
74
75 }
76}
77
78
然后看看我们的前台代码
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Net.Mail;
11using STS.MailSystem.Common;
12using System.Text;
13
14publicpartialclass _Default : System.Web.UI.Page
15{
16protectedvoid Page_Load(object sender, EventArgs e)
17{
18
19 }
20protectedvoid Button1_Click(object sender, EventArgs e)
21{
22 QMail mail =new QMail();
23
24 Attachment attachment =null;
25//File Name
26string fileName =string.Empty;
27string filePath =string.Empty;
28 StringBuilder mailBody =new StringBuilder();
29//mailBody.Append("content-type:base64");
30//mailBody.Append("content-transfer-encodinf:");
31//mailBody.Append("content-disposition:inline");
32//mailBody.Append("filename:aa");
33//增加附件
34//这里指去考虑附件是图片的情况.
35//其他情况不考虑
36if (File1.Value !="")
37{
38 filePath =this.File1.PostedFile.FileName;
39 fileName = filePath.Substring(filePath.LastIndexOf("\\") +1);
40//增加显示图片
41 mailBody.Append("content-id:"+ fileName);
42 mailBody.Append("
");
43 attachment =new Attachment(filePath);
44 }
45
46
47//Send Mail
48 mail.MailSend("minqiang.zhang@metinform.cn", "minqiang.zhang@metinform.cn",
49"minqiang.zhang@metinform.cn", "演示如果在正文显示附件", mailBody.ToString(), attachment, "");
50
51 }
52 }
53
54