>백엔드 개발 >C++ >.NET Framework를 사용하여 SSL SMTP 이메일 전송을 구성하는 방법은 무엇입니까?

.NET Framework를 사용하여 SSL SMTP 이메일 전송을 구성하는 방법은 무엇입니까?

DDD
DDD원래의
2024-10-30 01:02:041008검색

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

.NET Framework를 사용한 SSL SMTP

SMTP 클라이언트를 구성하면 .NET Framework를 사용하여 SSL SMTP 서버를 통해 이메일을 보낼 수 있습니다. 따라서. 단계별 가이드는 다음과 같습니다.

1. SmtpClient 인스턴스 생성:

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

2. 서버 호스트 및 포트 지정:

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

3. SSL 활성화:

_SmtpServer.EnableSsl = true;

4. 자격 증명 설정(선택 사항):

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

5. 전송 시간 초과 구성:

_SmtpServer.Timeout = 5000;

6. UseDefaultCredentials를 False로 설정:

_SmtpServer.UseDefaultCredentials = false;

7. 메일 메시지 만들기:

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

8. 이메일 보내기:

_SmtpServer.Send(mail);

Gmail의 SMTP 서버 예:

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;
        }
    }
}

위 내용은 .NET Framework를 사용하여 SSL SMTP 이메일 전송을 구성하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.