WebForm sending mail

Source: Internet
Author: User
Tags mailmessage smtpclient

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Net.Mail;usingSystem.Configuration;usingSystem.Net;usingSystem.Reflection;usingSystem.Text;namespacesendmailmessage.uitls{ Public classEMail {//instantiate a message classMailMessage _mailmessage =NewMailMessage (); /// <summary>        ///E -mail address used by the sender/// </summary>        Private string_fromemail = configurationmanager.appsettings["Emailsender"].        ToString (); /// <summary>        ///e-mail address password/// </summary>        Private string_senderpassword = configurationmanager.appsettings["emailserver_pwd"].        ToString (); /// <summary>        ///The name of the sender that appears on the message/// </summary>        Private string_displayname = configurationmanager.appsettings["Emailsendername"].        ToString (); /// <summary>        ///the server that the outgoing mailbox uses/// </summary>        Private string_SMTP = configurationmanager.appsettings["Emailserver"].        ToString (); /// <summary>        ///recipient of the message/// </summary>        Private stringToemail {Set;Get; } /// <summary>        ///cc/// </summary>        Private stringCC {Set;Get; } /// <summary>        ///Secret Delivery/// </summary>        Private stringBCC {Set;Get; } /// <summary>        ///message Header/// </summary>        Private stringSubject {Set;Get; } /// <summary>        ///message body/// </summary>        Private stringBody {Set;Get; } /// <summary>        ///Accessories/// </summary>        Private stringAttachments {Set;Get; }  PublicEMail (stringToemail,stringSubjectstringbody) {Toemail=Toemail; Subject=subject; Body=body; }         PublicEMail (stringToemail,stringCcstringBccstringSubjectstringBodystringattachments) {CC=cc; BCC=bcc; Toemail=Toemail; Subject=subject; Body=body; Attachments=attachments; }        /// <summary>        ///Send email/// </summary>        /// <param name= "Toemail" >message recipients, support mass, multiple addresses separated by commas</param>            /// <param name= "Subject" >Theme</param>        /// <param name= "Body" >Body</param>             Public Static BOOLSendEmail (stringToemail,stringSubjectstringbody) {Email email=NewEMail (toemail, subject, body); returnemail.        Send (); }                /// <summary>        ///Send email/// </summary>        /// <param name= "Toemail" >message recipients, support mass, multiple addresses separated by commas</param>        /// <param name= "CC" >cc, separated by half-width commas between multiple addresses</param>        /// <param name= "Bcc" >bcc, separated by a comma with half-width between multiple addresses</param>        /// <param name= "Subject" >Theme</param>        /// <param name= "Body" >Body</param>        /// <param name= "Attachments" >Attachment Address</param>         Public Static BOOLSendEmail (stringToemail,stringCcstringBccstringSubjectstringBodystringattachments) {Email email=NewEMail (Toemail, CC, BCC, subject, body, attachments); returnemail.        Send (); }        /// <summary>        ///Send mail/// </summary>         Public BOOLSend () {Type type=typeof(SmtpClient); SmtpClient _smtpclient=NewSmtpClient (); BOOLresult =false; Try            {                //set the outbound mode of SMTP to network_smtpclient.deliverymethod =smtpdeliverymethod.network; //whether the SMTP server enables SSL encryption_smtpclient.enablessl =false; //Specify the SMTP server address_smtpclient.host =_smtp; //specifies the port of the SMTP server, which defaults to 25, and if the default port is used, save//SMTP.                             Port = 25; //If your SMTP server does not require authentication, use the following method, but there is no need for authentication at this time.//SMTP.                useDefaultCredentials = true; //If authentication is required, use the following method_smtpclient.credentials =NewNetworkCredential (_fromemail, _senderpassword); //the priority of the message is divided into low, normal, and high, usually with normal ._mailmessage.priority =Mailpriority.high; //the recipient sees the source of the message; The first parameter is the sender's e-mail address, the second parameter is the name of the sender, the third parameter is the encoding used by the second parameter, if the specified is not correct, the other party received after the display garbled, 936 is the Simplified Chinese codepage value_mailmessage.from =NewMailAddress (_fromemail, _displayname, encoding.getencoding (936)); //ReplyTo means the default receiving address when replying to a message, that is, you use one mailbox to send a letter, but use another to receive it, the meaning of the last two parameters, the same from_mailmessage.replyto =NewMailAddress (_fromemail, _displayname, encoding.getencoding (936)); //message recipients, support mass, multiple addresses separated by commas_mailmessage.to.add (Toemail); //cc                if(!string.                IsNullOrEmpty (cc)) {_mailmessage.cc.add (cc); }                //Secret Delivery                if(!string.                IsNullOrEmpty (BCC)) {_mailmessage.bcc.add (BCC); }                //message Header_mailmessage.subject =Subject; //here is very important, if your mail title contains Chinese, it must be specified here, otherwise the other party received the most likely is garbled_mailmessage.subjectencoding = encoding.getencoding (936); //whether the message body is in HTML format_mailmessage.isbodyhtml =true; //message body encoding, incorrect settings, receiver will receive garbled_mailmessage.bodyencoding = encoding.getencoding (936); //message body_mailmessage.body =Body; //Accessories                if(!string. IsNullOrEmpty (Attachments)) {string[] Attachmentsarray = Attachments.split (',');  for(inti =0; i < attachmentsarray.length; i++) {_mailmessage.attachments.add (NewAttachment (Attachmentsarray[i])); }                }                //Send mail_smtpclient.send (_mailmessage); Result=true; }            Catch(Exception ex) {result=false; }            returnresult; }     }    }

WebForm sending 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.