Problem sending email

The SMTP send method always throws an exception. Message: Mail sending failure

Here is my code:

MailMessage mm = new MailMessage(); mm.To.Add(" Yuvraj.dhot@xxxxx.co.in "); mm.From = new MailAddress(" Kumar.Chaudhari@xxxxx.co.in "); 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 he does not generate any exceptions, he carries out a fine, but the recipient does not receive mail in his inbox.

How can i fix this?

Edit is a stack trace receiving

  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 
+1
source share
4 answers

Your performance should contain more information than "Sending mail"

For debugging, check out this link for details on the Exceptions thrown by SmtpClient.Send Method - > SmtpClient.Send Method

This code works with "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, and port 587 is an outgoing port. For credentials you need a google account.

There is nothing wrong with the code - most likely your server or firewall

+2
source

I had the same problem and I was my proxy, I really don’t know, because my ie has a proxy with a good configuration, and I read that the C # application gets the OS proxy configuration. I just change my dedication to those who do not have a proxy server and work very well. All solutions, 1st, 2nd and 3rd. Perhaps this is something like a proxy server or a firewall, or try checking your program on another PC. Hope this helps you.

+1
source

Try using this in your web.config first:

  <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 to C: \ Tmp. You can simply instantiate the class as follows:

 SmtpClient client = new SmtpClient(); 

And change the configuration in your web.config afterwards. Try letting us know if this helps.

0
source

try it

  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(" xyz@gmail.com "); message.From = fromAddress; message.To.Add(" xyz111@gmail.com "); message.Body = txt_des.Text; smtpClient.EnableSsl = true; System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(); NetworkCred.UserName = " xyz@gmail.com "; 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; } 
0
source

Source: https://habr.com/ru/post/1333160/


All Articles