skip to Main Content

During the time I used iis express, I did not have any problems sending mail.
After uploading to the host, loading goes on while testing, and then returns an http 500 error. Creates a user when looking at the database but gives an error because it cannot send mail. what should I do?
In the meantime, I use asp.net core, .net version 5.0, the host I use is godaddy windows host

these were my email codes

using System.Threading.Tasks;

namespace gamesellMVC.EmailServices
{
    public interface IEmailSender
    {
        Task SendEmailAsync(string email, string subject, string htmlMessage);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;

namespace gamesellMVC.EmailServices
{
    public class SmtpEmailSender : IEmailSender
    {
        private string _host;
        private int _port;
        private bool _enableSSL;
        private string _username;
        private string _password;
        public SmtpEmailSender(string host, int port, bool enableSSL, string username, string password)
        {
            this._host = host;
            this._port = port;
            this._enableSSL = enableSSL;
            this._username = username;
            this._password = password;
        }
        public Task SendEmailAsync(string email, string subject, string htmlMessage)
        {
            var client = new SmtpClient(this._host, this._port)
            {
                Credentials = new NetworkCredential(_username, _password),
                EnableSsl = this._enableSSL
            };
            return client.SendMailAsync(
                new MailMessage(this._username, email, subject, htmlMessage)
                {
                    IsBodyHtml = true
                });
        }
    }
}

my startup codes


services.AddScoped<IEmailSender, SmtpEmailSender>(i =>
                        new SmtpEmailSender(
                            _configuration["EmailSender:Host"],
                            _configuration.GetValue<int>("EmailSender:Port"),
                            _configuration.GetValue<bool>("EmailSender:EnableSSL"),
                            _configuration["EmailSender:UserName"],
                            _configuration["EmailSender:Password"]
                            ));

my appsetting.json codes


"EmailSender": {
    "Host": "smtp.office365.com",
    "Port": 587,
    "EnableSSL": true,
    "UserName": "my microsft email",
    "Password": "my microsft password"
  },

and the code section I used to send the mail confirmation

[AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> Register(RegisterModelF model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var user = new User()
            {
                UserName = model.NickName,
                Email = model.Email,
                Dob = model.Dob
            };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, "Customer");
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                var url = Url.Action("ConfirmEmail", "Account", new
                {
                    userId = user.Id,
                    token = code
                });

                await _emailSender.SendEmailAsync(model.Email, "Hesabı təstiqləyin",
                    "<!DOCTYPE html>" +
                    "<html>" +
                    "<body style="background-color:#ff7f26; text-align:center;">" +
                    "<h2 style="color:#051a80;">Confirm your mail</h2>" +
                    $"<label style="color:orange;font-size:100px;border:5px dotted;"><a href='https://localhost:44348{url}'>Confirm</a></label>" +
                    "</body>" +
                    "</html>"
                    );

                return RedirectToAction("Login", "Account");

            }

            ModelState.AddModelError("", "Error");
            return View(model);
        }

2

Answers


  1. Chosen as BEST ANSWER

    After learning that the code I wrote did not work on this host, I found a new short code and there was no problem using it. I'm adding a code here that might be useful to someone

    using (MailMessage mm = new MailMessage(" from @mail.com ", " to @mail.com "))
                    {
                        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                        mm.Subject = " Your Title ";
                        mm.Body = "<!DOCTYPE html>" +  // your shot message or like my html page
                        "<html>" +
                        "<body style="background-color:#ff7f26; text-align:center;">" +
                        "<h2 style="color:#051a80;">Confirm your mail</h2>" +
                        $"<label style="color:orange;font-size:100px;border:5px dotted;"><a href='https://mylink{url}'>Confirm</a></label>" +
                        "</body>" +
                        "</html>";
                        mm.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = " smtp link ";
                        smtp.EnableSsl = true; // (true or false)
                        NetworkCredential NetworkCred = new NetworkCredential(" from @mail.com ", " from mail's password");
                        smtp.UseDefaultCredentials = true; // (true or false)
                        smtp.Credentials = NetworkCred;
                        smtp.Port = 25; // (25,465,587 or ...)
                        smtp.Send(mm);
                    }
    

  2. Have you contacted their support team about the email settings that you need to use on your code?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search