C# 邮件发送和接收实现代码

黄舟
发布: 2016-12-22 13:49:13
原创
2509人浏览过

邮件发送 
方法一:使用system.web.mail命名空#region 发送邮件:此方法失败 

protected void SendFailed() 
{ 
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); 
mail.From = "test@ gmail.com"; 
mail.To = " test@ gmail.com "; 
mail.Subject = "For Test"; 
mail.Priority = System.Web.Mail.MailPriority.Normal; 
mail.BodyEncoding = Encoding.Default; 
mail.BodyFormat = MailFormat.Html; 
mail.Body = "this is a Email!<input type='button' value='ok'/>"; 
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication 
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "test"); //set your username here 
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "****"); //set your password here 
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com"); 
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "587"); 
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); 
SmtpMail.SmtpServer = "smtp.gmail.com"; 
SmtpMail.Send(mail); 
} 
#endregion间(此方法我测试没有成功过)
登录后复制

方法二:使用System.Net.Mail命名空间(此方法测试成功) 
我使用的gmail的邮箱,以及他提供免费smtp服务,之前试了好几个邮箱都不成功。Gmail的smtp服务必须经过ssl加密,才可以验证成功。 

#region 发送邮件:此方法可行 
protected void SendSuccess() 
{ 
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); 
message.From = new MailAddress("test@gmail.com", "someone");//必须是提供smtp服务的邮件服务器 
message.To.Add(new MailAddress("test@yahoo.com.cn")); 
message.Subject = "测试邮件" ; 
message.CC.Add(new MailAddress("test@126.com")); 
message.Bcc.Add(new MailAddress("test@126.com")); 
message.IsBodyHtml = true; 
message.BodyEncoding = System.Text.Encoding.UTF8; 
message.Body = "邮件发送测试"; 
message.Priority = System.Net.Mail.MailPriority.High; 
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); // 587;//Gmail使用的端口 
client.Credentials = new System.Net.NetworkCredential("test@gmail.com", "password"); //这里是申请的邮箱和密码 
client.EnableSsl = true; //必须经过ssl加密 
try 
{ 
client.Send(message); 
Response.Write("邮件已经成功发送到" + message.To.ToString() + "<br>"); 
} 
catch (Exception ee) 
{ 
Response.Write(ee.Message + "<br>" /* + ee.InnerException.Message*/ ); 
} 
} 
#endregion
登录后复制

邮件接收 
我使用的是LumiSoft.Net这个开源的项目,也是从一个网友哪里看到的下载地址,然后自己看了下代码,写了个简单的接收方法。首先将代码中relrease目录下的dll文件引用到项目中。 

using LumiSoft.Net.POP3.Client; 
using LumiSoft.Net.Mail; 
…… 
public IList<Mail_Message> ReceiveMail() 
{ 
IList<Mail_Message> mailList = new List<Mail_Message>(); 
using (POP3_Client client = new POP3_Client()) 
{ 
client.Connect("pop.gmail.com",995,true); 
client.Authenticate("zw.seaman", "zw_seaman", false); 
POP3_ClientMessageCollection coll = client.Messages; 
for (int i = 0; i < coll.Count; i++) 
{ 
POP3_ClientMessage message = coll[i]; 
Mail_Message mm = Mail_Message.ParseFromByte(coll[i].MessageToByte()); 
mailList.Add(mm); 
} 
} 
return mailList; 
} 
protected void Page_Load(object sender, EventArgs e) 
{ 
IList<Mail_Message> mailList = new ZMail.Mail().ReceiveMail(); 
foreach (Mail_Message mail in mailList) 
{ 
StringBuilder sb = new StringBuilder(); 
sb.Append(mail.From.ToString()).Append("  发送给  "); 
sb.Append(mail.To.ToString()).Append("<br/>") ; 
sb.Append(mail.Subject).Append("<br/>"); 
sb.Append(mail.BodyHtmlText).Append("<hr/>"); 
Response.Write(sb.ToString()); 
} 
}
登录后复制

 以上就是C# 邮件发送和接收实现代码的内容,更多相关内容请关注PHP中文网(www.php.cn)!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号