0
votes

I'm trying to add a new email confirmation method to my MVC site, so whenever a user registers he will receive a confirmation email. The problem is, that no matter what code I use (I've found some different options on GitHub) I always get the same error:

"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 66.102.1.108:587".

The code I used is:

using ATMProject.Models;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;

namespace ATMProject.Services
{
    public class SendEmailConfirmation
    {
        public string email { get; set; }
        public string fullname { get; set; }
        public string username { get; set; }
        public string random { get; set; }

        public SendEmailConfirmation(string Email, string Fullname, 
          string Username, string Random) 
        {
            email = Email;
            fullname = Fullname;
            username = Username;
            random = Random;
        }

        public void sendEmail ()
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new 
              System.Net.NetworkCredential("[email protected]", "123456");
            smtp.EnableSsl = true;
            MailMessage msg = new MailMessage();

            #region Msg
            msg.Subject = "Hello " + fullname;
            msg.Body = "Hello " + fullname + 
              "Thanks for Registering in Budgetize...Your Account Details are given below:";
            msg.Body += "<tr>";
            msg.Body += "<td>User Name :" + username + "</td>";
            msg.Body += "</tr>";
            msg.Body += "<tr>";
            msg.Body += "<td>Activation Number :" + random + "</td>";
            msg.Body += "</tr>";
            msg.Body += "<tr>";
            msg.Body += "<td>Thanking</td><td>Team Budgetize</td>";
            msg.Body += "</tr>";
            #endregion

            string toAddress = email; // Add Recepient address
            msg.To.Add(toAddress);
            string fromAddress = "\"Budgetize \" <[email protected]>";
            msg.From = new MailAddress(fromAddress);
            msg.IsBodyHtml = true;

            try {smtp.Send(msg);}
            catch (Exception ex) {throw;}
        }
    }
}
2
I hope that you haven't put your real passoword thereSteve

2 Answers

2
votes

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

2
votes

I found the solution! I've run the same code on a different computer (home computer) and it worked well. It seems like the port (587) was blocked in my work pc by another layer of security (firewall did not block it, neither did the antivirus). The IT Manager said he's blocking many ports with another layer, which I don't have access to. If the same bug occurs in your work pc - try running it on a different pc (that is not connected to the same network) and look if the code works.