>通過.net通過gmail發送電子郵件:一種簡化的方法
>厭倦了依靠您的網絡主機進行電子郵件發送? 使用您的Gmail帳戶進行更多個性化的消息傳遞。 System.Net.Mail
>提供了比過時的System.Web.Mail
的優越替代方案,簡化了SSL配置。以下代碼片段演示瞭如何使用.NET毫不費力地從您的Gmail帳戶發送電子郵件:
<code class="language-csharp">using System.Net; using System.Net.Mail; // Replace with your actual credentials var fromAddress = new MailAddress("[email protected]", "Your Name"); var toAddress = new MailAddress("[email protected]", "Recipient Name"); string fromPassword = "Your Gmail App Password"; // See instructions below string subject = "Email Subject"; string body = "Email Body"; var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body }) { smtp.Send(message); }</code>
gmail安全設置:正確的配置對於成功的電子郵件傳遞至關重要。 檢查您的Google帳戶的安全設置(安全&gt;登錄到Google&GT; 2步驗證):
fromPassword
以上是如何使用.NET從Gmail帳戶發送電子郵件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!