欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#发送邮件,可发送多个附件

程序员文章站 2022-07-05 13:55:26
1:创建SendMail类 2:调用方法 SendMail send = new SendMail("123456@qq.com", "123456@163.com", "邮件内容测试", "邮件标题测试", "mail20181224");send.Attachments(@"D:\\工作\\ab ......

1:创建sendmail类

2:调用方法

sendmail send = new sendmail("123456@qq.com", "123456@163.com", "邮件内容测试", "邮件标题测试", "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);
}
}
}