Home >Backend Development >C++ >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:
<code class="language-csharp">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); }</code>
If your Google account is opened for two steps, please generate the application password to bypass it in the code.
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!