http://blog.163.com/hao_2468/blog/static/130881568201141251642215/

.net System.Web.Mail发送邮件

2011-05-12 17:16:42|  分类: asp.net学习 |  标签:.net发送邮件  |举报|字号 订阅

 
 

net System.Web.Mail发送邮件

用System.Web.Mail发送邮件,适用于.net1.1。net2.0请用System.Net.Mail

先引用System.Web

1,发送简单邮件

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "you@yourcompany.com";

mail.Subject = "this is a test email.";

mail.Body = "this is my test email body";

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

这里的smtpserver只能是那些不需要验证的smtp服务器,像126,sina,yahoo等等的邮箱,都需要验证,所以不能用。用这些邮箱发信后面会讲到

2,发送Html邮件

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "you@yourcompany.com";

mail.Subject = "this is a test email.";

mail.BodyFormat = MailFormat.Html;

mail.Body = "this is my test email body.<br><b>this part is in bold</b>";

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

3,发送附件

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "you@yourcompany.com";

mail.Subject = "this is a test email.";

mail.Body = "this is my test email body.";

MailAttachment attachment = new MailAttachment( Server.MapPath( "test.txt" ) ); //create the attachment

mail.Attachments.Add( attachment ); //add the attachment

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

4,修改发件人和收件人的名称

比如发件人的地址是abc@126.com,我们用outlook收到信,From一栏里将直接显示abc@126.com.

能不能在From一栏里显示友好一点的名字呢?

比如显示Tony Gong

方法如下:

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "\"John\" <me@mycompany.com>";

mail.From = "\"Tony Gong\" <you@yourcompany.com>";

mail.Subject = "this is a test email.";

mail.Body = "this is my test email body.";

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

5,发送给多人

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com;him@hiscompany.com;her@hercompany.com";

mail.From = "you@yourcompany.com";

mail.Subject = "this is a test email.";

mail.Body = "this is my test email body.";

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

6,用需要Smtp验证的邮箱发信

现在为了防止垃圾邮件,绝大部分Smtp服务器需要验证了

发信方法如下:

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "abc@126.com";

mail.Subject = "this is a test email.";

mail.Body = "Some text goes here";

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); //set your username here

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); //set your password here

SmtpMail.SmtpServer = "smtp.126.com"; //your real server goes here

SmtpMail.Send( mail );

7,修改smtp服务器的端口,以及使用SSL加密

大部分smtp服务器的端口是25,但有些却不是

同时,绝大部分Smtp服务器不需要SSL登陆,有些却需要

比如Gmail,smtp端口是:465,同时支持SSL

代码如下:

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "abc@126.com";

mail.Subject = "this is a test email.";

mail.Body = "Some text goes here";

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); //set your username here

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); //set your password here

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",465);

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");

SmtpMail.SmtpServer = "smtp.126.com"; //your real server goes here

SmtpMail.Send( mail );

