c#發送郵件,可發送多個附件
1:創建SendMail類
2:調用方法
SendMail send = new SendMail("[email protected]", "[email protected]", "郵件內容測試", "郵件標題測試", "mail20181224");
send.Attachments(@"D:\\工作\\abc.txt");
send.Attachments(@"D:\\工作\\123.txt");
send.Send();
---------------------------------------------------------------------------------------------------下面是類-------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net.Mime;
/// <summary>
/// SendMail 的摘要說明
/// </summary>
public class SendMail
{
private MailMessage mailMessage;
private SmtpClient smtpClient;
private string password;//發件人密碼
/// <summary>
/// 處審核後類的實例
/// </summary>
/// <param name="To">收件人地址</param>
/// <param name="From">發件人地址</param>
/// <param name="Body">郵件正文</param>
/// <param name="Title">郵件的主題</param>
/// <param name="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]);//獲取附件的創建日期
disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);//獲取附件的修改日期
disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);//獲取附件的讀取日期
mailMessage.Attachments.Add(data);//添加到附件中
}
}
/// <summary>
/// 異步發送郵件
/// </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);//設置發件人身份的票據
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host = "smtp." + mailMessage.From.Host;
smtpClient.SendCompleted += new SendCompletedEventHandler(CompletedMethod);//註冊異步發送郵件完成時的事件
smtpClient.SendAsync(mailMessage, mailMessage.Body);
}
}
/// <summary>
/// 發送郵件
/// </summary>
public void Send()
{
if (mailMessage != null)
{
smtpClient = new SmtpClient();
smtpClient.Credentials = new System.Net.NetworkCredential(mailMessage.From.Address, password);//設置發件人身份的票據
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.Host = "smtp." + mailMessage.From.Host;
smtpClient.Send(mailMessage);
}
}
}
c#發送郵件,可發送多個附件