Sending Emails via Gmail with .NET: A Simplified Approach
Tired of relying on your web host for email delivery? Use your Gmail account for more personalized messaging. System.Net.Mail
offers a superior alternative to the outdated System.Web.Mail
, simplifying SSL configuration. The following code snippet demonstrates how to effortlessly send emails from your Gmail account using .NET:
<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 Security Settings: Proper configuration is crucial for successful email delivery. Check your Google Account's security settings (Security > Signing in to Google > 2-Step Verification):
fromPassword
.This streamlined method ensures secure and reliable email sending directly from your Gmail account within your .NET applications.
The above is the detailed content of How Can I Send Emails from My Gmail Account Using .NET?. For more information, please follow other related articles on the PHP Chinese website!