[.net] 如何在mail的加入正文显示图片
转贴 原文:http://www.cnblogs.com/mqcan/archive/2008/06/02/1212157.html
最近看到很多人在问这个问题.就是如何在Mail的正文中如何显示附件的图片?本人也不会就去网上搜索.可是网上竟然没有(可能是太简单,很多人不屑提供代码),于是本人就尝试.
最先想到的就是outLook可以显示附件中的图片.于是在OutLook的邮件正文:右键->ViewSource 就看到了
新建一个网站,拖几个FileUpload 上去.如下图![](https://www.cnblogs.com/images/cnblogs_com/mqcan/mail1.GIF)
根据MicroSoft自带的System.Net.Mail 组件,完成发送方法,代码如下
然后看看我们的前台代码
写完之后,点击发送,我靠!真的可以也.
代码其实很简单我们来总结一下:
这里最重要的东西是在正文中如何使用Img显示附件中的图片,从代码中我们可以看到content-id:附件中图片名字的方案解决的.
以上是自己方法,如果谁有更好的方法请贴出来,大家共享!
声明:由于代码是简单测试是否可以在附件中显示附件,所以代码写的很乱.大家看思路就行了.
最近看到很多人在问这个问题.就是如何在Mail的正文中如何显示附件的图片?本人也不会就去网上搜索.可是网上竟然没有(可能是太简单,很多人不屑提供代码),于是本人就尝试.
最先想到的就是outLook可以显示附件中的图片.于是在OutLook的邮件正文:右键->ViewSource 就看到了
1
" <img width=560 height=420 id="_x0000_i1025"
2
src="cid:image001.jpg@01C8C4AF.C7E6ED20">"
这种代码 所以产生的第一个想法就是在写正文的时候,自动根据附件去生成类似代码.说干就干,马上动手!![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
新建一个网站,拖几个FileUpload 上去.如下图
根据MicroSoft自带的System.Net.Mail 组件,完成发送方法,代码如下
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Net.Mail;
5
using System.Net;
6
using System.IO;
7
namespace STS.MailSystem.Common
8
{
9
public class QMail
10
{
11
/// <summary>
12
/// 描述:Email发送通用函数
13
/// </summary>
14
/// <param name="from">发件人</param>
15
/// <param name="to">收件人(多个收件人以逗号隔开)</param>
16
/// <param name="subject">主题</param>
17
/// <param name="text">内容</param>
18
/// <param name="attch">附件</param>
19
/// <returns></returns>
20
public string 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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
//message.CC.Add(new MailAddress(from)); //超送给自己
29
//message.Bcc.Add(new MailAddress(""));
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
if (attch != null)
32
{
33
Attachment data = attch;
34
message.Attachments.Add(data);
35
}
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
message.BodyEncoding = System.Text.Encoding.UTF8;//编码方式
38
switch (priority.ToUpper())
39
{
40
case "HIGH":
41
message.Priority = MailPriority.High;//优先级
42
break;
43
case "NORMAL":
44
message.Priority = MailPriority.Normal;//优先级
45
break;
46
case "LOW":
47
message.Priority = MailPriority.Low;//优先级
48
break;
49
default:
50
message.Priority = MailPriority.Normal;//优先级
51
break;
52
}
53
54
message.IsBodyHtml = true;//是否是html格式
55
SmtpClient client = new SmtpClient();//不同情况更改
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
//client.Credentials = CredentialCache.DefaultNetworkCredentials;//匿名认证
58
59
60
try
61
{
62
client.Send(message);
63
return "1";
64
}
65
catch (Exception e)
66
{
67
68
return e.Message;
69
}
70
71
}
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
}
76
}
77![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
78![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
然后看看我们的前台代码
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using System.Net.Mail;
11
using STS.MailSystem.Common;
12
using System.Text;
13![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
public partial class _Default : System.Web.UI.Page
15
{
16
protected void Page_Load(object sender, EventArgs e)
17
{
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
}
20
protected void Button1_Click(object sender, EventArgs e)
21
{
22
QMail mail = new QMail();
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
Attachment attachment = null;
25
//File Name
26
string fileName = string.Empty;
27
string 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
//其他情况不考虑
36
if (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("<img src='cid:" + fileName+"' />");
43
attachment = new Attachment(filePath);
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
//Send Mail
48
mail.MailSend("minqiang.zhang@metinform.cn", "minqiang.zhang@metinform.cn",
49
"minqiang.zhang@metinform.cn", "演示如果在正文显示附件", mailBody.ToString(), attachment, "");
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
}
52
}
53![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
写完之后,点击发送,我靠!真的可以也.
代码其实很简单我们来总结一下:
这里最重要的东西是在正文中如何使用Img显示附件中的图片,从代码中我们可以看到content-id:附件中图片名字的方案解决的.
以上是自己方法,如果谁有更好的方法请贴出来,大家共享!
声明:由于代码是简单测试是否可以在附件中显示附件,所以代码写的很乱.大家看思路就行了.