Send mail using SMTP

Source: Internet
Author: User

 // <summary>        ///Send mail/// </summary>        /// <param name= "Mailservername" >Server</param>        /// <param name= "Pubmail" >Public Email Address</param>        /// <param name= "Mailusername" >User name</param>        /// <param name= "MailPassword" >Password</param>        /// <param name= "to" >Recipient</param>        /// <param name= "CC" >cc Person</param>        /// <param name= "Mailhistoryguid" >message record GUID, getting attachments</param>        /// <param name= "Mailbody" >message body</param>        /// <param name= "Subject" >Theme</param>        /// <returns></returns>        Private intSendmessage_internal (stringMailservername,stringPubmail,stringMailusername,stringMailPassword,stringTo,stringCcstringMailhistoryguid,stringMailbody,stringSubject) {            //Simple Mail Transfer Protocol classSmtpClient client =NewSmtpClient (); //e-mail message classMailMessage MailMessage =NewMailMessage (); intIssucess =0; Try{client. Host= Mailservername;//setting up a mail serverClient. Port = -;//SMTP Host port number, defaultClient. Deliverymethod =smtpdeliverymethod.network; Client. Credentials=NewSystem.Net.NetworkCredential (Pubmail, MailPassword); Mailmessage.sender=NewMailAddress (Pubmail, mailusername);//Sender Information//Set SenderMailmessage.from =Newmailaddress (Pubmail, mailusername); //set up a pickupDataTable dttemp=getmailtolist (); intCount =DtTemp.Rows.Count; if(Count >0)                {                     for(inti =0; I < count; i++) {MAILMESSAGE.TO.ADD (NewMailAddress (dttemp.rows[i]["Email"]. ToString (), dttemp.rows[i]["UserName"].                    ToString ())); }                }                //Set cc people                if(!string. IsNullOrEmpty (CC)) {dttemp=getccuserlist (); Count=DtTemp.Rows.Count; if(Count >0)                    {                         for(inti =0; I < count; i++) {MAILMESSAGE.CC.ADD (NewMailAddress (dttemp.rows[i]["Email"]. ToString (), dttemp.rows[i]["UserName"].                        ToString ())); }                    }                }                //Add an attachmentdttemp=getattachdocuments (); Count=DtTemp.Rows.Count; if(Count >0)                {                     for(inti =0; I < count; i++) {MAILMESSAGE.ATTACHMENTS.ADD (NewAttachment (HttpContext.Current.Server.MapPath ("/") + dttemp.rows[i][" Location"] + dttemp.rows[i]["FileName"].                    ToString ())); }                }                //ThemeMailmessage.subject =Subject; Mailmessage.body=Mailbody; Mailmessage.subjectencoding= System.Text.Encoding.UTF8;//Message Subject Codemailmessage.bodyencoding = System.Text.Encoding.GetEncoding ("GB2312");//Message content Encodingmailmessage.isbodyhtml =true;//whether the message content is in HTML formatclient.                Send (MailMessage); Issucess=1; }            Catch(Exception e) {mydb.logexception (e); Issucess=0; }            finally{mailmessage.dispose (); }            returnissucess; }     }

Send mail using SMTP

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.