C # Send mail

Source: Internet
Author: User
Tags mailmessage smtpclient

Email class

  Public classEmail {/// <summary>        ///sent by/// </summary>         Public stringMailfrom {Get;Set; } /// <summary>        ///Recipient/// </summary>         Public string[] Mailtoarray {Get;Set; } /// <summary>        ///cc/// </summary>         Public string[] Mailccarray {Get;Set; } /// <summary>        ///title/// </summary>         Public stringMailsubject {Get;Set; } /// <summary>        ///Body/// </summary>         Public stringMailbody {Get;Set; } /// <summary>        ///Sender Password/// </summary>         Public stringmailpwd {Get;Set; } /// <summary>        ///SMTP mail server/// </summary>         Public stringHost {Get;Set; } /// <summary>        ///whether the body is in HTML format/// </summary>         Public BOOLisbodyhtml {Get;Set; } /// <summary>        ///Accessories/// </summary>         Public string[] Attachmentspath {Get;Set; }  Public BOOLSend () {//Initializes a mailaddress instance with the specified mail addressMailAddress maddr =Newmailaddress (Mailfrom); //initializing an MailMessage instanceMailMessage MyMail =NewMailMessage (); //Add a mail address to a collection of recipient addresses            if(Mailtoarray! =NULL)            {                 for(inti =0; i < mailtoarray.length; i++) {myMail.To.Add (Mailtoarray[i].                ToString ()); }            }            //Add an email address to the CC recipient address Collection            if(Mailccarray! =NULL)            {                 for(inti =0; i < mailccarray.length; i++) {myMail.CC.Add (Mailccarray[i].                ToString ()); }            }            //Sender AddressMymail.from =maddr; //the title of the e-mailMymail.subject =Mailsubject; //the encoding used for the subject content of the e-mailMymail.subjectencoding =Encoding.UTF8; //Email BodyMymail.body =Mailbody; //encoding of the body of the e-mailMymail.bodyencoding =Encoding.default; Mymail.priority=Mailpriority.high; Mymail.isbodyhtml=isbodyhtml; //Add an attachment with an attachment            Try            {                if(Attachmentspath! =NULL&& attachmentspath.length >0) {Attachment attachfile=NULL; foreach(stringPathinchAttachmentspath) {Attachfile=NewAttachment (path);                    MYMAIL.ATTACHMENTS.ADD (Attachfile); }                }            }            Catch(Exception err) {Throw NewException ("there was an error when adding an attachment:"+err); } smtpclient SMTP=NewSmtpClient (); //setting up an SMTP mail serverSmtp. Host =host; //SMTP. Enablessl = true;Smtp. Port = -; //SMTP.              useDefaultCredentials = true; //Specify the sender's e-mail address and password to verify sender identitySmtp. Credentials =NewSystem.Net.NetworkCredential (Mailfrom, mailpwd); Try            {                //send a message to an SMTP mail serverSMTP.                Send (MyMail); return true; }            Catch(System.Net.Mail.SmtpException ex) {return false; }        }    }

Send code

 Static voidMain (string[] args) {Email email=NewEmail (); Email.mailfrom="[email protected]"; Email.mailpwd="jie0628"; Email.mailsubject="Programmer's Life"; Email.mailbody="a lot of programmers actually very hard one thing"; Email.isbodyhtml=true; Email.host="smtp.qq.com"; Email.mailtoarray=New string[] {"[email protected]" }; //Email.mailtoarray = new string[] {"[email protected]", "[Email protected]"}; //Email.mailccarray = new string[] {"[Email protected]"};Email.attachmentspath =New string[] { }; if(email.) Send ()) {Console.WriteLine ("message sent successfully ~");            Console.readkey (); }            Else{Console.WriteLine ("Message Delivery Failed ~");                        Console.readkey (); }        }

C # Send mail

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.