Home > Backend Development > C++ > How Can I Send Emails Through SSL SMTP Server Using the .NET Framework?

How Can I Send Emails Through SSL SMTP Server Using the .NET Framework?

DDD
Release: 2024-10-30 20:53:30
Original
864 people have browsed it

How Can I Send Emails Through SSL SMTP Server Using the .NET Framework?

How Can I Send Emails Through SSL SMTP with the .NET Framework?

To send emails through an SSL SMTP server on port 465 using the .NET Framework, utilize the following code snippet, which demonstrates how to send an email using GMail's SSL/465 configuration:

<code class="csharp">using System.Web.Mail;
using System;

public class MailSender
{
    public static bool SendEmail(
        string pGmailEmail, 
        string pGmailPassword, 
        string pTo, 
        string pSubject,
        string pBody, 
        System.Web.Mail.MailFormat pFormat,
        string pAttachmentPath)
    {
        try
        {
            System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                              "smtp.gmail.com");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                              "465");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                              "2");

            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1");

            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                    pGmailEmail);
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                     pGmailPassword);
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                     "true");
            myMail.From = pGmailEmail;
            myMail.To = pTo;
            myMail.Subject = pSubject;
            myMail.BodyFormat = pFormat;
            myMail.Body = pBody;
            if (pAttachmentPath.Trim() != "")
            {
                MailAttachment MyAttachment = 
                        new MailAttachment(pAttachmentPath);
                myMail.Attachments.Add(MyAttachment);
                myMail.Priority = System.Web.Mail.MailPriority.High;
            }

            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(myMail);
            return true;
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}</code>
Copy after login

By setting the appropriate field values, including SMTP host, port, and TLS settings, you can configure the email setup for various SMTP providers. Note that minor adjustments may be necessary to ensure compatibility with different providers.

The above is the detailed content of How Can I Send Emails Through SSL SMTP Server Using the .NET Framework?. 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