Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

c# - SMTP Email and ERR_CONNECTION_RESET Error in Angular Project

Hi when i use the below code in angular api project my requests are not getting saved and i am gettign this following error. And when i comment the code to send mail everything is working fine

ERROR in console

 polyfills POST net::ERR_CONNECTION_RESET

And code to send mail

 public async Task SendEmail(string email, string subject, string firstName, string contact)
    {
        try
        {
            string message = BuildMessageBody(firstName, email, contact);

            using (var client = new SmtpClient())
            {
                var networkCredential = new NetworkCredential
                {
                    UserName = _configuration["Email:Email"],
                    Password = _configuration["Email:Password"]
                };

                client.UseDefaultCredentials = false;
                client.Credentials = networkCredential;
                client.Host = _configuration["Email:Host"];
                client.Port = int.Parse(_configuration["Email:Port"]);
                client.EnableSsl = true;

                using (var emailMessage = new MailMessage())
                {
                    emailMessage.To.Add(new MailAddress(email));
                    emailMessage.CC.Add(new MailAddress("my cc adddress"));
                    emailMessage.From = new MailAddress(_configuration["Email:Email"]);
                    emailMessage.Subject = subject;
                    emailMessage.Body = message;
                    emailMessage.IsBodyHtml = true;
                    emailMessage.BodyEncoding = System.Text.Encoding.UTF8;
                    emailMessage.SubjectEncoding = System.Text.Encoding.Default;
                    emailMessage.ReplyToList.Add(new MailAddress(_configuration["Email:Email"]));

                    client.SendCompleted += (s, e) => {
                        client.Dispose();
                        emailMessage.Dispose();
                    };
                    await client.SendMailAsync(emailMessage);
                }
            }
            await Task.CompletedTask;
        }
        catch (Exception ex)
        {
            _appLogger.CreateLog("SendEmail - " + ex.Message);
        }
    }

Any idea why this behaviour???

question from:https://stackoverflow.com/questions/66065617/smtp-email-and-err-connection-reset-error-in-angular-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...