• 发送QQ邮件


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Mail;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;

    namespace elder.BLL
    {
        public partial class sendmail
        {
            //成功开启POP3/SMTP服务,在第三方客户端登录时,密码框请输入以下授权码:
            //oicbllypdhhzbehi
            ///


            /// 发送邮件(附件好像是不能超过50M)
            ///

            /// 收件人地址
            /// 抄送人地址(多人)
            /// 发件人邮箱
            /// 发件人名称
            /// 授权码(百度上有)
            /// 邮件标题
            /// 邮件内容(支持html格式)
            /// 附件
            public static void SendEmail(String sendTo, string sendCC,
                String fromEmail, String fromName,string fromCode, String title, String body,
                List attachment
                )
            {
                if (string.IsNullOrEmpty(fromEmail)) { fromEmail = "xxxxxxx@qq.com"; }
                if (string.IsNullOrEmpty(fromCode)) { fromCode = "oicbllypdhhzbehi"; }
                if (string.IsNullOrEmpty(title)) { title = DateTime.Now.ToString("yyyyMMdd") + "数据备份"; }
                if (string.IsNullOrEmpty(body)) { body = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }


                MailMessage msg = new MailMessage();
                String[] sendCCArr = null;
                //设置收件人地址
                msg.To.Add(sendTo); //收件人地址 
                //设置抄送人地址
                if (!string.IsNullOrEmpty(sendCC))
                {
                    sendCCArr = sendCC.Split(';');
                    if (sendCCArr.Length > 0)
                    {
                        foreach (String cc in sendCCArr)
                        {
                            msg.CC.Add(cc);
                        }
                    }
                }
                //设置发件人邮箱及名称
                msg.From = new MailAddress(fromEmail, fromName);

                msg.Subject = title;//邮件标题 
                msg.SubjectEncoding = Encoding.UTF8; //标题格式为UTF8 

                msg.Body = body;//邮件内容
                msg.BodyEncoding = Encoding.UTF8; //内容格式为UTF8 
                msg.IsBodyHtml = true;//设置邮件格式为html格式

                //string filePath = @"E:\导出数据.xls";//添加附件
                for(int i=0;i             {
                    if(System.IO.File.Exists(attachment[i]))
                    {
                        msg.Attachments.Add(new Attachment(attachment[i]));
                    }
                   
                }
             
                SmtpClient client = new SmtpClient();

                //发送邮箱信息
                client.Host = "smtp.qq.com"; //SMTP服务器地址 
                client.Port = 587; //SMTP端口,QQ邮箱填写587 

                client.EnableSsl = true; //启用SSL加密 (使用除QQ邮箱之外的最好关闭)
                client.Timeout = 9999999;
                //发件人邮箱账号,授权码
                //授权码获取请自行百度
                client.Credentials = new System.Net.NetworkCredential(fromEmail,fromCode);

                try
                {
                    client.Send(msg); //发送邮件
                   // client.SendAsync(msg, fromCode);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            ///


            /// 邮箱格式校验
            ///

            ///
            ///
            private bool EmailFormat(string address)
            {
                Regex r = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
                if (r.IsMatch(address))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }
    }
     

  • 相关阅读:
    PageOffice 在线编辑 office文件,回调父页面
    SQL手工注入漏洞测试(Access数据库)
    gradle缓存路径
    学习大数据之后可以做什么?未来发展怎么样?
    Shell脚本——提取目录名和文件名
    C++类的赋值操作
    营销互动类小游戏策划与开发
    Oracle数据库:oracle内连接inner join on,多表查询各种自链接、内连接、外连接的练习示例
    025: vue父子组件中传递方法控制:$emit,$refs,$parent,$children
    Web前端开发之HTML_2
  • 原文地址:https://blog.csdn.net/wysdong/article/details/126359437