Home Backend Development C#.Net Tutorial C# email sending and receiving implementation code

C# email sending and receiving implementation code

Dec 22, 2016 pm 01:49 PM

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间(此方法我测试没有成功过)
Copy after login

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
Copy after login

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

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

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

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

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

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

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

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

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.

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

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

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

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.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

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

C# StringWriter C# StringWriter Sep 03, 2024 pm 03:23 PM

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

See all articles