System.Net.Mail

davearia

Well-known member
Joined
Jan 4, 2005
Messages
184
Hi,

I have some code:

Code:
Imports System.Net.Mail

Public Function SendEmail(ByVal recipients As String, ByVal ccRecipients As String, ByVal subject As String, ByVal body As String, ByVal userName As String, ByVal userID As Int32) As Boolean
    Dim from As String = System.Configuration.ConfigurationManager.AppSettings("ITHelpdeskEmailAddress")
    Dim objMM As New MailMessage(from, recipients, subject, body)
    If ccRecipients.Length > 0 Then
         objMM.Cc.Add(ccRecipients)
    End If
    objMM.IsBodyHtml = True
    objMM.Priority = MailPriority.High
     Dim sender As New System.Net.Mail.SmtpClient("?")
     sender.DeliveryMethod = SmtpDeliveryMethod.Network
     sender.UseDefaultCredentials = True
     sender.Send(objMM)
     Return True
End Function

This code was originally part of a web service, but for simplicitys sake it is called by a button click in a basic windows form application. On my machine I get this exception:

ex.ToString "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
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 NetMail.Form1.bSendEmail(String sRecipient, String sSubject, String sBody) in C:\Documents and Settings\djohnson\Desktop\NetMail\Form1.vb:line 67" String

Apart from my machine only one other machine throw this exception. On all the machines tried this code runs perfectly ok with no exceptions and the email is received.

Can anyone explain what night be causing this to happen?

Thanks, Dave.
 
It could be a networking issue (firewall etc.), easiest way to discount this is to go to a command prompt and type
Code:
telnet <name or ip address of mail server> 25
and see what happens. If this fails then it is a network issue and not your code, if that works drop a reply with what you see on screen and we will see what we come up with.
 
I tried the the following

telnet 127.1.1.192:25

and

telnet 127.1.1.192 25

Both said that it could open connection to host.

Cheers, Dave.
 
Is there anything different about this one machine i.e. firewalls, ip address configuration etc? Does this particular pc have any other connectivity issues?

Can the other computers ping that address without problems?

It certainly looks more like a networking issue rather than a code problem though, if telnet cannot access the smtp service then there is no way your code would be able to do so either.
 
I tried the same telnet test on other machines that run the code ok and they failed to connect as well. Strange! No there are no other network issues with this machine that I am aware of. I agree with you it doesnt sound like a code issue. There is something different with how that machine is networked but I cannot think what it would be.
 
Are you sure you have the correct server name / ip address? I cant see why telnet would fail and the code work when both operations are doing the same thing...

Where did the 127.1.1.192 address come from? It is a peculiar choice for an address range (putting it mildly).
 
Hi,

Im sorry, I changed the IP for this post. The IP I sued was a different one. But the IP I am quite sureish is the right one.
 
Can you ping the server (by name and address) from the computer(s) that cant send e-mail?

If not are you noticing any other networking issues with these computers? Either way it might be worth comparing address, gateway and mask of a working computer to a non-working computer to see if there are any obvious differences.

Are you aware of any firewalls or similar being installed?
 
This was caused by the Antivirus (Mcafee). Everyone else has an older version than I am using.

I turned the thing off and the email sent, what a pain!

Thanks again for all your help, appreciated as always.:D
 
Back
Top