Summary of several methods of asp.net sending mail

Source: Internet
Author: User
Tags foreach decrypt mail email account mailmessage server port smtpclient

There are a lot of ways to send mail in the

 .net, such as mailmessage,smtpmail. Let me use these methods to implement mail in. NET, and I hope this method will be helpful to the alumni.

MailMessage provides properties and methods to create a mail message object. You can usually build your mail program by building the MailMessage object and then setting its properties.   commonly used properties: from--Send the address of the message to--accept the address of the message Subject--the message's title Priority--Message priority (valid value is High,low,normal) Attachments--Returns a collection , representing the attachment Bcc--The BCC address CC--CC address body--Gets or sets the content BodyFormat of the e-mail message--Gets or sets the MailFormat enumeration value that specifies the format of the message body message (HTML format, text format) Bodyen Coding--Specifies the encoding of the message (mainly Base64,uuencode) urlcontentbase: URL encoding in HTML-formatted messages Urlcontentlocation: Priority of message messages (high, Medium,low)   SmtpMail is responsible for the SMTP protocol that sends messages, providing properties and methods to send mail messages by using a federated data object for the message component of Windows CDOSYS. The SmtpMail class uses the Send method, which is designed to send messages with two overloaded methods.   1. Smtpmail.send ("Address of Mail Sent", "Address to accept Mail", "title of Message", "Content of mail Message") This method is simple enough to send messages with attachments.   2. Smtpmail.send (MailMessage) This method is complex, flexible, suitable for sending attachments, and can set various property values for MailMessage objects. If we use ASP.net to write a mail-sent program, then how do we get SMTP first? There are two ways: The first method calls SMTP of the currently well-known mail service providers, such as Sina, Sohu, NetEase's free e-mail SMTP; the second method is to install an SMTP virtual server, which is installed together with IIS.   MailAttachment object classes related to mail attachments, which are used primarily to provide properties and methods to create a message attachment object. The constructor creates an attachment object mailattachment  objmailattachment = new MailAttachment ("D:test. TxT ")//Email attachment call Form   code as follows: MailMessage objmailmessage= new MailMessage (); OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attach attachment to mail message object     encapsulated mail delivery class     code is as follows: using System; Using System.Data; Using System.Configuration; Using System.Web; Using System.Web.Security; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Web.UI.WebControls.WebParts; Using System.Web.UI.HtmlControls; Using System.Net; Using System.Net.Mail; Using System.Text;   public class SendMail ... {    public SendMail ()     ... {   }     private string _host;    /**////<summary>    ///server address    ///</summary>     public stri ng Host     ... {        get ... {return _host;}         Set ... {_host = value;}    }     private int _port;    /**////<summary>    ///server port   &NBSp </summary>     public int Port     ... {        get ... {return _port;}         Set ... {_port = value;}    }     private string _smtpusername;    /**////<summary>    ///senders mailbox username    ///</summary>     public s Tring smtpusername     ... {        get ... {return _smtpusername;}         Set ... {_smtpusername = value;}    }     private string _sendemail;    /**////<summary>    ///Sender email account (only receive encrypted string, decrypt when sending mail)    ///</summary>   & nbsp public string SendEmail     ... {        get         ... {            return _sendemail        }         s Et... {_sendemail = value;}    }     PRIVate string _replytoemail;    /**////<summary>    ///reply person email account    ///</summary>     Public St Ring Replytoemail     ... {        get ... {return _replytoemail;}         Set ... {_replytoemail = value;}    }     private string _replyusername;    /**////<summary>    ///reply person username    ///</summary>     Public str ing replyusername     ... {        get ... {return _replyusername;}         Set ... {_replyusername = value;}    }     private string _getemail;    /**////<summary>    ///recipient mailbox account    ///</summary>     Public St Ring Getemail     ... {        get ... {return _getemail;}         Set ... {_getemail = value;}    } &nbsP   private string _smtppassword;    /**////<summary>    ///sender's email password (only receive encrypted string, decrypt when sending mail)    ///</summary>   & nbsp public string Smtppassword     ... {        get ... {return _smtppassword;}         Set ... {_smtppassword = value;}    }     private string _content;    /**////<summary>    ///mail content    ///</summary>     public Strin G Content     ... {        get ... {return _content;}         Set ... {_content = value;}    }     private string _title;    /**////<summary>    ///Mail title    ///</summary>     public Strin G Title     ... {        get ... {return _title;}         Set ... {_title = value;}    }     PRIVate string[] _cc = null;    /**////<summary>    ///cc mail    ///</summary>     public Strin G[] cc     ... {        get ... {return _cc;}         Set ... {_cc = value;}    }     Private string[] _bcc = null;    /**////<summary>    ///mailbox    ///</summary>     public Strin G[] bcc     ... {        get ... {return _BCC;}         Set ... {_bcc = value;}    }    /**////<summary>    ///send mail    ///</summary>   &nbs P <returns> return success </returns>     public bool Send ()     ... {        try         ... {            MailMessage objmailmessage             Objmailm EssagE = new MailMessage (SendEmail, _getemail, _title, _content);             if (!string. IsNullOrEmpty (_replytoemail) &&!string. IsNullOrEmpty (_replyusername))             ... {                mailaddress reply = new MailAddress (_replytoemail, _replyusernam e);                 OBJMAILMESSAGE.REPLYTOLIST.ADD (reply);                         objmailmessage.bodyencoding = Encodi Ng. GetEncoding (936);             objmailmessage.isbodyhtml = true;             if (cc!= null && cc. Length > 0)             ... {                foreach (string ccaddress in cc)         &NB Sp       ... {          &NBSp         OBJMAILMESSAGE.CC.ADD (new MailAddress (ccaddress));                            }             if (Bcc!= null && bcc. Length > 0)             ... {                foreach (String bccaddress in Bcc)         & nbsp       ... {                    OBJMAILMESSAGE.BCC.ADD (new MailAddress (bccaddress) );                            }             SmtpClient client = new SmtpClient (This._host, This._port);             if (! String.IsNullOrEmpty (this. Smtpusername) &&! String.IsNullOrEmpty (this. Smtppassword))             ... {               client. Credentials = new NetworkCredential (this. Smtpusername, this. Smtppassword);                         client. Enablessl = false;             client. Send (Objmailmessage);             objmailmessage.dispose ();             return true;        }         catch         ... {            return false;         {   }}     &N Bsp Call method and step: 1, create an object of the SendMail class, and then send the object the necessary parameters of the message, 2, call the Send () method.   The attachment function of the mail, you can also expand the SendMail class according to the above introduction. Here is not an example.   Send mail using SMTP for native SMTP virtual server asp.net first of all, the SMTP configuration.           (1) Right-click "SMTP Virtual Server" select "->" to set "IP address (P)" On the General tab, I set 192.168.1.100.           (2) Select the "Access" tab, click "Relay", select "only the following list" (the default is selected), click "Add",Add 192.168.1.100 to the "single computer".             prompt, if not completed (2), there will be a common error prompts: The server rejected one or more recipient addresses. The server response is: 5.7.1 Unable to relay for scucj@126.com (friendship hint: error in the mail address is different) and then start the core code, in fact, and method (a) almost. The main difference with (a) is: 1. SMTP, 2.objmailmessage.from This method can be easily filled in, but (i) not just fill out the asp.net (C #) to send the message of the core code as follows:      code as follows:          //core code start            using system.web.mail;      &NB Sp    mailmessage objmailmessage;           mailattachment objmailattachment;            //Create an Attachment object            objmailattachment = new Maila Ttachment ("D:test.txt")/Send mail attachments            //create mail message           &N Bsp;objmailmessage = new MailMessage ();           objmailmessage.from = "mysina@sina.com" //Source Email address            objmailmessage.to = "scucj@126.com"//Destination email address, which is sent to me ha            objmailmessage.subject = "Mail send title: Hello";/ Send Message title            objmailmessage.body = "Mail Send the content: Test if send success!" ";//email content            objmailmessage.attachments.add (objmailattachment); Attach attachments to mail message objects            //SMTP address             Smtpmail.smtpserver = "192.168.1.100";           //start sending mail            smtpmail.send (Objmailmessage);     The above two methods are introduced here. The easiest way to use the above method is to add a server button to the page and put the statement except the reference in the button click event. Of course, don't forget to put the quoted statement on top.
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.