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

相關文章
相關標籤/搜索