C# email sending and receiving implementation code
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='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间(此方法我测试没有成功过)
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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to C# StringWriter. Here we discuss a brief overview on C# StringWriter Class and its working along with different Examples and Codes.
