C # Mail send and receive implementation code _c# tutorial

Source: Internet
Author: User
Tags mailmessage smtpclient
Send mail
method One: Use the System.Web.Mail namespace (this method I test did not succeed)
Copy Code code as follows:

#region Send message: This method fails
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 Two: Use the System.Net.Mail namespace (this method tests successfully)
I use Gmail's mailbox, and he provides free SMTP service, before trying several mailboxes are unsuccessful. Gmail's SMTP service must be SSL-encrypted before it can be verified successfully.
Copy Code code as follows:

#region Send mail: This method works
protected void sendsuccess ()
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage ();
Message. from = new MailAddress ("test@gmail.com", "someone");//Must be the mail server that provides the SMTP service
Message. To.add (New MailAddress ("test@yahoo.com.cn"));
Message. Subject = "Test Mail";
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 = "Mail send Test";
Message. Priority = System.Net.Mail.MailPriority.High;
SmtpClient client = new SmtpClient ("smtp.gmail.com", 587); Ports Used by 587;//gmail
Client. Credentials = new System.Net.NetworkCredential ("test@gmail.com", "password"); This is the mailbox and password for the application.
Client. Enablessl = true; Must be encrypted by SSL
Try
{
Client. Send (message);
Response.Write ("Mail has been successfully sent to" + message.) To.tostring () + "<br>");
}
catch (Exception ee)
{
Response.Write (EE. Message + "<br>"/* + EE. innerexception.message*/);
}
}
#endregion

Mail Receive
I am using lumisoft.net this open source project, also from a netizen where to see the download address, and then look at the code, wrote a simple receive method. First, reference the DLL file in the Relrease directory in your code to your project.
Copy Code code as follows:

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 ("Sent");
Sb. Append (mail. To.tostring ()). Append ("<br/>");
Sb. Append (mail. Subject). Append ("<br/>");
Sb. Append (mail. Bodyhtmltext). Append ("Response.Write (sb.) ToString ());
}
}

These two methods are easy to understand, with only the most basic functionality, and you can view the source code for more information if you need to.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.