Jmail is a third-party mail operation component, usually located on the web server, to achieve mail receiving and sending functions (the client can use Foxmail ). It can be used to easily implement the functions of sending, CC, BCC, sending, logging, and email receiving. This chapter focuses on mail, CC, BCC, multiple sending, and log records.
I. Component preparation
Download and install JMail44_pro (write down the installation path)
Find the installation path,Copy jmail. dll to the project.
Ii. Core sending code
Create MailAPI. cs and enter the following code
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using jmail;
Using System. IO;
Namespace JmailDemo
{
Public class MailAPI
{
/// <Summary>
/// Send email
/// </Summary>
/// <Param name = "zt"> topic </param>
/// <Param name = "zw"> body </param>
/// <Param name = "getMail"> recipient address </param>
/// <Param name = "fromMail"> sender address. Separate multiple addresses with commas (,) or semicolons (;). </param>
/// <Param name = "csMail"> CC address. Multiple addresses are separated by commas (,) or semicolons (;). </param>
/// <Param name = "msMail"> BCC address. Multiple addresses are separated by commas (,) or semicolons (;). </param>
/// <Param name = "fjMail"> the attachment Server Directory, separated by commas (,) or semicolons (;). </param>
Public void Send (string zt, string zw, string getMail, string fromMail, string csMail, string msMail, string fjMail)
{
Jmail. Message jmessage = new Message ();
// Jmail System Configuration
Jmessage. Charset = "GB2312 ";
Jmessage. Encoding = "base64 ";
// Configure email information
Jmessage. Subject = zt. Trim (); // Email Subject
Jmessage. HTMLBody = zw; // mail body
// Configure Recipient Information
String [] sj = getMail. Split (',',';');
If (sj. Length> 1)
{
For (int I = 0; I <sj. Length; I ++)
{
Jmessage. AddRecipient (sj [I], "", ""); // recipient email address
}
}
Else
{
Jmessage. AddRecipient (sj [0], "", "");
}
// Configure the CC user information
String [] cs = csMail. Split (',',';');
If (cs. Length> 1)
{
For (int I = 0; I <cs. Length; I ++)
{
Jmessage. AddRecipientCC (cs [I]);
}
}
Else
{
Jmessage. AddRecipient (cs [0], "", "");
}
// Configure the BCC information
String [] MS = msMail. Split (',',';');
If (ms. Length> 1)
{
For (int I = 0; I <ms. Length; I ++)
{
Jmessage. AddRecipientBCC (MS [I]);
}
}
Else
{
Jmessage. AddRecipient (MS [0], "", "");
}
// Configure the sender information
Jmessage. ReplyTo = fromMail; // specifies the address to which the recipient replies.
Jmessage. From = fromMail; // email address of the mail sender
Jmessage. FromName = "Test! "; // Name displayed by the mail sender
Jmessage. MailServerUserName = fromMail; // User Name authentication
Jmessage. MailServerPassWord = "password"; // password used to send emails to the server
// Configure the attachment
String [] fj = fjMail. Split (',',';');
If (fj. Length> 1)
{
For (int I = 0; I <fj. Length; I ++)
{
Jmessage. AddAttachment (fj [I], true, null );
}
}
Else
{
Jmessage. AddRecipient (sj [0], "", "");
}
// Configure append information
Jmessage. AppendHTML ("<a href = 'HTTP: // www.jb51.net '> this email sender </a> ");
// Send an email
For (int I = 0; I <sj. Length; I ++)
{
Jmessage. Send ("smtp.qq.com", false); // Mail Server
}
// Configure jmail logs
Jmessage. Logging = true;
Using (StreamWriter sw = new StreamWriter ("E: \ log.txt", true ))
{
Sw. Write (jmessage. Log );
Sw. Close ();
}
Jmessage. Close (); // After the email is sent, disable the email sending status.
}
}
}
Note:
1) In // configure the Recipient Information // configure the CC user information // configure the BCC information // configure the attachment to use if... else... it is set to ** to solve the problem of single mail sending and multi-mail sending **. the length> 0 condition can also be used, but in this case, for example, if no cc is sent, the cs length is 1, and cs [0] is not sent because there is no such email address;
2) configure the jmail mail Log to be written to the end of the sent mail, and jmessage. Logging = true; and sw. Write (jmessage. Log); must be used at the same time;
3) logs must be recorded in the file E: \ log.txt. The log must exist and use log4net;
4) When an error is reported: the message was undeliverable. when all servers failed to receive the message, the reason is that the smtp server is not supported and the smtp server is changed. It is okay to use 163 before, but now it cannot work with 163.
3. Call the sending code:
Create Mail. aspx, add a server button, and write the following code in the event:
Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
MailAPI sendMail = new MailAPI ();
String fj = Server. MapPath ("files/JSON.rar ");
// SendMail. send ("this is the topic", "this is the body", "this is the recipient email list", "this is the sender email address", "cc email list", "BCC email list ", "attachment server address list ");
SendMail. Send ("this is the subject", "this is the body", "aaa@163.com, bbb@qq.com", "ccc@qq.com", "ddd@163.com," ", fj );
// Lists are separated by commas (,).
}