Home  >  Article  >  Backend Development  >  C# email sending and receiving implementation code

C# email sending and receiving implementation code

黄舟
黄舟Original
2016-12-22 13:49:132275browse

Sending emails
Method 1: Use System.Web.Mail namespace #region to send emails: This method failed

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=&#39;button&#39; value=&#39;ok&#39;/>"; 
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间(此方法我测试没有成功过)

Method 2: Use System.Net.Mail namespace (this method was tested successfully)
I use gmail Email, and it provides free SMTP service. I tried several emails before but failed. Gmail's SMTP service must be SSL encrypted before it can be successfully verified.

#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

Email Reception
I use LumiSoft.Net, an open source project, and I saw the download address from a netizen. Then I read the code myself and wrote a simple receiving method. First, reference the dll file in the relrease directory in the code into the project.

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()); 
} 
}

The above is the content of the C# email sending and receiving implementation code. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn