163和腾讯企业邮箱 发邮件,多个附件

1、腾讯企业邮箱发送 报错:mail from address must be same as authorization user

用账号密码登录,不要用微信扫。进入后:点 设置——邮箱绑定——生成新密码。用新密码代替原登录密码。因为这里显示“关闭安全登录”,说明已经开启了安全登录。

 2、465 端口发送邮件

 /// <summary>
        /// 465 端口发送邮件 带附件
        /// </summary>
        /// <param name="mailHost">邮件服务器</param>
        /// <param name="mailPort">端口</param>
        /// <param name="strSendMail">发送邮件的用户名</param>
        /// <param name="mailPassword">发送邮件的密码</param>
        /// <param name="strToList">收件人邮箱地址</param>
        /// <param name="file">附件地址</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="body">内容</param>
        public static void Sendmail(string mailHost, string strSendMail, string mailPassword, string strToList, string subject = "", string body = "", List<string> file = null)
        {
            try
            {
                System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
                //验证  
                mmsg.Subject = subject;// "zhuti1";//邮件主题

                mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
                mmsg.Body = body;// "wqerwerwerwer";//邮件正文
                mmsg.BodyEncoding = Encoding.UTF8;//正文编码
                mmsg.Priority = System.Web.Mail.MailPriority.High;//优先级

                //添加邮件附件,可发送多个文件
                if (file != null && file.Count() > 0)
                {
                    foreach (var filename in file)
                    {
                        System.Web.Mail.MailAttachment mailAttach_1 = new System.Web.Mail.MailAttachment(filename);
                        mmsg.Attachments.Add(mailAttach_1);
                    }
                }
                //发件者邮箱地址
                mmsg.From = strSendMail;
                mmsg.To = strToList;//收件人收箱地址
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//身份验证
                //登陆名  邮箱登录账号,这里跟前面的发送账号一样就行
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mmsg.From);
                //登陆密码 这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码 
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", mailPassword);
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口 
                //SSL加密
                mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
                ////企业账号用smtp.exmail.qq.com
                System.Web.Mail.SmtpMail.SmtpServer = mailHost;
                System.Web.Mail.SmtpMail.Send(mmsg);
            }
            catch (Exception ex)
            {
                throw new Exception("邮箱异常!" + ex.Message);
            }

        }

3、多附件发送

     
     /// <summary>
        ///  发送邮件,提供调用
        /// </summary>
        /// <param name="strSendToList">收件人,多个收件人用做好事隔开</param>
        /// <param name="strSubject">主题</param>
        /// <param name="strBody">内容</param>
        /// <param name="strAttachmentsPath">附件路径</param>
        /// <param name="strOutErr">错误信息</param>
        /// <returns></returns>
        public bool SendMail(string strSendToList, string strSubject, string strBody, string[] arrAttachmentsPath, out string strOutErr)
        {
            //发送邮件服务器
            string strHost = System.Configuration.ConfigurationManager.AppSettings["MailHost"].ToString();
            //发件人地址
            string strUserMail = System.Configuration.ConfigurationManager.AppSettings["MailSender"].ToString();
            //发件人密码
            string strUserPwd = System.Configuration.ConfigurationManager.AppSettings["MailSenderPwd"].ToString();           
            string ss = string.Empty;
            return SendMail(strHost, strUserMail, strUserPwd, strSendToList, strSubject, strBody, arrAttachmentsPath,out strOutErr);
       
        }

        /// <summary>
        /// 发邮件
        /// </summary>
        /// <param name="strMailService">邮箱服务器</param>
        /// <param name="strSendMail">发件人邮箱</param>
        /// <param name="strSendMailPwd">发件人密码</param>
        /// <param name="strToList">收件人,多个收件人用做好事隔开</param>
        /// <param name="strSubject">主题</param>
        /// <param name="strMessage">邮件内容</param>
        /// <param name="AttachFile">附件路径</param>
        public bool SendMail(string strMailService, string strSendMail, string strSendMailPwd, string strToList, string strSubject, string strMessage, string[] AttachFile, out string strOutErr)
        {
            System.Net.Mail.SmtpClient client = new SmtpClient(strMailService);
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(strSendMail, strSendMailPwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strSendMail, strToList);
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Subject = strSubject;
            message.Body = strMessage;
            message.IsBodyHtml = true;
            //在有附件的情况下添加附件
            if (AttachFile != null && AttachFile.Length > 0)
            {
                foreach (string path in AttachFile)
                {
                    var attachFile = new Attachment(path);
                    message.Attachments.Add(attachFile);
                }
            }
            try
            {
                client.Send(message);
                strOutErr = string.Empty;
            }
            catch (Exception err)
            {
                strOutErr = err.Message;
                return false;
            }
            return true;
        }
 

4. C#代码直接发送

腾讯企业邮箱端口号用:25 、用:465有时会发送不成功。

 

1)163发送:
SmtpClient smtp = new SmtpClient("smtp.163.com", 25);
2)腾讯企业邮箱发送:

POP3/SMTP协议
POP3/SMTP协议:
接收邮件服务器:pop.exmail.qq.com (端口 110),使用SSL,端口号995
发送邮件服务器:smtp.exmail.qq.com (端口 25),使用SSL,端口号465或587
IMAP协议:
接收邮件服务器:imap.exmail.qq.com (端口 143),使用SSL,端口号993
发送邮件服务器:smtp.exmail.qq.com (端口 25),使用SSL,端口号465或587
账户名:您的企业邮箱账户名,账户名需要填写完整的邮件地址
密码:您的企业邮箱密码
电子邮件地址:您的企业邮箱的完整邮件地址
海外用户可使用以下服务器
POP3/SMTP协议
接收邮件服务器:hwpop.exmail.qq.com ,使用SSL,端口号995
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465

IMAP协议
接收邮件服务器:hwimap.exmail.qq.com ,使用SSL,端口号993
发送邮件服务器:hwsmtp.exmail.qq.com ,使用SSL,端口号465

如下代码已验证可用:

 /// <summary>
        /// 发送邮件 有问题
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="list">收件人</param>
        public static async void  Send(string title, string content, params string[] list)
        {
            try { 
            MailMessage message = new MailMessage
            {
                From = new MailAddress(address, name)//发送人邮箱
            };
            foreach (var item in list)
            {
                message.To.Add(item);//收件人地址
            }

            message.Subject = title;//发送邮件的标题
            message.Body = content;//发送邮件的内容
                                   //配置smtp服务地址
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient
            {
                Host = host,
                Port = port,//端口 腾讯企业邮箱端口号用:25 、用:465有时会发送不成功。
                EnableSsl = enableSsl,
                //发送人邮箱与授权密码
                Credentials = new NetworkCredential(address, authPwd)
            };
             client.SendAsync(message, UserContext.Current.UserInfo.User_Id);
            }
            catch (Exception ex)
            {
                //这个日志可能也没有用。
                Logger.Error("委托单发邮件失败!"+ex.Message);
            }
        }

 调用:

 StringBuilder sb= new StringBuilder();
                    sb.Append($@"{userInfo[0].UserTrueName} 你好!" + "\r\n\r\n");
                    sb.Append($@"车牌:{updateEntity.CarrierNo} 待审批,请尽快处理。" + "\r\n");
                    sb.Append($@"系统邮件,请勿回复!" + "\r\n\r\n");                    
                    
                    Send("车辆备案审批", sb.ToString(), userInfo[0].Email);

 

posted @ 2019-08-05 15:11  丁焕轩  阅读(467)  评论(0编辑  收藏  举报