如何使用 .NET Framework 透過 SSL SMTP 發送電子郵件?
使用連接埠465 透過SSL SMTP 伺服器傳送電子郵件在.NET Framework 中,使用下列程式碼片段,示範如何使用GMail 的SSL/465 設定傳送電子郵件:
<code class="csharp">using System.Web.Mail; using System; public class MailSender { public static bool SendEmail( string pGmailEmail, string pGmailPassword, string pTo, string pSubject, string pBody, System.Web.Mail.MailFormat pFormat, string pAttachmentPath) { try { System.Web.Mail.MailMessage myMail = new System.Web.Mail.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; if (pAttachmentPath.Trim() != "") { MailAttachment MyAttachment = new MailAttachment(pAttachmentPath); myMail.Attachments.Add(MyAttachment); myMail.Priority = System.Web.Mail.MailPriority.High; } System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465"; System.Web.Mail.SmtpMail.Send(myMail); return true; } catch (Exception ex) { throw; } } }</code>
透過設定適當的欄位值,包括SMTP 主機、連接埠和TLS 設定,您可以為各種SMTP 提供者配置電子郵件設定。請注意,可能需要進行細微調整以確保與不同供應商的相容性。
以上是如何使用.NET Framework透過SSL SMTP伺服器發送電子郵件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!