C# 邮件发送与接收实现代码
邮件发送功能实现
第一种方案尝试使用 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!"
";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() + "
");
}
catch (Exception ee)
{
Response.Write(ee.Message + "
" /* + ee.InnerException.Message*/ );
}
}
#endregion
邮件接收功能实现
接收模块基于 LumiSoft.Net 开源项目开发,该方案源自网友分享并经过自行代码研读后编写而成。首先需将项目中 release 目录下的 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());
}
}
上述两种方法逻辑清晰,仅实现了基础功能。如需更复杂特性,建议查阅源代码以获取更多信息。