[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SMTP - Failure sending mail - C#

Posted on 2009-02-10
12
Medium Priority
?
10,897 Views
Last Modified: 2013-11-30
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
Comment
Question by:FIM2003
  • 5
  • 4
  • 3
12 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 23601309
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
 
LVL 7

Expert Comment

by:DarkHill
ID: 23601362
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
 
LVL 7

Expert Comment

by:DarkHill
ID: 23601375
Usually when allowing relay from a certain IP you can override the need to provide authentication details.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:FIM2003
ID: 23601475
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
 
LVL 19

Expert Comment

by:daveamour
ID: 23601489
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
 
LVL 7

Accepted Solution

by:
DarkHill earned 2000 total points
ID: 23601641
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
 
LVL 19

Expert Comment

by:daveamour
ID: 23601655
yep I believe AOL are a pain for doing that!
0
 

Author Comment

by:FIM2003
ID: 23604419
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
 
LVL 7

Expert Comment

by:DarkHill
ID: 23608375
So it can send if you do not specify host?

Which host does the copier use to send scans?
0
 

Author Comment

by:FIM2003
ID: 23612532
We finally found the problem, the antivirus which is updated by our IT department started blocking the port.  Thanks for all the suggestions.
0
 
LVL 7

Expert Comment

by:DarkHill
ID: 23615257
Please note that on 02.10.2009 at 05:53PM I DID advise you to check your AV software.
0
 

Author Comment

by:FIM2003
ID: 23616473
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There was an incident about the POP3 issue for the double read receipts and delivery receipts in Exchange 2013.  There was huge research been done and found solution for the duplicate mails. Especially when the user gets  duplicate mails.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question