Home > Backend Development > C++ > How to Send Personalized Emails via Gmail using .NET?

How to Send Personalized Emails via Gmail using .NET?

Patricia Arquette
Release: 2025-02-03 00:16:10
Original
193 people have browsed it

How to Send Personalized Emails via Gmail using .NET?

Use .NET to send personalized emails through gmail

This article introduces how to use the .NET application to send personalized emails through the Gmail account. You can use system.net.mail naming space to implement this function.

The following code examples demonstrate how to use Gmail as a SMTP server to send email:

Important precautions:
using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("sender@gmail.com", "发件人姓名");
var toAddress = new MailAddress("recipient@example.com", "收件人姓名");
const string fromPassword = "我的Gmail密码";
const string subject = "邮件主题";
const string 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);
}
Copy after login

If your Google account is opened for two steps, please generate the application password to bypass it in the code.

    It is recommended not to enable the "Low Safety Application Access" option in the Google account security settings. Please use two steps to verify.

The above is the detailed content of How to Send Personalized Emails via Gmail using .NET?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template