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. 获取 ProgramData 之类的环境变量(文件夹路径)的值

    GetEnvironmentVariable Recognized Environment Variables https://docs.microsoft.com/en-us/windows/dep ...

  2. js的属性监听

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8">     <title ...

  3. 更改 vsftpd 的端口号

    2019独角兽企业重金招聘Python工程师标准>>> vsftpd启动后,默认的ftp端口是21,现在我想把ftp端口改成 801 ,修改后能保证用户上传下载不受影响 1.编辑 / ...

  4. 打造livecd的注意事项

    一:在CentOS.ks的定制脚本中,删除syslinux组件:出错提示: /usr/lib/python2.6/site-packages/imgcreate/errors.py:45: Depre ...

  5. Codeforce 1102 C. Doors Breaking and Repairing

    Descirbe You are policeman and you are playing a game with Slavik. The game is turn-based and each t ...

  6. CF--思维练习--CodeForces - 220C Little Elephant and Shifts (STL模拟)

    ACM思维题训练集合 The Little Elephant has two permutations a and b of length n, consisting of numbers from ...

  7. thinkphp5.x系列 RCE总结

    Thinkphp  MVC开发模式 执行流程: 首先发起请求->开始路由检测->获取pathinfo信息->路由匹配->开始路由解析->获得模块.控制器.操作方法调度信息 ...

  8. 支付宝小程序serverless---插入数据后获取数据的主键_id(mongodb)

    支付宝小程序serverless---插入数据后获取数据的主键_id(mongodb) 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除, ...

  9. dp (1)

    D - Tree of Tree ZOJ - 3201 这个题目我开始是这么定义的dp[i][j][0] dp[i][j][1] 表示对于第i个节点还有j个的选择 0 代表不选这个节点,1 代表选这个 ...

  10. 自定义比较器(IComparer接口的实现)

    class FileNameSort : IComparer { [System.Runtime.InteropServices.DllImport("Shlwapi.dll", ...