依據要使用的Mail服務更改smtpAddress、portNumber以及enableSSL三個變數
主機名稱 |
SMTP Address |
Port |
SSL |
Yahoo! |
smtp.mail.yahoo.com |
587 |
True |
GMail |
smtp.gmail.com |
587 |
True |
Hotmail |
smtp.live.com |
587 |
True |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| using System.Net; using System.Net.Mail;
public void SendEmail() { string smtpAddress = "smtp.mail.yahoo.com"; int portNumber = 587; bool enableSSL = true; string emailFrom = "email@gmail.com"; string password = "abcdefg"; string emailTo = "someone@domain.com"; string subject = "Hello"; string body = "Hello, I'm just writing this to say Hi!"; using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress(emailFrom); mail.To.Add(emailTo); mail.Subject = subject; mail.Body = body; mail.IsBodyHtml = false; mail.Attachments.Add(new Attachment("C:\\SomeFile.txt")); mail.Attachments.Add(new Attachment("C:\\SomeZip.zip")); using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber)) { smtp.Credentials = new NetworkCredential(emailFrom, password); smtp.EnableSsl = enableSSL; smtp.Send(mail); } } }
|