简体   繁体   English

SMTP服务器无法从托管c#asp.net发送电子邮件

[英]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. 我写了一个基于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. SMTP服务器需要安全连接或客户端未经过身份验证。 The server response was: 5.5.1 Authentication Required. 服务器响应为:5.5.1需要身份验证。

I've tried to change port to 465, but then it will be get me an tcp_ip error on the hosting. 我试图将端口更改为465,但是它会在主机上收到tcp_ip错误。 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. 我已经尝试使用谷歌搜索并找到了关于2级注册的内容,但是不明白我需要在我的方法中实现它。

I'm using arvixe hosting. 我正在使用arvixe托管。 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. 请手动登录您的Gmail帐户,并在gmail设置下允许您托管的IP。这就是为什么我认为它在您的本地工作完美。 Same happened with me and after doing this there was no problem. 同样的事情发生在我身上,这样做没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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