C # Mail send and receive implementation code

Source: Internet
Author: User
Tags mailmessage smtpclient
Send mail
Method One: Use System.Web.Mail to name empty #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间 (This method I have not successfully tested)

Method Two: Use the System.Net.Mail namespace (this method tests successfully)
I use Gmail's mailbox, and he provides a free SMTP service, before trying several mailboxes are unsuccessful. Gmail's SMTP service must be SSL-encrypted before it can be verified successfully.

#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 a mail server message that provides the SMTP service. 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 sending test"; Message. priority = System.Net.Mail.MailPriority.High; SmtpClient client = new SmtpClient ("smtp.gmail.com", 587); The port client used by the 587;//gmail. Credentials = new System.Net.NetworkCredential ("test@gmail.com", "password"); Here is the application email and password client. Enablessl = true; Must be encrypted by SSL try {client. Send (message); Response.Write ("The message has been successfully sent to" + message.) To.tostring () + "<br>"); } catch (Exception ee) {Response.Write (EE. Message + "<br>"/* + EE. innerexception.message*/); }} #endregion

Mail receive
I use lumisoft.net this open-source project, but also from a netizen where to see the download address, and then read the code himself, wrote a simple way to receive. Start by referencing the DLL files in your code in the Relrease directory into your 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 ("Sent to"); Sb. Append (mail. To.tostring ()). Append ("<br/>"); Sb. Append (mail. Subject). Append ("<br/>"); Sb. Append (mail. Bodyhtmltext). Append ("

The above is the C # Mail send and receive implementation code content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.