MailMessage
Provides attributes and methods to create an email message object. Generally, you can build a MailMessage object first, and then set its attributes to build a mail program.
Common attributes:
From -- email sending Address
To -- Address of the email recipient
Subject -- mail title
Priority -- mail Priority (valid values: High, Low, Normal)
Attachments -- returns a set that represents an attachment.
Bcc-Bcc address
Cc-Cc address
Body -- get or set the content of the email message
BodyFormat -- gets or sets the enumerated value of MailFormat. This value specifies the message body email format (Html format and Text format)
Bodyencoding -- specify the message encoding method (mainly including Base64 and uencode)
UrlContentBase: URL encoding method in HTML-format mail
UrlContentLocation: Priority of mail Information (High, Medium, Low)
SmtpMail
The SMTP protocol for sending mail. It provides attributes and methods to send mail messages by using the combined data object of the message component of windows 2000 CDOSYS.
The SmtpMail class uses the Send method, which is used to Send emails. There are two overload methods.
1. smtpMail. send ("email sending Address", "email Receiving address", "email title", and "email message content") is very simple, it is not suitable for sending emails with attachments.
2. SmtpMail. Send (MailMessage) This method is complex and flexible. It is suitable for sending attachments, and various attribute values of MailMessage objects can be set. If we use ASP. NET to write a mail sending program, how should we first obtain SMTP. There are two methods: the first method is to call the SMTP of well-known mail service providers, such as Sina, Sohu, and Netease free email; the second method is to install an SMTP virtual server, which is installed together When IIS is installed.
MailAttachment
Object Class Related to email attachments. It is mainly used to provide attributes and methods to create an email attachment object.
Constructor
Create an attachment object
MailAttachment objMailAttachment = new MailAttachment ("d: \ test. Txt "); // The attachment for sending the email
Call form
Copy codeThe Code is as follows:
MailMessage objMailMessage = new MailMessage ();
ObjMailMessage. Attachments. Add (objMailAttachment); // attaches the attachment to the mail message object.
Encapsulated mail sending class
Copy codeThe 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 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's email user name
/// </Summary>
Public string SmtpUsername
...{
Get... {return _ smtpUsername ;}
Set... {_ smtpUsername = value ;}
}
Private string _ sendemail;
/** // <Summary>
/// Sender's email account (only receives encrypted strings and decrypts emails)
/// </Summary>
Public string SendEmail
...{
Get
...{
Return _ sendemail;
}
Set... {_ sendemail = value ;}
}
Private string _ replyToEmail;
/** // <Summary>
/// Recipient's email account
/// </Summary>
Public string ReplyToEmail
...{
Get... {return _ replyToEmail ;}
Set... {_ replyToEmail = value ;}
}
Private string _ replyUserName;
/** // <Summary>
/// User Name of the respondent
/// </Summary>
Public string ReplyUserName
...{
Get... {return _ replyUserName ;}
Set... {_ replyUserName = value ;}
}
Private string _ getemail;
/** // <Summary>
/// Recipient's email account
/// </Summary>
Public string GetEmail
...{
Get... {return _ getemail ;}
Set... {_ getemail = value ;}
}
Private string _ smtpPassword;
/** // <Summary>
/// Sender's email password (only encrypted strings are received and decrypted when an email is sent)
/// </Summary>
Public string SmtpPassword
...{
Get... {return _ smtpPassword ;}
Set... {_ smtpPassword = value ;}
}
Private string _ content;
/** // <Summary>
/// Email content
/// </Summary>
Public string Content
...{
Get... {return _ content ;}
Set... {_ content = value ;}
}
Private string _ title;
/** // <Summary>
/// Mail title
/// </Summary>
Public string Title
...{
Get... {return _ title ;}
Set... {_ title = value ;}
}
Private string [] _ cc = null;
/** // <Summary>
/// CC email
/// </Summary>
Public string [] cc
...{
Get... {return _ cc ;}
Set... {_ cc = value ;}
}
Private string [] _ bcc = null;
/** // <Summary>
/// BCC
/// </Summary>
Public string [] bcc
...{
Get... {return _ bcc ;}
Set... {_ bcc = value ;}
}
/** // <Summary>
/// Send an email
/// </Summary>
/// <Returns> whether the returned result is successful </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;
}
}
}
Call method and steps:
1. Create an object of the SendMail class and send the required parameters to the object in sequence,
2. Call the Send () method.
You can also extend the SendMail class according to the above description. Here is not an example.
ASP. NET uses the SMTP of the local SMTP virtual server to send emails
First, let's talk about SMTP configuration.
(1) Right-click "SMTP virtual server" and choose "properties". On the "General" tab, set "IP address (P)". What I set is 192.168.1.100.
(2) Select the "access" tab, click "relay", select "only the following list" (selected by default), and click "add ", add 192.168.1.100 to "single computer.
Tip: if it is not completed (2), a common error message is displayed: the server rejects one or more recipient addresses. Server Response: 550 5.7.1 Unable to relay for scucj@126.com (friendly tip: the email address in the error is different) and then start the core code, in fact, and method (a) is similar. The main difference with (1) is: 1. SMTP differences, 2. objMailMessage. in the From statement, you can enter the method as needed, but in (1) do not enter the method using ASP. NET (C #) Core code for sending emails is as follows:
Copy codeThe Code is as follows:
// Start with the core code
Using System. Web. Mail;
MailMessage objMailMessage;
MailAttachment objMailAttachment;
// Create an attachment object
ObjMailAttachment = new MailAttachment ("d: \ test.txt"); // attachments for sending emails
// Create an email message
ObjMailMessage = new MailMessage ();
ObjMailMessage. From = "mysina@sina.com"; // source email address
ObjMailMessage. To = "scucj@126.com"; // destination email address, that is, send it To me
ObjMailMessage. Subject = "mail title: Hello"; // The Mail title
ObjMailMessage. Body = "mail sender content: test whether the email is successfully sent! "; // Content of the email
ObjMailMessage. Attachments. Add (objMailAttachment); // attaches the attachment to the mail message object.
// SMTP address
SmtpMail. SmtpServer = "192.168.1.100 ";
// Start sending emails
SmtpMail. Send (objMailMessage );
The above two methods are introduced here. The simplest way to use the preceding method is to add a server button on the page and put the referenced statement in addition to the button and click the event. Of course, do not forget to put the referenced statement at the top.