【转】C#发送Email邮件_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 【转】C#发送Email邮件

【转】C#发送Email邮件

 2014/12/9 16:35:26  h82258652  程序员俱乐部  我要评论(0)
  • 摘要:转自:http://hi.baidu.com/bluesky_cn/item/8bb060ace834c53f020a4df2下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号。需要引用:1usingSystem.Net.Mail;2usingSystem.Text;3usingSystem.Net;程序代码:1MailMessagemyMail=newMailMessage();//创建邮件实例对象2myMail.From=newMailAddress("");//发送者
  • 标签:C# 邮件

转自:http://hi.baidu.com/bluesky_cn/item/8bb060ace834c53f020a4df2

下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号。

需要引用:

1 using System.Net.Mail;
2 using System.Text;
3 using System.Net;

程序代码:

 1         MailMessage myMail = new MailMessage(); //创建邮件实例对象
 2         myMail.From = new MailAddress(""); //发送者,要和邮件服务器的验证信息对应,不能随便更改
 3         
 4         myMail.To.Add(new MailAddress("")); //接收者
 5         myMail.Subject = "C#发送Email"; //邮件标题
 6         myMail.SubjectEncoding = Encoding.UTF8; //标题编码
 7         myMail.Body = "this is a test email!"; //邮件内容
 8         myMail.BodyEncoding = Encoding.UTF8; //邮件内容编码
 9         myMail.IsBodyHtml = true;     //邮件内容是否支持html
10         SmtpClient smtp = new SmtpClient(); //创建smtp实例对象
11         smtp.Host = "mail.sina.com"; //邮件服务器SMTP
12         smtp.Port = 25;                  //邮件服务器端口
13         smtp.Credentials = new NetworkCredential("", "123456"); //邮件服务器验证信息
14         smtp.Send(myMail); //发送邮件

使用Gmail邮箱发送邮件示例

 1         MailMessage myMail = new MailMessage();
 2         myMail.From = new MailAddress("");
 3         myMail.To.Add(new MailAddress(""));
 4         myMail.Subject = "C#发送Email";
 5         myMail.SubjectEncoding = Encoding.UTF8;
 6         myMail.Body = "this is a test email from gmail!<a href='http://www.sina.com.cn'>sina</a>";
 7         myMail.BodyEncoding = Encoding.UTF8;
 8         myMail.IsBodyHtml = true;
 9         SmtpClient smtp = new SmtpClient();
10         smtp.Host = "smtp.gmail.com";
11         smtp.Port = 587; //Gmail的smtp端口
12         smtp.Credentials = new NetworkCredential("", "123456");
13         smtp.EnableSsl = true; //Gmail要求SSL连接
14         smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //Gmail的发送方式是通过网络的方式,需要指定
15         smtp.Send(myMail);

使用QQ邮箱发送邮件示例

 1         MailMessage myMail = new MailMessage();
 2         myMail.From = new MailAddress("");
 3         myMail.To.Add(new MailAddress(""));
 4         myMail.Subject = "C#发送Email";
 5         myMail.SubjectEncoding = Encoding.UTF8;
 6         myMail.Body = "this is a test email from QQ!";
 7         myMail.BodyEncoding = Encoding.UTF8;
 8         myMail.IsBodyHtml = true;
 9         SmtpClient smtp = new SmtpClient();
10         smtp.Host = "smtp.qq.com";
11         smtp.Credentials = new NetworkCredential("", "123456");
12         smtp.Send(myMail);

 

发表评论
用户名: 匿名