ASP. NET allows you to send emails + multiple recipients + multiple attachments and asp.net recipients.
Recently, the project needs to implement the function of sending emails + adding attachments, so I learned about the System. net. mail. mailMessage and System. net. mail. smtpClient. net, according to some code on the internet, I made a Demo to share it.
The old rule is to check the effect first. The mail sending interface is as follows:
The sender enters the sender's email address and password;
You can enter multiple recipients in the recipient column. Multiple recipients must be separated by semicolons (;) in English, you can also design a contact input method as needed, such as selecting a contact from the address book );
You can select multiple attachments for themes and content. Only three input controls are provided here. You can also add or delete controls or dynamically set the number of controls as required.
Let's take a look at the code and sort out the ideas based on the Code:
This is the solution structure of this small Demo:
A files folder is created in the root directory, which serves as a temporary directory on the server when the client sends an email (if you do not understand it here, please continue reading it ).
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "SendEmail_Attachments.Default" %> <! DOCTYPE html> The interface code is relatively simple.
Default. aspx. cs
Reference the corresponding namespace
Here we mainly use two classes: System. net. mail. mailMessage and System. net. mail. smtpClient, in short, MailMessage is the mail we want to send. The SmtpClient function is to send the mail under certain conditions.
System. Net. Mail. MailMessage class
It can be found on MSDN that the attributes of MailMessage are as follows:
The recipient To attributes, CC, BCC, and Attachments can all be multiple values. Therefore, in the code above, in the SendEmail () function () you can add multiple recipients and multiple attachments to the email. The BCC and CC functions are not added here, but the principle is the same.
System. Net. Mail. SmtpClient class
Attributes of SmtpClient include:
Based on these attributes and methods, you can certainly understand how SmtpClient sends emails in the Code. Here are some notes:
The Host attribute of SmtpClient is different for servers used to send different types of emails. Therefore, the Host attribute is different. If you have multiple types of mailboxes, check whether the address of the sender server in each mailbox is different, for example:
163 mail sending server address: smtp.163.com
The sending server address of Google Mail is smtp.gmail.com.
Hotmail mail sending server address: smtp-mail.outlook.com
The sending server address of Foxmail and QQ mail is smtp.qq.com.
Therefore, you need to determine the sender's email type to set the sender server address ~ Region ~
When the email is successfully sent or fails, you must call the DeleteFiles () function to delete the temporary files (attachments) uploaded to the server, because this example does not need to add this folder for testing on a computer, you just need to add the local file as an attachment to the MailMessage instance, but because the code myEmail is added as an attachment for MailMessage. attachments. add (new Attachment (ServerFileName); is run on the server. Therefore, if a program is released, it cannot be performed in a remote location, therefore, you must first upload the attachment to the server (the files folder is added to the server to store temporary files), add the attachment to MailMessage, and finally clear the attachment uploaded to the client.
This is done for the time being, and then optimized when necessary.
It is not easy to be original, and the reference article is attached at last:
This example is modified on the basis of this document.