.net System.Web.Mail发送邮件 (设置发件人 只显示用户名)的更多相关文章

  1. System.Web.mail ----虚拟发件人发送邮件

     转载别人的 使用SMTP发送邮件   说到邮件发送,先提一下SMTP. SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议.它是一组用于从源地址到目的 ...

  2. C# 发送电子邮件(含附件)用到的类 system.web.mail

    主要是用到了System.Web.Mail命名空间,用到了此空间的三个类,分别是: ●MailMessage类,用于构造电子邮件●MailAttachment类,用于构造电子邮件附件●SmtpMail ...

  3. C#中使用System.Web.Mail.MailMessage类无法CC多人的问题

    从.NET 2.0 开始,引入了一个新的类,System.Net.Mail.MailMessage.该类用来取代 .NET 1.1 时代System.Web.Mail.MailMessage类.Sys ...

  4. 利用System.Net.Mail 发送邮件

    我这里只是试了一下发mail的功能,感觉.net自带的发mail是比较全的,还是直接上我的code 参数文章:System.Net.Mail 发送邮件 SMTP协议 using System; usi ...

  5. C# System.Web.Mail.MailMessage 发邮件

    C# System.Web.Mail.MailMessage 发邮件 新建控制台Console项目,然后添加 System.Web引用 代码如下: using System; using System ...

  6. C#使用 System.Net.Mail发送邮件功能

    .NET 里包含了很多很丰富的邮件发送与接受的API在 System.Net.Mail命名空间里,使得我们开发发送和接受邮件相关功能变得简单,下面是一个简单发送邮件的功能: private void ...

  7. 使用System.Net.Mail发送邮件

    引用命名空间: using System.Net.Mail; /// <summary> /// 发送HTML邮件,有抄送和密送 /// 需要在Web.config文件中的system.n ...

  8. echarts将图表Y坐标刻度设置成只显示整数

    echarts的配置项中没有直接将坐标刻度强制设为整数的选项,但可以通过minInterval属性将刻度以整数形式显示,在配置项的yAxis对象中添加属性: minInterval: 1 表示将刻度的 ...

  9. 转 EasyUi日期控件datebox设置,只显示年月,也只能选择年月

    1.引入Jquery和easyui,注低版本的Jquery和easy不能使用,这里使用的Jquery是1.8.2easyui是1.6.1.1.easyui下载地址:http://www.jeasyui ...

随机推荐

  1. opencv-2-VS2017与QT显示图像

    opencv-2-VS2017与QT显示图像 opencvqtVSC++ 目的 使用 VS 构建第一个 opencv 程序 使用 QT 构建 第一个 opencv 程序 VS 导入 QT 程序 开始 ...

  2. Centos下安装Docker,并配置国内docker源

    Centos下安装Docker 替换为国内的yum源(可选): mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.re ...

  3. 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)

    最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...

  4. 2019 ICPC 南京网络赛 H-Holy Grail

    As the current heir of a wizarding family with a long history,unfortunately, you find yourself force ...

  5. 用Navicat建MySQL数据库表,动态改变创建时间和更新时间戳

    1.create_time 记录创建的时间,设默认值为:CURRENT_TIMESATMP 注意:不勾选那个[根据当前时间戳更新] 2.operator_time 更新记录的时间,勾选那个[根据当前时 ...

  6. Maxim实时时钟芯片设计指南5413-二进制编码十进制(BCD)格式实时时钟中的状态机逻辑

    网上DS12C887的文章涉及到时间的存储格式使用的都是二进制代码,究竟使用BCD码该如何操作?正好美信官网上有一篇文章.美信官网不稳定,先贴到这里,有时间再翻译. 原文链接 State Machin ...

  7. 中文分词工具简介与安装教程(jieba、nlpir、hanlp、pkuseg、foolnltk、snownlp、thulac)

    2.1 jieba 2.1.1 jieba简介 Jieba中文含义结巴,jieba库是目前做的最好的python分词组件.首先它的安装十分便捷,只需要使用pip安装:其次,它不需要另外下载其它的数据包 ...

  8. Web快速输入标签

    在书写web代码的时候,掌握一些快捷输入方式不仅可以提高效率,还能省不少力气. 1. > :下一个子标签,如 div>p 加Tab达到: <div><p></ ...

  9. 【漫画】JAVA并发编程 J.U.C Lock包之ReentrantLock互斥锁

    在如何解决原子性问题的最后,我们卖了个关子,互斥锁不仅仅只有synchronized关键字,还可以用什么来实现呢? J.U.C包中还提供了一个叫做Locks的包,我好歹英语过了四级,听名字我就能马上大 ...

  10. Keycloak & Asp.net core webapi 整合跳坑之旅

    前言 之前,一直使用IdentityServer4作为.net core程序的外部身份认证程序,ID4的优点自不必说了,缺点就是缺乏完善的管理界面. 后来,学习java quarkus框架时,偶然遇到 ...