Problems accessing remote SMTP server

enak

Member
Joined
Aug 29, 2006
Messages
11
Programming Experience
10+
I need to be able to send email via a remote SMTP server. I am having trouble connecting to the server.Here is my code:

VB.NET:
Public Sub SendEmailMessage() 
Dim dr As SqlClient.SqlDataReader 
Dim smtpCredentials As New Net.NetworkCredential("myusername", "password") 
'For each to address create a mail message 
Dim MailMsg As New MailMessage(New MailAddress("from@address.com"), New MailAddress([EMAIL="to@address.com"]to@address.com[/EMAIL])) 
MailMsg.Subject = "Test JWI" 
MailMsg.Body = "Testing" & vbCrLf 
MailMsg.Priority = MailPriority.Normal 
MailMsg.IsBodyHtml = True 
'Smtpclient to send the mail message 
Dim SmtpMail As New SmtpClient 
SmtpMail.UseDefaultCredentials = False 
SmtpMail.Host = "smtp.remotesite.com" 
SmtpMail.Credentials = smtpCredentials 
SmtpMail.Send(MailMsg) 
End If 
cnn.Close() 
Catch ex As Exception 
MsgBox("Error: " & ex.Message, MsgBoxStyle.Exclamation) 
End Try 
End Sub

The error message is that it can not connect to the smtp server. Again, the smtp server is a remote server.here is a trace of the error:

A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dllSystem.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 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 WindowsApplication1.Email.SendEmailMessage() in D:\Email.vb:line 103Failure sending mail.System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 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)System

I don't see anything there that helps me know why I can not connect to the remote server.--

Thanks,enak
 
enak, oh my goodness!! How on earth can you expect anyone to read that? and to top the lot you posted it in the wrong forum.

So being the nice guy that i am i've sorted it out and moved it..
 
I am sorry. I formatted it twice but the result was the same. I don't know why it came out like that. All I did was cut and paste.
 
I don't see you set the port to communicate with Smtp server, it's usually port 25.
 
I set the port to 25 and nothing changed. Any other ideas? I will try anything at this point.

I have another project that is asp.net that I can send email through the remote smtp server. I don't understand why I can not do it through the winforms application.

Here is the asp.net code

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnSend_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnSend.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objMail [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MailMessage[/SIZE]

 
[SIZE=2]lSuccess = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] System.IO.File.Exists([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtAttachment.Text) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]objMail.To = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtTo.Text[/SIZE]
[SIZE=2]objMail.From = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtFrom.Text[/SIZE]
[SIZE=2]objMail.Cc = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtCC.Text[/SIZE]
[SIZE=2]objMail.BodyFormat = MailFormat.Text[/SIZE]
[SIZE=2]objMail.Subject = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtSubject.Text[/SIZE]
[SIZE=2]objMail.Body = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtBody.Text[/SIZE]
[SIZE=2]objMail.Attachments.Add([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MailAttachment([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtAttachment.Text, MailEncoding.Base64))[/SIZE]
[SIZE=2][COLOR=#008000]''SmtpMail.SmtpServer.Insert(0, "smtp.registeredsite.com")[/COLOR][/SIZE]
[SIZE=2]objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1") [/SIZE][SIZE=2][COLOR=#008000]'basic authentication[/COLOR][/SIZE]
[SIZE=2]objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "tc-reports") [/SIZE][SIZE=2][COLOR=#008000]'set your username here[/COLOR][/SIZE]
[SIZE=2]objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password") [/SIZE][SIZE=2][COLOR=#008000]'set your password here[/COLOR][/SIZE]
[SIZE=2]SmtpMail.SmtpServer = "mail.yourdomain.com"[/SIZE]
[SIZE=2]SmtpMail.Send(objMail)[/SIZE]
[SIZE=2]lSuccess = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2]Response.Redirect("ReportSwitchBoard.aspx")[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception[/SIZE]
[SIZE=2]sMessage = ex.ToString[/SIZE]
[SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] ex.InnerException [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2]Response.Write("+++++++++++++++++++++++++++++++++++<BR>")[/SIZE]
[SIZE=2]Response.Write("Error (InnerException): " & sMessage & "<BR>")[/SIZE]
[SIZE=2]ex = ex.InnerException[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'sMessage = ex.InnerException.ToString[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][COLOR=black]
[/COLOR][/SIZE]


Is there any reason why I can not get the winform to work?

Thanks
 
Last edited by a moderator:
It all kinda looks ok, i have heard that port 25 has been blocked by some of the big players. Other than that i can only think that you check your network credentials.
 
from what I can see those links are referring to asp.net code. I already have that working. I need help with the winforms code.

Thanks for the reply, though.
 
The authentication schema applies nevertheless whether webform or winform app sending the smtp mail, and documentation says that 'smtpauthenticate' is only relevant if 'sendusing' is set.

By the way, did your regular winform mail application handle sending this mail through the same server with your same settings like I asked? I can be good to verify with an application you know works for sending mail, if this app is ok then there is a configuration issue with yours, of this app also is not ok then there may be a security issue with the remote server, perhaps it doesn't allow calls from outside its domain? or outside it's allow list of hosted webs.
 
Another item that just came up is that the System.Web.Mail is obsolete. If this is so then why can I not do this with System.Net.Mail?
 
I was under the impression that you were using System.Net.Mail, that's what your exception told at least. They moved this from web.mail in .Net 1 to Net.Mail in .Net 2 and improved and expanded the library, you know. I missed that you posted two different codes, one old and one new. The Net.Mail.SmtpClient provides the authentication done in the fields schema earlier. Btw, there is no difference to set port 25 and not set it in SmtpClient because this is defualt.
 
So, why can't I log into the remote smtp server? The .Net.Mail is not working. None of the code samples that I have looked at work for me either.
 
Back
Top