惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
Recent Announcements
Recent Announcements
C
Check Point Blog
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
爱范儿
爱范儿
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
月光博客
月光博客
AI
AI
美团技术团队
SecWiki News
SecWiki News
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cybersecurity and Infrastructure Security Agency CISA
M
MIT News - Artificial intelligence
PCI Perspectives
PCI Perspectives
aimingoo的专栏
aimingoo的专栏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Visual Studio Blog
T
The Exploit Database - CXSecurity.com
小众软件
小众软件
N
News | PayPal Newsroom
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
MongoDB | Blog
MongoDB | Blog
H
Heimdal Security Blog
Schneier on Security
Schneier on Security
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
D
Docker
Spread Privacy
Spread Privacy
Cloudbric
Cloudbric
www.infosecurity-magazine.com
www.infosecurity-magazine.com
I
Intezer
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
AWS News Blog
AWS News Blog

博客园 - 陋室

成功与一截树枝 sql 操作全集-整理收藏 JavaScript使用技巧精萃(转载) ASP.NET 中在指定的位置处插入字符 提示按下大写键的控件:MQTool(提供下载) NET设计模式之一:装饰模式(Decorator Pattern) 微软AJAX 教学系列第一讲:ScriptManager控件 微软AJAX 教学系列第一讲:局部刷新Partial Page Updates(翻译) 李开复:21世纪最需要的7种人才 Building a File Service 超卓越的你_读完后让你恢复自信 how to write professional business letters? 到底什么是托管,什么是非托管的研究 数据库设计中的五个范式(本文转载,收藏下) 如何直接用XML做数据源? ASP.NET程序的优化建议<转> ASP.NET部署与安装_MSI制作图文教程. 在打包的时候,创建应用程序池,并自动将程序assign到新创建的池中(MSI制作) Understand SQL Cache Notifications
如何在mail的正文显示图片
陋室 · 2008-06-02 · via 博客园 - 陋室

    最近看到很多人在问这个问题.就是如何在Mail的正文中如何显示附件的图片?本人也不会就去网上搜索.可是网上竟然没有(可能是太简单,很多人不屑提供代码),于是本人就尝试.
     最先想到的就是outLook可以显示附件中的图片.于是在OutLook的邮件正文:右键->ViewSource 就看到了

1<img width=560 height=420 id="_x0000_i1025"
2src="cid:image001.jpg@01C8C4AF.C7E6ED20">

这种代码 所以产生的第一个想法就是在写正文的时候,自动根据附件去生成类似代码.说干就干,马上动手!
    新建一个网站,拖几个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{
 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

然后看看我们的前台代码

 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
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

写完之后,点击发送,我靠!真的可以也.
     代码其实很简单我们来总结一下:
     这里最重要的东西是在正文中如何使用Img显示附件中的图片,从代码中我们可以看到content-id:附件中图片名字的方案解决的.
以上是自己方法,如果谁有更好的方法请贴出来,大家共享!
声明:由于代码是简单测试是否可以在附件中显示附件,所以代码写的很乱.大家看思路就行了.