2012-12-24 153 views
0

我想通过应用程序在asp.net使用C#发送电子邮件。我搜索了很多,主要是发现下面的代码使用asp.net C#中发送电子邮件:如何发送电子邮件在asp.net使用C#到任何电子邮件地址使用Gmail地址

 MailMessage objEmail = new MailMessage(); 

     objEmail.From = new MailAddress(txtFrom.Text); 
     objEmail.To.Add(txtTo.Text); 
     objEmail.CC.Add(txtCC.Text); 
     objEmail.Bcc.Add(txtBCC.Text); 
     objEmail.Subject = txtSubject.Text; 

     try 
     { 

      SmtpClient mail = new SmtpClient(); 

      mail.EnableSsl = true; 
      mail.DeliveryMethod = SmtpDeliveryMethod.Network; 
      mail.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text); 
      mail.Timeout = 20000; 

      mail.UseDefaultCredentials = false; 

      mail.Host = "smtp.gmail.com"; 
      mail.Port = 587; 

      mail.Send(objEmail); 

      Response.Write("Your Email has been sent sucessfully - Thank You"); 

     } 
     catch (Exception exc) 
     { 
      Response.Write("Send failure due to : <br />" + exc.ToString()); 
     } 

,而总是我收到以下错误:

"System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)"

+0

你需要找到一个SMTP服务器是愿意送你。 – SLaks

+0

http://stackoverflow.com/questions/14024715/how-to-send-email-in-asp-net-using-c-sharp-to-any-email-address-using-gmail-addr – 2014-03-29 14:06:44

回答

0

你的代码看起来很合理,所以你需要弄清楚是什么阻止它的工作。

你能消除防火墙问题的可能性吗?大多数中型或大型组织倾向于阻止端口25,465和587 - 他们根本不希望任何未经授权的邮件外出。如果您怀疑这可能是这种情况,您可以尝试从网络外部(即您的家用机器),尽管有些ISP也会阻塞端口。

如果防火墙未阻止您的连接,请验证您的凭据是否有效 - 可以通过Thunderbird,Outlook或同等方式发送。尝试使用未加密的邮件设置测试 - 根目录通过垃圾邮件文件夹,将一些垃圾邮件粘贴到垃圾邮件或类似内容中,然后查找开放中继。另外,还有一些商业电子邮件提供商支持未加密的电子邮件。

+0

我的应用程序正在我的家用机器上运行,我试图通过禁用Windows防火墙发送eamil,但那不提供解决方案。我通过gmail直接访问我的gmail帐户。但是当我尝试为我的gmail帐户配置Outlook时,这并未建立,并且在测试连接时显示以下错误:“登录到传入邮件服务器(POP3):Outlook无法连接到您的传入(POP3)电子邮件服务器如果仍然收到此消息,请与您的服务器管理员或Internet服务提供商(ISP)联系。“ –

+0

发送测试电子邮件:无法发送邮件。请确认您的电子邮件地址帐户属性服务器响应:530 5.7.0必须首先发出STARTTLS命令l5sm48012964wia.10 –

+0

您是否将Outlook配置为使用TLS发送?如果是这样,那么它会显示哟你无法创建从你的机器到谷歌的安全连接。可能你的ISP阻止了连接,或者你的机器上有其他东西阻止了安全连接。 – chris

3

您正在尝试使用Gmail的具有不属于Gmail的电子邮件地址的SMTP服务器(根据帖子的标题)。您需要更新主机和端口详细信息以适应您的电子邮件提供商的SMTP详细信息。

+0

我已修改我的问题是消除歧义。现在请查看帖子,我正在使用Gmail地址发送电子邮件,但得到了帖子中的错误。 –

+0

我的帖子中看不到任何错误。可能是防火墙/环境问题。 – keyboardP

+0

我试图通过禁用防火墙来发送电子邮件,但这没用。任何想法,我失去了什么关键点:( –

0

你可以看看这个..简单的代码和它的作品的Gmail帐户...

try 
    { 

     MailMessage msg = new MailMessage(); 
     msg.From = new MailAddress(TextFRM.Text); 
     msg.To.Add(TextTO.Text); 
     msg.Subject = TextSUB.Text; 
     msg.Body = TextMSG.Text; 


     SmtpClient sc = new SmtpClient("smtp.gmail.com"); 
     sc.Port = 25; 
     sc.Credentials = new NetworkCredential(TextFRM.Text, TextPSD.Text); 
     sc.EnableSsl = true; 
     sc.Send(msg); 
     Response.Write("Mail Send"); 
    } 

    catch (Exception ex) 
    { 

     Response.Write(ex.Message); 

    } 
相关问题