javascript 发送电子邮件的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5430097/
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
Problem with sending email
提问by kumar
The send method of SMTP is always throwing an exception. The message is : Failure sending mail
SMTP 的 send 方法总是抛出异常。消息是:发送邮件失败
Here is my code:
这是我的代码:
MailMessage mm = new MailMessage();
mm.To.Add("[email protected]");
mm.From = new MailAddress("[email protected]");
mm.Subject = "Ant Subject";
mm.Body = "Body Cha MEssag here ";
SmtpClient ss = new SmtpClient("localhost");
ss.EnableSsl = false;
try
{
**ss.Send(mm);**
Result.Text = "Message Sent";
Result.ForeColor = System.Drawing.Color.Green;
}
catch (SmtpException ex)
{
Result.Text = "Message Not Sent : \n\n " + ex.Message;
Result.ForeColor = System.Drawing.Color.Red;
}
I also tried using
我也尝试使用
ss.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
Now it doesn't throw any exception, it executes fine but receiver does not receive any mail in his inbox.
现在它没有抛出任何异常,它执行得很好,但接收者在他的收件箱中没有收到任何邮件。
How can I fix this?
我怎样才能解决这个问题?
Edit - This is the stack trace im getting
编辑 - 这是我得到的堆栈跟踪
Message=Failure sending mail. Source=System StackTrace: at System.Net.Mail.SmtpClient.Send
(MailMessage message) at WebApplication1._Default.Page_Load(Object sender, EventArgs e) in D:\Emcure-
Kumar\Work\Not in Use\WebApplication1\WebApplication1\Default.aspx.cs:line 30 InnerException:
System.Net.WebException Message=Unable to connect to the remote server Source=System StackTrace: –
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async,
IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at
System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout,
GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject,
GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject,
GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at
System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)
InnerException: System.Net.Sockets.SocketException Message=No connection could be made because the
target machine actively refused it 127.0.0.1:25 Source=System ErrorCode=10061 NativeErrorCode=10061
回答by Morten Anderson
Your execption should contain more info than "Failure sending mail"
您的执行应该包含比“发送邮件失败”更多的信息
For debugging look at this link for details on the Exeptions thrown by SmtpClient.Send Method - > SmtpClient.Send Method
有关调试,请查看此链接以了解有关 SmtpClient.Send 方法抛出的异常的详细信息 - > SmtpClient.Send 方法
This code works with "smtp.google.com"
此代码适用于“smtp.google.com”
MailMessage mm = new MailMessage();
mm.From = new MailAddress("xx");
mm.To.Add("xx");
mm.Subject = "Ant Subject";
mm.Body = "Body Cha MEssag here ";
SmtpClient ss = new SmtpClient();
ss.Host="smtp.gmail.com";
ss.Port = 587;
ss.EnableSsl = true;
ss.Credentials = new System.Net.NetworkCredential("xx", "xx");
try
{
ss.Send(mm);
Label1.Text = "Message Sent";
Label1.ForeColor = System.Drawing.Color.Green;
}
catch (SmtpException ex)
{
Label1.Text = "Message Not Sent : \n\n " + ex.Message;
Label1.ForeColor = System.Drawing.Color.Red;
}
Google requires SSL enabled and port 587 to be the outgoing port. You need a google account as well for credentials.
Google 要求启用 SSL 并将端口 587 作为传出端口。您还需要一个谷歌帐户来获取凭据。
Nothing wrong with your code - it's most likely your server or firewall
您的代码没有问题-很可能是您的服务器或防火墙
回答by Remy
Try to use this in your web.config first:
首先尝试在您的 web.config 中使用它:
<system.net>
<mailSettings>
<!-- Use this setting for development
<smtp deliveryMethod="Network">
<network host="mail.mydomain.com" port="25" />
</smtp>
-->
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Tmp"/>
</smtp>
</mailSettings>
</system.net>
This will copy all files into C:\Tmp. You can just instantiate the class like this:
这会将所有文件复制到 C:\Tmp。您可以像这样实例化类:
SmtpClient client = new SmtpClient();
And the change the configuration in your web.config afterwards. Give it a try an let us know if this helps.
然后更改 web.config 中的配置。尝试一下,如果这有帮助,请告诉我们。
回答by Mauricio Cordeiro
I Had the same problem, and was my proxy, i dont know really, because my ie have a proxy with a good configuration, and i read that a C# app get the configuration of the OS proxy. I just change my conecction to some that dont have Proxy and run very well. All solution, 1st, 2nd and 3th. Maybe is something like proxy or firewall, or try your program in other PC to confirm. I hope that help you.
我有同样的问题,是我的代理,我真的不知道,因为我的 ie 有一个配置良好的代理,我读到一个 C# 应用程序获得了操作系统代理的配置。我只是将我的连接更改为一些没有代理并且运行良好的连接。所有解决方案,第 1、第 2 和第 3 种。也许是代理或防火墙之类的东西,或者在其他PC上尝试您的程序以确认。我希望对你有帮助。
回答by shruti
try this
试试这个
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
// System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
MailAddress fromAddress = new MailAddress(txt_name.Text, txt_to.Text);
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 25;
// msg.From = new System.Net.Mail.MailAddress("[email protected]");
message.From = fromAddress;
message.To.Add("[email protected]");
message.Body = txt_des.Text;
smtpClient.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "[email protected]";
NetworkCred.Password = "xtz";
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = NetworkCred;
smtpClient.Send(message);
lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed." + ex.Message;
}