using System.Net.Mail;
using System.Net;
class Email
{
public string From {get;set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public string Account { get; set; }
public string Password { get; set; }
public string Server { get; set; }
public string Port { get; set; }
public void Send_email()
{
//這邊先設定From,To,Subject,Body,Account,Password,Server,Port
SmtpClient theSmtpClient = new SmtpClient(Server);
NetworkCredential networkcredential = new NetworkCredential(Account, Password);//帳密
theSmtpClient.UseDefaultCredentials = false;
theSmtpClient.Credentials = networkcredential;
int Int_port = Convert.ToInt32(Port);
if (Int_port == 587)
{
theSmtpClient.EnableSsl = true;
}
else//不是587 就是 25
{
theSmtpClient.EnableSsl = false;
}
theSmtpClient.Port = Int_port;
try
{
theSmtpClient.Send(From, To, Subject, Body);
}
catch (Exception)
{
}
}
}
十分簡單,不過要注意的地方就是Port在有SSL加密認證的環境下要用587(ex:gmail),沒有的話則用25即可,然後Using要加入net / net.mail
End