【发布时间】:2016-05-28 14:52:02
【问题描述】:
我在使用我的 gmail 帐户通过 ASP.NET 应用程序发送简单电子邮件时遇到问题。我已经尽我所能寻求帮助,但由于某种原因,它一直给我一个错误,并且没有发送任何电子邮件。
这是我设置为在按下按钮时触发的代码:
protected void SubmitButton_Click(object sender, EventArgs e)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(new System.Net.Mail.MailAddress("an email address"));
mail.From = new System.Net.Mail.MailAddress("From Email Address");
mail.Subject = "PDMS New User Request";
mail.IsBodyHtml = true;
mail.Body = "A new user has requested access" + "\n\n"
+ "Name: " + inputFName.Text.ToString() + " " + inputLName.Text.ToString()
+ "\n"
+ "Organization: " + inputOrg.Text.ToString()
+ "\n"
+ "Email: " + inputEmail.Text.ToString()
+ "\n\n"
+ "Please contact them with their login information"
+ "-PDMS System Message";
mail.IsBodyHtml = true;
System.Net.Mail.SmtpClient systemEmail = new System.Net.Mail.SmtpClient();
systemEmail.UseDefaultCredentials = false;
systemEmail.Credentials=new System.Net.NetworkCredential("From email address", "From Email Addresses' password");
systemEmail.Port = 587 ;
systemEmail.Host="smtp.gmail.com";
systemEmail.EnableSsl=true;
systemEmail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
systemEmail.Send(mail);
Label1.Visible = true;
Label2.Visible = false;
}
catch (Exception ex)
{
Label2.Text = "Email could not be sent due to errors";
}
我错过了什么?我似乎无法让此代码实际发送任何内容 - Gmail 中是否有我需要先配置的设置?
【问题讨论】:
-
你遇到了什么错误?
-
阅读错误信息。
-
异常应该告诉你你的问题是什么。
-
删除异常内的标签更多参考检查我的解决方案
-
我发生的异常是“System.IO.IOException:无法从传输连接读取数据:net_io_connectionclosed。”。我不确定问题是什么。我没有启用两因素身份验证,我启用了不太安全的应用程序,并且我的用户名和密码是正确的。
标签: c# asp.net email smtp smtp-auth