0

I'm trying to send a notification email, to users with ASP.NET Core 5.0 razor.

I can't understand what have I done wrong so that I can't send the email?

This is what I have done so far:

 <form method="post">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
         <div class="form-group" >
            <input asp-for="SendEmail.AdminEmail" class="form-control" value="@User.Identity.Name" readonly/>
            <span asp-validation-for="SendEmail.AdminEmail" class="text-danger"></span>
        </div>
       
        <div class="form-group">
            <input asp-for="SendEmail.ClientsEmail" class="form-control" value="test@mail.com" />
            <span asp-validation-for="SendEmail.ClientsEmail" class="text-danger"></span>
        </div>
         <div class="form-group">
            <input asp-for="SendEmail.Subject" class="form-control" placeholder="Subject"/>
            <span asp-validation-for="SendEmail.Subject" class="text-danger"></span>
        </div>
        <div class="form-group">
            <textarea asp-for="SendEmail.BodyText" class="form-control" style="min-height:50vh;max-height:50vh;" ></textarea>
            <span asp-validation-for="SendEmail.BodyText" class="text-danger"></span>
        </div>
       <div class="form-group" style="text-align:center;">

                <button class="btn btn-sm btn-outline-info font-weight-bold" style="font-size: 110%;float:unset;min-width:100px;">
                    Send
                </button>
            </div>
    </form>

And the backend code :

public ActionResult OnPostAsync()
{
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("admin@mymail.com");
        msg.To.Add(new MailAddress("test@mymail.com"));
        msg.Subject = SendEmail.Subject;

        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("admin@mymail.com", "******");
        smtpClient.Credentials = credentials;
        smtpClient.EnableSsl = true;
        smtpClient.Send(msg);

        return RedirectToPage("./Index");
}

This is the error I am getting:

An unhandled exception occurred while processing the request. ExtendedSocketException: An attempt was made to access a socket in a way forbidden by its access permissions. [::ffff:142.250.145.108]:587

System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
SmtpException: Failure sending mail.

System.Net.Mail.SmtpClient.Send(MailMessage message)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Fillo
  • 141
  • 1
  • 10
  • If 587 is already an int, why are you covering it to an int? Not that it will solve your problem, it's just very strange. – mason Jul 12 '22 at 20:27
  • Is mymail.com controlled by Gmail? – mason Jul 12 '22 at 20:32
  • For gmail I believe you need to register your app with google: https://support.google.com/googleapi/answer/6158862?hl=en – pcalkins Jul 12 '22 at 21:48
  • @mason, I put a dummy email, for the purpose of showing some email info(that is not the real email) the real one is on Gmail. I tried to get the port info from the database, but is was red as a string, so converted it to int. – Fillo Jul 12 '22 at 21:50
  • you can try to use [smtp4dev](https://github.com/rnwood/smtp4dev) to isolate the problem if this is happening because of code or other external factor such as gmail. – CodingMytra Jul 13 '22 at 03:18

1 Answers1

0

Maybe it's related to settings of gmail. Not sure but to be able use smtp you need to enable some setting like "allow less secure apps" etc.

Also from address must be a gmail address AFAIK

You may check this answer: An attempt was made to access a socket in a way forbidden by its access permissions. Why?

Ozan BAYRAM
  • 2,780
  • 1
  • 28
  • 35