C # Send mail _emailhelper

Source: Internet
Author: User

Emailhelper class
 public class Emailhelper {//<summary>//E-mail//</summary>//<param Name= "smtpserver" &GT;SMTP server </param>///<param Name= "Port" > Port </param>///<param Name = "Mailfrom" > Sender mailbox </param>//<param name= "UserPassword" > Password </param>//<param name= "MailTo" > Recipient </param>//<param name= "STRCC" > CC person </param>//<param name= "STRBCC" , BCC </param>//<param name= "Mailsubject" > Mail subject </param>//<param name= "Mailcontent" Content </param>//<param name= "STRs" > Accessories </param>///<returns> Send successful return true otherwise False</ret urns> public static bool SendEmail (string smtpserver, int port, string mailfrom, String UserPassword, String mai                LTo, String strcc,string strbcc,string mailsubject, String mailcontent, String STRs) {try { Set the sender'sMail message//Mail Service settings SmtpClient smtpclient = new SmtpClient (); Smtpclient.deliverymethod = smtpdeliverymethod.network;//Specifies how e-mail is sent smtpclient.host = SmtpServer; Specify SMTP Server Smtpclient.port = port;//Port smtpclient.credentials = new System.Net.NetworkCrede Ntial (Mailfrom, UserPassword);//verify user name and password Smtpclient.enablessl = true; Use SSL//Send mail settings mailmessage mailmessage = new MailMessage (Mailfrom, mailTo);                Sender and Recipient Mailmessage.subject = mailsubject;//subject Mailmessage.body = mailcontent;//content                mailmessage.bodyencoding = encoding.utf8;//Body Encoding mailmessage.isbodyhtml = true;//set to HTML format Mailmessage.priority = mailpriority.normal;//Priority//CC person if (!string.                IsNullOrEmpty (STRCC)) mailMessage.CC.Add (STRCC); Secret Send if (!striNg.                 IsNullOrEmpty (STRBCC)) mailMessage.Bcc.Add (STRBCC); Attachment if (!string.                    IsNullOrEmpty (STRs)) {list<string> paths = new list<string> (); if (STRs. Contains (",")) {paths = STRs. Split (', ').                    ToList (); } else {paths.                    ADD (STRs); } foreach (var path in paths) {MAILMESSAGE.ATTACHMENTS.ADD (n                    EW Attachment (STRs)); }} smtpclient.send (MailMessage);            Send message return true;            } catch {return false; }        }    }

  

Call the method that sent the message:

 Static voidMain (string[] args) {            //get various parameters, not required with an empty string            stringPath =directory.getcurrentdirectory (); stringFile = path +@"\excel.xlsx";//Annex 1            stringFile2 = path +@"\excel2.xlsx";//Annex 2            stringSmtpServer ="smtp.163.com";//163 SMTP server for mailbox            intPort = -;//Port            stringMailfrom ="******@163.com";//Sender Mailbox            stringPWD ="*********";//Password            stringMailTo ="[email protected],[email protected]";//recipient mailbox, multiple users separated by commas            stringMAILCC ="";//cc person, multiple users separated by commas            stringMAILBCC ="";//Secret Delivery            stringMailsubject ="Test Message";//Theme            stringMailcontent ="HI, this is a test email I sent you.";//content            stringAh = file+","+file2;//Attachments-File path            if(Emailhelper.sendemail (SmtpServer, Port, Mailfrom, PWD, MailTo, MAILCC, MAILBCC, Mailsubject, mailcontent, ah) = =true) {Console.WriteLine ("sent successfully!"); }            ElseConsole.WriteLine ("Send failed");        Console.readkey (); }

Attention:

    • Confirm that the sender's mailbox has been opened SMTP;
    • The SMTP of the corresponding mailbox is filled out correctly, for example: 126 e-mail: [Email protected]
    • Whether to use SSL encrypted connection in the corresponding mailbox SendEmail

C # Send mail _emailhelper

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.