Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMTP server can't send emails from hosting c# asp.net

Good day to everyone. I've writen a project based on asp.net mvc3. Part of project is based on sending emails from my application.

public void SendEmail(string address, string subject, string message, int id)
{
        string email = "[email protected]";
        string password = "somepassword";

        var loginInfo = new NetworkCredential(email, password);
        var msg = new MailMessage();
        var smtpClient = new SmtpClient("smtp.gmail.com", 587);

        msg.From = new MailAddress(email);
        msg.To.Add(new MailAddress(address));
        msg.Subject = subject;
        msg.Body = message;
        msg.IsBodyHtml = true;
        msg.Attachments.Add(new Attachment(Server.MapPath("~/Content/StudentPdf/student" + id + ".pdf")));

        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = loginInfo;
        smtpClient.Send(msg);

}

This code works locally, perfectly sending emails. But when I upload this to the hosting, it causes an error

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

I've tried to change port to 465, but then it will be get me an tcp_ip error on the hosting. And one more: and when users try to send emails from this mailbox google tell me that suspicious activity on the application. It's because my hosting in one country and I am in another country.

I have no idea what I have to do next. I've tried googling and found something about 2 level registration, but don't understand how I need implement it in my method.

I'm using arvixe hosting. Maybe others have the same problems?

like image 536
BorHunter Avatar asked Sep 03 '13 13:09

BorHunter


1 Answers

Please login into your gmail account manually and allow the IP of your hosting under gmail settings.Thats why i think its working perfectly on your local. Same happened with me and after doing this there was no problem.

like image 156
user1512186 Avatar answered Oct 21 '22 05:10

user1512186