The send method of SMTP is always throwing an exception. The message is : Failure sending mail
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
The send method of SMTP is always throwing an exception. The message is : Failure sending mail
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
Share
Improve this question
edited Mar 25, 2011 at 9:46
Morten Anderson
2,32115 silver badges20 bronze badges
asked Mar 25, 2011 at 8:27
kumarkumar
191 gold badge1 silver badge3 bronze badges
4
- 1 Do you have a mail service running on your local machine? – Mariusz Commented Mar 25, 2011 at 8:36
- yes i did start ir before excuting it – kumar Commented Mar 25, 2011 at 8:39
- @O.D he has, it is "Failure sending mail". – painotpi Commented Mar 25, 2011 at 8:43
- 3 Can you post the full details (type and all properties including stack trace and innerException's type and all properties if it is not null), not just the message. – Richard Commented Mar 25, 2011 at 8:59
4 Answers
Reset to default 2Your 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
This code works with "smtp.google."
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.";
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.
Nothing wrong with your code - it's most likely your server or firewall
Try to use this in your web.config first:
<system>
<mailSettings>
<!-- Use this setting for development
<smtp deliveryMethod="Network">
<network host="mail.mydomain." port="25" />
</smtp>
-->
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="C:\Tmp"/>
</smtp>
</mailSettings>
</system>
This will copy all files into C:\Tmp. You can just instantiate the class like this:
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.
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.
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.";
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;
}