如何在mail的正文显示图片
最近看到很多人在问这个问题.就是如何在Mail的正文中如何显示附件的图片?本人也不会就去网上搜索.可是网上竟然没有(可能是太简单,很多人不屑提供代码),于是本人就尝试.
最先想到的就是outLook可以显示附件中的图片.于是在OutLook的邮件正文:右键->ViewSource 就看到了
新建一个网站,拖几个FileUpload 上去.如下图
根据MicroSoft自带的System.Net.Mail 组件,完成发送方法,代码如下
然后看看我们的前台代码
写完之后,点击发送,我靠!真的可以也.
代码其实很简单我们来总结一下:
这里最重要的东西是在正文中如何使用Img显示附件中的图片,从代码中我们可以看到content-id:附件中图片名字的方案解决的.
以上是自己方法,如果谁有更好的方法请贴出来,大家共享!
声明:由于代码是简单测试是否可以在附件中显示附件,所以代码写的很乱.大家看思路就行了.
最先想到的就是outLook可以显示附件中的图片.于是在OutLook的邮件正文:右键->ViewSource 就看到了
1
" <img width=560 height=420 id="_x0000_i1025"
2
src="cid:image001.jpg@01C8C4AF.C7E6ED20">"
这种代码 所以产生的第一个想法就是在写正文的时候,自动根据附件去生成类似代码.说干就干,马上动手!
2

新建一个网站,拖几个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
28
//message.CC.Add(new MailAddress(from)); //超送给自己
29
//message.Bcc.Add(new MailAddress(""));
30
31
if (attch != null)
32
{
33
Attachment data = attch;
34
message.Attachments.Add(data);
35
}
36
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
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
73
74
75
}
76
}
77
78

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

然后看看我们的前台代码
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
14
public partial class _Default : System.Web.UI.Page
15
{
16
protected void Page_Load(object sender, EventArgs e)
17
{
18
19
}
20
protected void Button1_Click(object sender, EventArgs e)
21
{
22
QMail mail = new QMail();
23
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
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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

写完之后,点击发送,我靠!真的可以也.
代码其实很简单我们来总结一下:
这里最重要的东西是在正文中如何使用Img显示附件中的图片,从代码中我们可以看到content-id:附件中图片名字的方案解决的.
以上是自己方法,如果谁有更好的方法请贴出来,大家共享!
声明:由于代码是简单测试是否可以在附件中显示附件,所以代码写的很乱.大家看思路就行了.
分类:
NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!