简体   繁体   中英

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 = "emailname@gmail.com";
        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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM