• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 12268
  • Last Modified:

SMTP - Failure sending mail - C#

I'm receiving the following error when trying to send an email using System.Net.Mail.  I'm using a remote SMTP server which is a Windows 2003 server.  I've added the machine which is sending the mail in the relay settings.  Port 25 is open in the firewall settings.

Here is the Error I receive:
System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 139.78.129.87:25 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) ---
End of inner exception stack trace --- 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) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.Button1_Click(Object sender, EventArgs e) in e:\Code\VPAF\DW\TestEmail2\Default.aspx.cs:line 39


Any suggestions on why the email is not being sent?
//create the mail message
        MailMessage mail = new MailMessage();
 
        //set the addresses
        mail.From = new MailAddress("fromemail");
        mail.To.Add("toemail");
 
        //set the content
        mail.Subject = "This is an email";
        mail.Body = "this is a sample body";
 
        //send the message
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "ipaddress";
        smtp.Port = 25;
        try
        {
            smtp.Send(mail);
        }
        catch (Exception ex)
        {
            string strError = ex.ToString();
            lblError.Text = strError;
        }

Open in new window

0
FIM2003
Asked:
FIM2003
  • 5
  • 4
  • 3
1 Solution
 
daveamourCommented:
It must be misconfgured I would think.
Try telneting from a command prompt from the Web server - eg start, run, cmd, enter then telnet <ipaddress> 25
Double check the firewall settings and mail relaying security again, I'm sure it must have something missing or wrong.
0
 
DarkHillCommented:
Well the error states "No connection could be made because the target machine actively refused it 139.78.129.87:25".

Ensure the port is not blocked your side, in command prompt type:

telnet 139.78.129.87 25

If it goes through, then fine.

Next, the server may require authentication to send, in which case you need to add more code to your sending function to include username and password, e.g.

smtp.Credentials = New NetworkCredential("sender mail userName", "sender mail password")
0
 
DarkHillCommented:
Usually when allowing relay from a certain IP you can override the need to provide authentication details.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
FIM2003Author Commented:
I've tried to telnet and receive "Could not open connection to the host, on port 25: Connect failed".  

I've double checked the firewall settings and SMTP is in the exceptions with port 25.  I've also double checked the relay IPs and the machine I'm using is in there.  Also, under Authentication in the SMTP access settings, Anonymous access is checked.
0
 
daveamourCommented:
Its difficult to comment really without being there but I think you can be sure that your C# code is fine and that it is a configuration issue for sure, just not sure where!
0
 
DarkHillCommented:
I am able to telnet in so the block must be coming from your PC. Try checking any firewall or AV settings on your own PC. Also, if you are running this from your PC, check with your ISP as some ISPs actually block port 25 if not used via their service to prevent clients from spamming using other servers.
0
 
daveamourCommented:
yep I believe AOL are a pain for doing that!
0
 
FIM2003Author Commented:
We are not using an ISP that would block that.  

Here is the strange thing.  We are using the same SMTP server to forward scans from our copier, which is working fine.

Also, I a web application on this server that will send emails using System.Web.Mail.  However, it will only send the mail if I do not specify the server.  If I specify, the ip address, it fails.  This code worked last week, but quit working without any code changes.

Any more suggestions?
0
 
DarkHillCommented:
So it can send if you do not specify host?

Which host does the copier use to send scans?
0
 
FIM2003Author Commented:
We finally found the problem, the antivirus which is updated by our IT department started blocking the port.  Thanks for all the suggestions.
0
 
DarkHillCommented:
Please note that on 02.10.2009 at 05:53PM I DID advise you to check your AV software.
0
 
FIM2003Author Commented:
Sorry about that, I thought I reread to see if anyone suggested to check the AntiVirus software.  I'll change this to accept your solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now