/// <summary>
/// 透過SMTP發送信件範例, 以GMail為例 For C#
/// </summary>
private void SendMail()
{
try
{
SmtpClient mailClient = new SmtpClient("smtp.gmail.com");
//Credentials登陸SMTP服務器的身份驗證
mailClient.Credentials = new NetworkCredential("user", "password");
//發件人地址
MailMessage message = new MailMessage(new MailAddress("Sender<Sender@gmail.com>"));
//收件人地址
message.To.Add(new MailAddress("Recever<Recever@gmail.com>"));
//可以添加多個副本收件人(CC)
message.CC.Add(new MailAddress("Sender<Sender@gmail.com>"));
//可以添加多個密件副本收件人(BCC)
message.Bcc.Add(new MailAddress("Sender<Sender@gmail.com>"));
//郵件內容
message.Body = "Mail Test In C#!";
//郵件主題
message.Subject = "C# SMTP Mail Send!!";
//附件
message.Attachments.Add(new Attachment(@"C:\Attachment.txt")); //添加附件
mailClient.Send(message);
MessageBox.Show("寄件成功");
}
catch (Exception ex)
{
MessageBox.Show("寄件失敗" + "\n\n問題敘述: " + ex.Message);
}
}
張貼留言