OutLook发送邮件

   public BaseResult Office365Send(string toEmail, string fromEmail, string subject, string body, bool isBodyHtml)
        {
            try
            {
                logger.Info("发送失败,用户:" + toEmail + " ,主题:" + subject);
                MailMessage msg = new MailMessage();
                msg.To.Add(new MailAddress(toEmail, toEmail));
                msg.From = new MailAddress(fromEmail, fromEmail);
                msg.Subject = subject;
                msg.Body = body;
                msg.Priority = MailPriority.High;
                msg.IsBodyHtml = isBodyHtml;
                msg.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); //防止成为垃圾邮件,披上outlook的马甲
                msg.BodyEncoding = Encoding.GetEncoding("GB2312");
                msg.Priority = MailPriority.High;

                SmtpClient client = new SmtpClient();
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential(_serverUserName, _serverUserPassword);
                client.Port = 587;
                //client.Host = "smtp.office365.com";
                //client.TargetName = "STARTTLS/smtp.office365.com";
                client.Host = "outlook.office365.com";
                client.TargetName = "STARTTLS/outlook.office365.com";
                //client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.EnableSsl = true;
                client.Send(msg);
            }
            catch (Exception ex)
            {
                logger.Error("发送失败,用户:" + toEmail + ",内部错误:" + ex.Message);
                return new BaseResult { code = 1, msg = "发送失败,内部错误:" + ex.Message };
            }
            return new BaseResult { code = 0, msg = "发送成功" };
        }

需要关闭微软的默认安全值

posted @ 2021-04-23 14:40  _York  阅读(158)  评论(0编辑  收藏  举报