从 gmail SMTP C# 连接超时发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15555389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending mail from gmail SMTP C# Connection Timeout
提问by Bernice
I have been trying to send an email via C# from a gmail account for account registration for my website.
我一直在尝试从 Gmail 帐户通过 C# 发送电子邮件,以便为我的网站注册帐户。
I have tried several ways however the same exception continues to pop up: System.Net.Mail.Smtp Exception - Connection has timed out.
我已经尝试了几种方法,但是同样的异常继续弹出: System.Net.Mail.Smtp 异常 - 连接超时。
This is what I inluded in my Web.config file:
这是我在 Web.config 文件中包含的内容:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network"
from="Writely <[email protected]>">
<network host="smtp.gmail.com"
port="465"
enableSsl="true"
defaultCredentials="false"
userName="[email protected]"
password="******" />
</smtp>
</mailSettings>
</system.net>
where writely is the name of my website, and [email protected] is the account I wish to send an email from.
其中 writely 是我网站的名称,[email protected] 是我希望从中发送电子邮件的帐户。
Then in my Account Controller when I connect with my database and save the user in my table, I am creating my MailMessage object and attempting to same the mail by:
然后在我的帐户控制器中,当我连接到我的数据库并将用户保存在我的表中时,我正在创建我的 MailMessage 对象并尝试通过以下方式相同的邮件:
using (DBConnection conn = new DBConnection())
{
conn.UserInfoes.Add(userInfo);
conn.SaveChanges();
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Welcome to Writely";
mail.Body = "Test content";
SmtpClient smtp = new SmtpClient();
smtp.Send(mail);
}
Am I missing something or doing something wrong? I read that this is the good way to do this in some other question on stack overflow so I really don't know what's the problem here.
我是否遗漏了什么或做错了什么?我读到这是在堆栈溢出的其他问题中执行此操作的好方法,所以我真的不知道这里的问题是什么。
Thanks for your help :)
谢谢你的帮助 :)
采纳答案by Jason Watkins
You need to tell the SmtpClient what settings to use. It does not automatically read this information from the Web.Config file.
您需要告诉 SmtpClient 使用什么设置。它不会自动从 Web.Config 文件中读取此信息。
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 465);
smtp.Credentials = new NetworkCredential("[email protected]", "***");
smtp.EnableSsl = true;
smtp.Send(mail);
回答by Crudler
gmail requires authentication:
gmail 需要身份验证:
Outgoing Mail (SMTP) Server
requires TLS or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
so what i did is
所以我所做的是
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("[email protected]", "mypwd"),
EnableSsl = true
};
client.Send("[email protected]", "[email protected]", "Welcome to Writely", "Test content");
回答by Auguste
I had the exact same problem and it's resolved after switching the port number from 465 to 587.
我遇到了完全相同的问题,在将端口号从 465 切换到 587 后解决了。
I had the problem on "email confirmation", "password recovery", and "sending email" and now all 3 problems are resolved :).
我在“电子邮件确认”、“密码恢复”和“发送电子邮件”方面遇到了问题,现在所有 3 个问题都解决了 :)。
I know it's a pretty old post, but I usually use the existing posts to find answers instead of asking for new questions.
我知道这是一篇很老的帖子,但我通常使用现有的帖子来寻找答案,而不是提出新问题。
Thank you all for all your helps.
谢谢大家的帮助。
回答by dinhokz
As I have already answered here.
正如我已经在这里回答的那样。
This problem can also be caused by a security configuration in you gmail account.
此问题也可能是由您的 gmail 帐户中的安全配置引起的。
The correct port is 587, but to authenticate you need to allow access from less secure apps in your gmail account. Try it here
正确的端口是 587,但要进行身份验证,您需要允许从 Gmail 帐户中安全性较低的应用程序访问。在这里试试
It worked for me, hope it helps..
它对我有用,希望它有帮助..
回答by Rafael Estev?o
Example in asp.net web forms/sharepoint
asp.net web 表单/sharepoint 中的示例
StringBuilder Body = new StringBuilder();
Body.Append("Your text");
String FromEmail = "you email";
String DisplayNameFromEmailMedico = "display when you receive email";
MailMessage message = new MailMessage();
message.From = new MailAddress(FromEmail, DisplayNameFromEmailMedico);
message.To.Add(new MailAddress("[email protected]"));
message.Subject = "subject that print in email";
message.IsBodyHtml = true;
message.Body = Body.ToString();
SmtpClient client = new SmtpClient();
NetworkCredential myCreds = new NetworkCredential("[email protected]", "key from email smtp", "");
client.EnableSsl = true;
client.Credentials = myCreds;
client.Send(message);