Heim >Backend-Entwicklung >C++ >Wie konfiguriere ich den SSL-SMTP-E-Mail-Versand mit dem .NET Framework?

Wie konfiguriere ich den SSL-SMTP-E-Mail-Versand mit dem .NET Framework?

DDD
DDDOriginal
2024-10-30 01:02:041023Durchsuche

How to Configure SSL SMTP Email Sending with the .NET Framework?

SSL-SMTP mit .NET Framework

Das Senden von E-Mails über einen SSL-SMTP-Server mit .NET Framework kann durch die Konfiguration des SMTP-Clients erreicht werden entsprechend. Hier ist eine Schritt-für-Schritt-Anleitung:

1. Erstellen Sie eine SmtpClient-Instanz:

System.Net.Mail.SmtpClient _SmtpServer = new System.Net.Mail.SmtpClient();

2. Geben Sie Serverhost und Port an:

_SmtpServer.Host = "smtp.yourserver.com";
_SmtpServer.Port = 465;

3. SSL aktivieren:

_SmtpServer.EnableSsl = true;

4. Anmeldeinformationen festlegen (optional):

_SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");

5. Sende-Timeout konfigurieren:

_SmtpServer.Timeout = 5000;

6. Setzen Sie UseDefaultCredentials auf False:

_SmtpServer.UseDefaultCredentials = false;

7. Erstellen Sie eine E-Mail-Nachricht:

MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
mail.To.Add(to);
mail.Subject = subject;
mail.Body = content;
mail.IsBodyHtml = useHtml;

8. Senden Sie die E-Mail:

_SmtpServer.Send(mail);

Beispiel mit dem SMTP-Server von Gmail:

using System.Web.Mail;
using System;

public class MailSender
{
    public static bool SendEmail(string pGmailEmail, string pGmailPassword, string pTo, string pSubject, string pBody, MailFormat pFormat)
    {
        try
        {
            MailMessage myMail = new 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;

            SmtpMail.SmtpServer = "smtp.gmail.com:465";
            SmtpMail.Send(myMail);

            return true;
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}

Das obige ist der detaillierte Inhalt vonWie konfiguriere ich den SSL-SMTP-E-Mail-Versand mit dem .NET Framework?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn