C# send mail class which can send multiple attachments
1: Create a SendMail class
2: Call method
SendMail send = new SendMail(“123456@gmail.com”, “123456@hotmail.com”, “Mail Content Test”, “Mail Header Test”, “mail20190206”);
send.Attachments(@”D:\\Work \\abcdef.txt”);
send.Attachments(@”D:\\work\123456.txt”);
send.Send();
3: the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net.Mime;
/// <summary>
/// Summary of SendMail
/// </summary>
public class SendMail
{
private MailMessage mailMessage;
private SmtpClient smtpClient;
private string password;//Sender password
/// <summary>
// / Instance of the post-audit class
/// </summary>
/// <param name=”To”>recipient address</param>
/// <param name=”From”>Sender address</ Param>
/// <param name=”Body”>mail body</param>
/// <param name=”Title”>message of the mail</param>
/// <param name=”Password”>send Person password</param>
public SendMail(string To, string From, string Body, string Title, string Password)
{
mailMessage = new MailMessage();
mailMessage.To.Add(To);
mailMessage.From = new System.Net.Mail.MailAddress(From);
mailMessage.Subject = Title;
mailMessage.Body = Body;
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
this.password = Password;
}
/// <summary>
/// 添加附件
/// </summary>
public void Attachments(string Path)
{
string[] path = Path.Split(‘,’);
Attachment data;
ContentDisposition disposition;
for (int i = 0; i < path.Length; i++)
{
data = new Attachment(path[i], MediaTypeNames.Application.Octet);//实例化附件
disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);//Get the attachment creation date
disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//Get the attachment modification date
disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//Get the read date of the
attachment mailMessage.Attachments.Add(data);//Add to the attachment
}
}
/// <summary>
/ // Send mail asynchronously
/// </summary>
/// <param name=”CompletedMethod”></param>
public void SendAsync(SendCompletedEventHandler CompletedMethod)
{
if (mailMessage != null)
{
smtpClient = new SmtpClient();
smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//Set the sender’s identity ticket
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host = “smtp.” + mailMessage.From.Host;
smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//Register the event when the asynchronous send mail is completed
smtpClient.SendAsync(mailMessage, mailMessage.Body);
}
}
/// <summary>
/// Send mail
/// </summary>
public void Send()
{
if (mailMessage != null)
{
smtpClient = new SmtpClient( );
smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//Set the sender’s identity ticket
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host = ” Smtp.” + mailMessage.From.Host;
smtpClient.Send(mailMessage);
}
}
}
Comment disabled