使用.NET Framework 進行SSL SMTP
使用.NET Framework 透過電子郵件可以透過設定SMTP 用戶端來實現因此。這是分步指南:
1.創建SmtpClient 實例:
System.Net.Mail.SmtpClient _SmtpServer = new System.Net.Mail.SmtpClient();
2.指定服務器主機和端口:
_SmtpServer.Host = "smtp.yourserver.com"; _SmtpServer.Port = 465;
3.啟用SSL:
_SmtpServer.EnableSsl = true;
4.設定憑證(選用):
_SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
5.設定傳送逾時:
_SmtpServer.Timeout = 5000;
6.將UseDefaultCredentials 設定為False:
_SmtpServer.UseDefaultCredentials = false;
郵件消息:
MailMessage mail = new MailMessage(); mail.From = new MailAddress(from); mail.To.Add(to); mail.Subject = subject; mail.Body = content; mail.IsBodyHtml = useHtml;
8.發送電子郵件:
_SmtpServer.Send(mail);
Gmail 的SMTP 服務器示例:
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; } } }
以上是如何使用 .NET Framework 設定 SSL SMTP 電子郵件傳送?的詳細內容。更多資訊請關注PHP中文網其他相關文章!