>通过.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中文网其他相关文章!