asp.net 邮件发送类(带附件)

作者:袖梨 2022-06-25
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);
            }
        } 
    }
}

相关文章

精彩推荐