部署到主机后,无法通过C#和gmail发送邮件 [英] Sending mail through C# with gmail is not working after deploying to host
问题描述
我正在尝试通过Gmail发送邮件。在localhost上进行测试时,我已经成功发送了邮件,但是当我将其上传到Web主机时,此操作不起作用。我看到这种类型的错误:
I am trying to send mail through Gmail. I am sending mail successfully when I am testing on localhost, but this does not work when I upload it to a web host. I am seeing this type of error:
请求类型System.Net.Mail.SmtpPermission,System,Version = 4.0的许可。 0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089失败。
Request for the permission of type System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 failed.
每当我使用端口25时,都会出现以下错误:
Whenever I am using port 25 get this type of error below:
SMTP服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1需要身份验证
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
下面是我的发送电子邮件代码。
Below is my code of send email.
MailMessage mail = new MailMessage("host@gmail.com","User@gamil.com");
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.Subject = "Any String"
mail.Body = mailbody;
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("xyz@gmail.com","123");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
有什么解决方案吗?请向我建议!
Is there any solution? Please suggest to me!
推荐答案
编辑: OP添加了对回答此问题至关重要的其他信息,但是我会保留旧的答案,因为它仍然可以帮助某人
OP Added extra information crucial to answering this question, but I'm keeping the old answer around as it might still help someone
新答案:
此 StackOverflow问题已经回答了该问题
New Answer: This StackOverflow question already answered this question
OldAnswer:
作为此 StackOverflow答案已经回答,您将SMTP服务器上的端口更改为 587
而不是其默认值( 25
),这需要提升的权限,导致此错误的更改如下:
OldAnswer:
As this StackOverflow answer already answered, you changed the Port on the SMTP Server to 587
instead of its default (25
) and this requires elevated permissions causing this error change this:
SmtpServer.Port = 587;
为此:
SmtpServer.Port = 25;
它应该可以工作
>注意:使用SSL时,端口必须为 443
Note: When using SSL the port needs to be 443
这篇关于部署到主机后,无法通过C#和gmail发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!