JMail is a Third-party messaging component that is typically located on the Web server side, and is implemented by mail and email (the client is good with Foxmail). You can use it to easily implement e-mail, cc, BCC, multiple, logging, and mail-receiving functions. This chapter is about: Send mail, CC, Secret Send, multiple, log record.
First, component preparation
Download Jmail44_pro and install (note the installation path)
Locate the installation path and copy the Jmail.dll to the project
Second, the core send code
Create a new MailAPI.cs and enter the following code
Copy Code code 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 mail
</summary>
<param name= "ZT" > Theme </param>
<param name= "ZW" > Body </param>
<param name= "Getmail" > Recipient address </param>
<param name= "Frommail" > Sender address, separated by commas or semicolons between multiple addresses </param>
<param name= "Csmail" > CC address, separated by commas or semicolons between multiple addresses </param>
<param name= "MSMail" > Secret address, separated by commas or semicolons between multiple addresses </param>
<param name= "Fjmail" > Attachment server directory with multiple server-side directories 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 Mail information
Jmessage. Subject = ZT. Trim ()//Mail subject
Jmessage. HTMLBody = zw;//message 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 e-mail address
}
}
Else
{
Jmessage. AddRecipient (Sj[0], "", "" ");
}
Configure CC-PERSON 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 Secret sender 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 Sender Information
Jmessage. ReplyTo = frommail;//Specifies the address to which the recipient replies.
Jmessage. from = frommail;//Mail Sender's e-mail address
Jmessage. FromName = "test! "//The name displayed by the sender of the message
Jmessage. Mailserverusername = frommail;//User name authentication
Jmessage. Mailserverpassword = "password";//password to send message in the server
Configuring attachments
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 mail Sender </a>");
Send mail
for (int i = 0; i < SJ.) Length; i++)
{
Jmessage. Send ("smtp.qq.com", false);/Sending mail server
}
Configuring the JMail Log
Jmessage. Logging = true;
using (StreamWriter sw = new StreamWriter ("E:\\log.txt", True)
{
Sw. Write (jmessage. LOG);
Sw. Close ();
}
Jmessage. Close ()//Send message complete, turn off mail send status
}
}
}
Description
1 in/Configure recipient information//configure CC person information//Configure the Secret Send person information//configuration attachment this around with if...else ... is to solve the problem of single mail sending and multiple mail sending, set to **.length>0 this condition also line, but if do so, for example: No cc person, CS length is 1,cs[0] because there is no this email address to send out;
2 Configure JMail mail log must be written to the back of the sent message, and Jmessage. Logging = true; and SW. Write (jmessage. LOG), must be used at the same time;
3 log records to the file E:\\log.txt, to choose the existence, with log4net also line;
4 When an error occurs: The message is Undeliverable.all servers failed to receive the message, the original because the SMTP server does not support, replace the SMTP server, previously used 163 of the No problem, But now it's 163.
Three, call send code:
Create a new mail.aspx, add a service-side button, and write the following code in its Click event:
Copy Code code as follows:
protected void Button1_Click (object sender, EventArgs e)
{
Mailapi sendMail = new Mailapi ();
String fj = Server.MapPath ("Files/json.rar");
Sendmail.send ("This is the subject", "This is the body", "This is the recipient's mailbox list", "This is the sender's email address", "CC's Mailbox List", "BCC's mailbox List", "Attachment server address List");
Sendmail.send ("This is the subject", "This is the text", "aaa@163.com,bbb@qq.com", "ccc@qq.com", "ddd@163.com,eee@qq.com", "", FJ);
Comma separated by semicolon in English state between lists
}