SSL SMTP dengan .NET Framework
Menghantar e-mel melalui pelayan SMTP SSL menggunakan Rangka Kerja .NET boleh dicapai dengan mengkonfigurasi klien SMTP sewajarnya. Berikut ialah panduan langkah demi langkah:
1. Buat Instance SmtpClient:
System.Net.Mail.SmtpClient _SmtpServer = new System.Net.Mail.SmtpClient();
2. Nyatakan Hos dan Port Pelayan:
_SmtpServer.Host = "smtp.yourserver.com"; _SmtpServer.Port = 465;
3. Dayakan SSL:
_SmtpServer.EnableSsl = true;
4. Tetapkan Bukti Kelayakan (Pilihan):
_SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
5. Konfigurasi Hantar Timout:
_SmtpServer.Timeout = 5000;
6. Tetapkan UseDefaultCredentials kepada Palsu:
_SmtpServer.UseDefaultCredentials = false;
7. Cipta Mesej Mel:
MailMessage mail = new MailMessage(); mail.From = new MailAddress(from); mail.To.Add(to); mail.Subject = subject; mail.Body = content; mail.IsBodyHtml = useHtml;
8. Hantar E-mel:
_SmtpServer.Send(mail);
Contoh dengan Pelayan SMTP Gmail:
using System.Web.Mail; using System; public class MailSender { public static bool SendEmail(string pGmailEmail, string pGmailPassword, string pTo, string pSubject, string pBody, MailFormat pFormat) { try { MailMessage myMail = new MailMessage(); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com"); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465"); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2"); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", pGmailEmail); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pGmailPassword); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true"); myMail.From = pGmailEmail; myMail.To = pTo; myMail.Subject = pSubject; myMail.BodyFormat = pFormat; myMail.Body = pBody; SmtpMail.SmtpServer = "smtp.gmail.com:465"; SmtpMail.Send(myMail); return true; } catch (Exception ex) { throw; } } }
Atas ialah kandungan terperinci Bagaimana untuk Mengkonfigurasi Penghantaran E-mel SMTP SSL dengan Rangka Kerja .NET?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!