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

c# 发邮件

程序员文章站 2022-05-12 12:38:14
using System; using System.Collections.Generic; using System.Text; using System.Configuration; using System.IO; using System.Web; using System.Net.Mai... ......
using system;
using system.collections.generic;
using system.text;
using system.configuration;
using system.io;
using system.web;
using system.net.mail;
using system.net;
using system.componentmodel;

namespace easyframe.common
{
    public class gtmail
    {
        #region 发送电子邮件
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="smtpserver">smtp服务器</param>
        /// <param name="username">登录帐号</param>
        /// <param name="pwd">登录密码</param>
        /// <param name="nickname">发件人昵称</param>
        /// <param name="strfrom">发件人</param>
        /// <param name="strto">收件人</param>
        /// <param name="subj">主题</param>
        /// <param name="bodys">内容</param>
        public static void sendmail(string smtpserver, string username, string pwd, string nickname, string strfrom, string strto, string subj, string bodys)
        {
            smtpclient _smtpclient = new smtpclient();
            _smtpclient.deliverymethod = smtpdeliverymethod.network;//指定电子邮件发送方式
            _smtpclient.host = smtpserver;//指定smtp服务器
            _smtpclient.credentials = new system.net.networkcredential(username, pwd);//用户名和密码

            //mailmessage _mailmessage = new mailmessage(strfrom, strto);
            mailaddress _from = new mailaddress(strfrom, nickname);
            mailaddress _to = new mailaddress(strto);
            mailmessage _mailmessage = new mailmessage(_from, _to);
            _mailmessage.subject = subj;//主题
            _mailmessage.body = bodys;//内容
            _mailmessage.bodyencoding = system.text.encoding.default;//正文编码
            _mailmessage.isbodyhtml = true;//设置为html格式
            _mailmessage.priority = mailpriority.normal;//优先级
            _smtpclient.send(_mailmessage);
        }
        #endregion

        #region 简单电子邮件发送
        /// <summary>
        /// 简单电子邮件发送
        /// </summary>
        /// <param name="smtpserver">smtp服务器</param>
        /// <param name="username">登录帐号</param>
        /// <param name="pwd">登录密码</param>
        /// <param name="nickname">发件人昵称</param>
        /// <param name="mailfrom">发件人</param>
        /// <param name="mailto">收件人</param>
        /// <param name="subj">主题</param>
        /// <param name="bodys">内容</param>
        public static void sendmail(string smtpserver, int smtpport, string username, string pwd, string nickname, string mailfrom, string mailto, string subj, string bodys)
        {
            smtpclient _smtpclient = new smtpclient();
            _smtpclient.deliverymethod = smtpdeliverymethod.network;//指定电子邮件发送方式
            _smtpclient.host = smtpserver;//指定smtp服务器
            _smtpclient.port = smtpport;
            _smtpclient.enablessl = true;
            //_smtpclient.sendcompleted += new sendcompletedeventhandler(smtp_sendcompleted);
            _smtpclient.credentials = new networkcredential(username, pwd);//用户名和密码    
            mailaddress _from = new mailaddress(mailfrom, nickname, encoding.getencoding(936));
            mailaddress _to = new mailaddress(mailto);
            mailmessage _mailmessage = new mailmessage(_from, _to);
            _mailmessage.subject = subj;//主题
            _mailmessage.body = bodys;//内容
            _mailmessage.bodyencoding = encoding.default;//正文编码
            _mailmessage.isbodyhtml = true;//设置为html格式
            _mailmessage.priority = mailpriority.normal;//优先级
            _smtpclient.send(_mailmessage);
            //_smtpclient.sendmailasync(_mailmessage);
        }

        private static void smtp_sendcompleted(object sender, asynccompletedeventargs e)
        {
            string msg = e.userstate.tostring();
            smtpclient smtp = (smtpclient)sender;
            if (e.error == null)//问题出现后,这里的error并没有错误
            {
                msg += ",1";//发送成功
            }
            else
            {
                msg += ",0";
                smtp.sendasynccancel();
            }
        }
        #endregion

        #region 邮件批量发送
        /// <summary>
        /// 邮件批量发送
        /// </summary>
        /// <param name="smtpserver"></param>
        /// <param name="smtpport"></param>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <param name="mailfrom"></param>
        /// <param name="nickname"></param>
        /// <param name="mailto"></param>
        /// <param name="subj"></param>
        /// <param name="bodys"></param>
        /// <param name="cc"></param>
        /// <param name="bcc"></param>
        /// <param name="mailattachments"></param>
        public static void sendmail(string smtpserver, int smtpport, string username, string pwd, string mailfrom, string nickname, string[] mailto, string subj, string bodys, string[] cc, string[] bcc, string[] mailattachments)
        {
            smtpclient smtp = new smtpclient(); //实例化一个smtpclient
            smtp.deliverymethod = smtpdeliverymethod.network; //将smtp的出站方式设为 network
            smtp.enablessl = false;//smtp服务器是否启用ssl加密
            smtp.host = smtpserver; //指定 smtp 服务器地址
            smtp.port = smtpport;             //指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去
                                              //如果你的smtp服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了
            smtp.usedefaultcredentials = true;
            //smtp.sendcompleted += new sendcompletedeventhandler(smtp_sendcompleted);
            //如果需要认证,则用下面的方式
            smtp.credentials = new networkcredential(username, pwd);
            mailmessage mm = new mailmessage(); //实例化一个邮件类
            mm.priority = mailpriority.normal; //邮件的优先级,分为 low, normal, high,通常用 normal即可
            mm.from = new mailaddress(mailfrom, nickname, encoding.getencoding(936));
            //收件方看到的邮件来源;
            //第一个参数是发信人邮件地址
            //第二参数是发信人显示的名称
            //第三个参数是 第二个参数所使用的编码,如果指定不正确,则对方收到后显示乱码
            //936是简体中文的codepage值
            //注:上面的邮件来源,一定要和你登录邮箱的帐号一致,否则会认证失败

            mm.replytolist.add(mailfrom);
            //replyto 表示对方回复邮件时默认的接收地址,即:你用一个邮箱发信,但却用另一个来收信

            if (cc.length > 0)
            {
                mm.cc.add(string.join(",", cc));//抄送  
            }
            if (bcc.length > 0)
            {
                mm.bcc.add(string.join(",", bcc));//密送
            }
            if (mailattachments.length > 0)
            {
                for (int i = 0; i < mailattachments.length; i++)
                {
                    attachment objmailattachment = new attachment(@mailattachments[i]);//发送邮件的附件的地址
                    mm.attachments.add(objmailattachment);//将附件附加到邮件消息对象中 
                }
            }
            mm.to.add(string.join(",", mailto));
            mm.subject = subj; //邮件标题
            mm.subjectencoding = encoding.getencoding(936);
            // 这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。
            // 936是简体中文的pagecode,如果是英文标题,这句可以忽略不用
            mm.isbodyhtml = true; //邮件正文是否是html格式

            mm.bodyencoding = encoding.getencoding(936);
            //邮件正文的编码, 设置不正确, 接收者会收到乱码

            mm.body = bodys;
            //邮件正文
            smtp.send(mm);
            //smtp.sendmailasync(mm); //发送邮件,如果不返回异常, 则大功告成了。
        }
        #endregion 
    }
}