Home > Backend Development > C++ > How Can I Send Emails from My Gmail Account Using .NET?

How Can I Send Emails from My Gmail Account Using .NET?

DDD
Release: 2025-02-02 23:48:17
Original
412 people have browsed it

How Can I Send Emails from My Gmail Account Using .NET?

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>
Copy after login

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):

  • 2-Step Verification Enabled: Generate an App Password. Go to App Passwords, select "Mail" as the app and "Windows Computer" as the device. Use this generated password as your fromPassword.
  • 2-Step Verification Disabled: While possible to use your regular Gmail password, enabling "Less secure app access" is strongly discouraged for security reasons. Using App Passwords is the recommended approach.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template