【C#學習筆記】smtp發郵件

Dsp Tian發表於2017-08-27
using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("***@gmail.com");
            mail.To.Add(new MailAddress("***@sina.cn"));
            mail.Subject = "郵件標題";
            mail.SubjectEncoding = Encoding.UTF8;
            mail.Body = "郵件內容";
            mail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient(); 
            smtp.Host = "smtp.gmail.com";       //不同的郵箱這個不一樣
            smtp.Port = 587;                    //大部分郵箱為25,gmail為587
            smtp.Credentials = new NetworkCredential("賬號","密碼");    //gmail的賬號和密碼
            smtp.EnableSsl = true;               //其他郵箱不需要ssl
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;   //其他郵箱也不需要這個

            smtp.Send(mail);

            Console.Write("this work");
            Console.Read();
        }      
    }
}

 

相關文章