The mail sending function is used in a recent project. This is different from the method used to send emails for a previous project. Previously, information about the emails to be sent was inserted into a database table on the server. The database checks newly added records and sends emails. That is, I only need to insert records into a database. This email sending function uses the smtpclient in the Microsoft system. net. Mail namespace to send messages. Pay attention to reference the system. net. Mail namespace during usage.
The process is as follows:
Smtpclient sends an email
/// <Summary>
/// Send email
/// </Summary>
/// <Param name = "server"> SMTP Server </Param>
/// <Param name = "sender"> Sender </Param>
/// <Param name = "recipient"> Recipient </Param>
/// <Param name = "subject"> Topic </Param>
/// <Param name = "body"> Subject content </Param>
/// <Param name = "isbodyhtml"> Whether the subject content is html </Param>
/// <Param name = "encoding"> Encoding Method </Param>
/// <Param name = "isauthentication"> Verify </Param>
/// <Param name = "Files"> Attachment </Param>
Public Static Void Send ( String Server, String Sender, String [] Recipient, String Subject,
String Body, Bool Isbodyhtml, encoding, Bool Isauthentication, Params String [] Files)
{
// SMTP Server
Smtpclient = New Smtpclient (server );
// Email information
Mailmessage message = New Mailmessage ();
// Client user name verification must be the same as the sender here
Message. From = New Mailaddress (sender );
// Recipient
Foreach ( String To In Recipient)
{
Message. to. Add ();
}
// Whether the subject content is html
Message. isbodyhtml = Isbodyhtml;
// Encoding Method
Message. subjectencoding = Encoding;
Message. bodyencoding = Encoding;
// Topic
Message. Subject = Subject;
// Subject content
Message. Body = Body;
// Attachment
Message. attachments. Clear ();
If (Files ! = Null && Files. Length ! = 0 )
{
For ( Int I = 0 ; I < Files. length; ++ I)
{
Attachment attach = New Attachment (files [I]);
Message. attachments. Add (attach );
}
}
If (Isauthentication = True )
{
// User name credential
Smtpclient. Credentials = New Networkcredential (confighelper. getconfigstring ( " Userid " ), Confighelper. getconfigstring ( " Password " ));
}
// Sending method: Network Mode
Smtpclient. deliverymethod = Smtpdeliverymethod. Network;
// The client sends an email
Smtpclient. Send (Message );
}
This is probably the case. There are several points to explain:
1. Public email authentication. Many public mailboxes enhance the restriction on sending emails to users for Anti-Spam purposes. The user name and password are required for security verification. That is, my
Smtpclient. Credentials = new networkcredential (confighelper. getconfigstring ("userid"), confighelper. getconfigstring ("password "));
Confighelper. getconfigstring ("userid") and confighelper. getconfigstring ("password") are methods of other class libraries, where userid and password are set in Web. config. Message. From must be the same as the configured userid. That is, after an identity passes authentication, it is sent as its identity. Otherwise, an error occurs. Another point is that you can set the sending method to network.
2. Sending frequency problems. I want to implement the Intranet (the company uses Foxmail) to send emails. If a large number of emails are sent instantaneously, that is, the frequency of sending several emails in one second is as follows. Failed to send. Email service providers and companies cannot connect. It is easy for email service providers to handle spam. At the same time, the company cannot pass through the process of sending foxmai emails within the company, occupying a large amount of bandwidth resources. (I directly send emails to my 163 mailbox through the company's Foxmail mailbox, there is no limit on the sending frequency, and the sending is successful. As a result, thousands of emails were sent at once, which pushed up 163 of my mailboxes .) Therefore, it is best to send one email in several seconds. By setting system. Threading. thread. Sleep (5000), it is OK to send an email every five seconds.
3. Pay attention to firewall and anti-virus software. I have not paid too much attention to this point, which has been mentioned by others. Pay attention to adding rules to these software to prevent them from blocking email sending.