使用Gmail发送邮件心得

 /// <summary>
        /// 发送邮件
        /// </summary>
        /// <returns></returns>
        public static bool SendEmail(string subject, string body, string to)
        {
            MailMessage myEmail = new MailMessage();
            Encoding eEncod = Encoding.GetEncoding("utf-8");
            myEmail.From = new System.Net.Mail.MailAddress(SmtpUserName, SmtpUserName, eEncod);
            myEmail.To.Add(to);
            myEmail.Subject = subject;
            myEmail.Body = body;
            myEmail.BodyEncoding = Encoding.UTF8;
            myEmail.Priority = System.Net.Mail.MailPriority.Normal;
            myEmail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient(SmtpServer, 587); smtp.EnableSsl = true; string UserName = SmtpUserName; string Password = SmtpPassword; smtp.Credentials = new System.Net.NetworkCredential(UserName, Password); try { smtp.Send(myEmail); return true; } catch (Exception ex) { return false; } return true; }

注意:

使用gmail发送邮件写端口,但是当把端口设为465时,会报超时,所以只能设为587

原文地址:https://www.cnblogs.com/loveAnimal/p/3259462.html