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.
Common Properties:
From--Address of sending mail
To-accept the address of the message
Subject--The title of the message
Priority--Priority of the message (valid value is High,low,normal)
Attachments--Returns a collection representing the attachment
BCC--Secret address
CC--CC address
Body--Gets or sets the content of an e-mail message
BodyFormat-Gets or sets the MailFormat enumeration value that specifies the format of the message body message (HTML format, text format)
Bodyencoding--Specifies how the message is encoded (mainly Base64,uuencode)
Urlcontentbase: URL encoding in an HTML-formatted message
Urlcontentlocation: Priority of message information (high, Medium,low)
SmtpMail
The SMTP protocol that is responsible for sending messages, providing properties and methods to send mail messages by using the Federated data object of 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 of Mail Accepted", "title of Message", "Content of mail Message") This method is very simple, not suitable for sending 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 message attachments, which are used primarily to provide properties and methods to create a message attachment object.
Constructors
Create an Attachment object
MailAttachment objmailattachment = new MailAttachment ("D:\test. TXT ");/send an attachment to a message
Call form
The code is as follows |
Copy Code |
MailMessage objmailmessage= New MailMessage (); OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attach attachment to mail message object |
Encapsulated message Sending class
C # code replication
The code is as follows |
Copy Code |
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 string Host ... { Get ... {return _host;} Set ... {_host = value;} } private int _port; /**////<summary> Server port </summary> public int Port ... { Get ... {return _port;} Set ... {_port = value;} } private string _smtpusername; /**////<summary> Sender Mailbox User Name </summary> public string Smtpusername ... { Get ... {return _smtpusername;} Set ... {_smtpusername = value;} } private string _sendemail; /**////<summary> ///Sender's mailbox account (only receive encrypted string, decrypt when sending mail) ///</summary> public string SendEmail ... { get ... { return _sendemail; } Set ... {_sendemail = value;} } private string _replytoemail; /**////<summary> Reply Person e-mail account </summary> public string Replytoemail ... { Get ... {return _replytoemail;} Set ... {_replytoemail = value;} } private string _replyusername; /**////<summary> Reply Person User name </summary> public string Replyusername ... { Get ... {return _replyusername;} Set ... {_replyusername = value;} } private string _getemail; /**////<summary> Recipient mailbox Account </summary> public string Getemail ... { Get ... {return _getemail;} Set ... {_getemail = value;} } private string _smtppassword; /**////<summary> Sender's mailbox password (only receive encrypted string, decrypt when sending mail) </summary> public string Smtppassword ... { Get ... {return _smtppassword;} Set ... {_smtppassword = value;} } private string _content; /**////<summary> Message content </summary> public string Content ... { Get ... {return _content;} Set ... {_content = value;} } private string _title; /**////<summary> Message headers </summary> public string Title ... { Get ... {return _title;} Set ... {_title = value;} } Private string[] _cc = null; /**////<summary> CC E-Mail </summary> Public string[] CC ... { Get ... {return _cc;} Set ... {_cc = value;} } Private string[] _bcc = null; /**////<summary> The Secret Send mailbox </summary> Public string[] Bcc ... { Get ... {return _BCC;} Set ... {_bcc = value;} } /**////<summary> Send mail </summary> <returns> return Success </returns> public bool Send () ... { Try ... { MailMessage objmailmessage; Objmailmessage = new MailMessage (SendEmail, _getemail, _title, _content); if (!string. IsNullOrEmpty (_replytoemail) &&!string. IsNullOrEmpty (_replyusername)) ... { mailaddress reply = new MailAddress (_replytoemail, _replyusername); ObjMailMessage.ReplyToList.Add (reply); } objmailmessage.bodyencoding = encoding.getencoding (936); Objmailmessage.isbodyhtml = true; if (CC!= null && cc. Length > 0) ... { foreach (String ccaddress in cc) ... { OBJMAILMESSAGE.CC.ADD (New MailAddress (ccaddress)); } } if (Bcc!= null && bcc. Length > 0) ... { foreach (String bccaddress in Bcc) ... { 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; } } }
|
Calling methods and steps:
1, create an object of the SendMail class, and then send the message to this object the necessary parameters,
2, call Send () method.
The attachment function of the message, you can also expand the SendMail class according to the above introduction. Here is not an example.
To send mail using SMTP from the native SMTP virtual server in asp.net
First, the SMTP configuration.
(1) Right-click "SMTP Virtual Server" Select "Properties"-> on the General tab, set IP address (P), I set 192.168.1.100.
(2) Select "Access" tab, click "Relay", select "only the following list" (the default is selected), click "Add", in "Single computer" add 192.168.1.100.
Tip, if not completed (2), there will be common one of the 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) don't just fill out the core code that uses ASP.net (C #) to send messages is as follows:
The code is as follows |
Copy Code |
/Core Code Start Using System.Web.Mail; MailMessage objmailmessage; MailAttachment objmailattachment; Create an Attachment object Objmailattachment = new MailAttachment ("D:\test.txt");//Send message attachment Create a mail message 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 the title of the message Objmailmessage.body = "Mail Send the content: test whether to send success!" ";/Send the contents of the message OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attach attachment to mail message object 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.