使用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; }

注意:orm

使用gmail发送邮件写端口,可是当把端口设为465时,会报超时,因此只能设为587blog

相关文章
相关标签/搜索