net jMail includes cc BCC MULTIPLE and logging instance codes
- 2020-09-28 08:50:39
- OfStack
jmail is a third party mail operation component, usually on the web server side, that receives and sends mail (Foxmail and the like are good for clients). It can be used to easily realize the functions of sending emails, cc, BCC, multiple, logging and receiving emails. This chapter is about emailing, CC, BCC, multi-tasking, and logging.
1. Component preparation
Download JMail44_pro and install (note the installation path)
Find the installation path and copy jmail.dll from it into the project
2. Core sending code
Create MailAPI.cs and enter the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using jmail;
using System.IO;
namespace JmailDemo
{
public class MailAPI
{
/// <summary>
/// Mail delivery
/// </summary>
/// <param name="zt"> The theme </param>
/// <param name="zw"> The body of the </param>
/// <param name="getMail"> Recipient address </param>
/// <param name="fromMail"> The address of the sender, separated by a comma or semicolon </param>
/// <param name="csMail"> A cc address separated by a comma or semicolon </param>
/// <param name="msMail"> BCC addresses separated by English commas or semicolons </param>
/// <param name="fjMail"> Attachment server directory, multiple server side directories separated by English 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 The system configuration
jmessage.Charset = "GB2312";
jmessage.Encoding = "base64";
// Configuring mail information
jmessage.Subject = zt.Trim();// Email subject
jmessage.HTMLBody = zw;// Email 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 cc 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 sender information
jmessage.ReplyTo = fromMail;// The address to which the designated recipient responds
jmessage.From = fromMail;// The email address of the sender of the message
jmessage.FromName = "Test ! ";// The name of the sender of the message
jmessage.MailServerUserName = fromMail;// Username authentication
jmessage.MailServerPassWord = "password";// The password to send the message on 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='https://www.ofstack.com'> The sender of this message </a>");
// Send E-mail
for (int i = 0; i < sj.Length; i++)
{
jmessage.Send("smtp.qq.com", false);// Sending mail server
}
// configuration jmail The log
jmessage.Logging = true;
using (StreamWriter sw = new StreamWriter("E:\\log.txt", true))
{
sw.Write(jmessage.Log);
sw.Close();
}
jmessage.Close();// When the mail has been sent, turn off the mail sending state
}
}
}
Description:
1) Use if... at // configure recipient information // cc information // configure BCC information // Configure attachment 4. else... Is set to **.length to solve the problem of single and multiple mail sending
>
The 0 condition is ok, but if you do this, for example, cs has length 1 when there is no CC, cs[0] is not sent because there is no email address.
2) Configure jmail email log 1 must be written to the end of the sent email, and jmessage. Logging = true; And sw Write (jmessage Log); 1 must be used at the same time;
3) Log to file E:\\ log.txt, choose to exist, use log4net;
4) When an error is reported: the message ES52en. all servers receive the message, the reason is that the smtp server is not supported, so replace the smtp server. It is ok to use 163 before, but it is not ok to use 163 now.
3. Call sending code:
Create Mail. aspx, add 1 server button, and write the following code in its click event:
protected void Button1_Click(object sender, EventArgs e)
{
MailAPI sendMail = new MailAPI();
string fj = Server.MapPath("files/JSON.rar");
//sendMail.Send(" This is a topic ", " This is the body of the ", " This is the recipient email list ", " This is the E-mail address of the sender ", " Cc email list ", " BCC email list ", " Attachment server address list ");
sendMail.Send(" This is a topic ", " This is the body of the ", "aaa@163.com,bbb@qq.com", "ccc@qq.com", "ddd@163.com,eee@qq.com", "", fj);
// Between the lists The English state is separated by a comma and a minute
}