asp教程.net 邮件发送类(带附件)
using system;
using system.collections.generic;
using system.text;
using system.net;
using system.net.mail;
using system.net.mime;
namespace windowsformsapplication1
{
class sendmail
{
private mailmessage mailmessage;
private smtpclient smtpclient;
private string password;//发件人密码
///
/// 处审核后类的实例
///
/// 收件人地址
/// 发件人地址
/// 邮件正文
/// 邮件的主题
/// 发件人密码
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;
}
///
/// 添加附件
///
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);//添加到附件中
}
}
///
/// 异步发送邮件
///
///
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);
}
}
///
/// 发送邮件
///
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);
}
}
}
}
我的博物馆故事 官方安卓版v1.61.2
我的博物馆故事是一款以消除为主题的经营养成类手游,在这里玩家
专业模拟飞行10 手机版v12.2.4
专业模拟飞行10安卓版是一款飞行休闲手游,顶尖的物理飞行引擎
动物起义战斗模拟器二琳同款 最新版v4.1.1
动物起义战斗模拟器是一个非常有趣的模拟类游戏,玩家可以召唤各
迷你世界七周年 安卓手机版v1.43.0
迷你世界7周年是一款由《迷你世界》官方推出的庆祝特别版本,在
劫后公司无限资源版 v1.0.5.1
劫后公司内置菜单版是游戏的破解版本,在该版本中为玩家提供了内