खोज…


परिचय

वर्तमान में नेट कोर की तरह ईमेल भेजने के लिए समर्थन शामिल नहीं करता System.Net.Mail नेट से। MailKit प्रोजेक्ट (जो कि नूगेट पर उपलब्ध है) इस उद्देश्य के लिए एक अच्छा पुस्तकालय है।

नौगट पैकेज स्थापित करना

Install-Package MailKit

ईमेल भेजने के लिए सरल कार्यान्वयन

using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Text;
using System.Threading.Tasks;

namespace Project.Services
{
    /// Using a static class to store sensitive credentials
    /// for simplicity. Ideally these should be stored in
    /// configuration files
    public static class Constants
    {
        public static string SenderName => "<sender_name>";
        public static string SenderEmail => "<sender_email>";
        public static string EmailPassword => "email_password";
        public static string SmtpHost => "<smtp_host>";
        public static int SmtpPort => "smtp_port";
    }
    public class EmailService : IEmailSender
    {
        public Task SendEmailAsync(string recipientEmail, string subject, string message)
        {
            MimeMessage mimeMessage = new MimeMessage();
            mimeMessage.From.Add(new MailboxAddress(Constants.SenderName, Constants.SenderEmail));
            mimeMessage.To.Add(new MailboxAddress("", recipientEmail));
            mimeMessage.Subject = subject;

            mimeMessage.Body = new TextPart(TextFormat.Html)
            {
                Text = message,
            };

            using (var client = new SmtpClient())
            {

                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                client.Connect(Constants.SmtpHost, Constants.SmtpPort, false);    

                client.AuthenticationMechanisms.Remove("XOAUTH2");

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate(Constants.SenderEmail, Constants.EmailPassword);

                client.Send(mimeMessage);

                client.Disconnect(true);
                return Task.FromResult(0);
            }
        }        
    }       
    
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow