Bug when sending email using Gmail smtp and vb.net

stoymigo

Senior Member
Joined
Dec 11, 2008
Messages
975
Reaction score
26
I'm trying to send an email using VB.NET
Initially I used System.web.mail, but the attachment it was sending was corrupted, so I used System.net.mail and it works.

Ran it on my side, client ran it on their side and it could send emails. Until
this morning the client said they got this error:

“Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server <Ip address here> (<Ip address here>:587), connect timeout”

I'm trying to reproduce this error on my side but can't. If I disconnect from internet completely or my internet is so slow that it can't upload the email, I do get errors but none of them the same as the one the client is getting.

I've read something about port issues, but have to read more on it, thx for help.
 
I think you need to investigate the machine's smtp settings first.

I don't understand, the settings are in the code, unless the client has an email client running and that is interfering, if
that's what you mean by smtp settings.
 
Some ISPs do port redirection on port 25 to their own SMTP servers. Maybe this, or something similar is happening to port 587 now?

Ask the client to open a telnet session to IP:587. Just keep in mind that on Windows 7 machines the telnet client is not installed by default. I may also be that they have some antivirus/firewall blocking it.
 
My guess is that the client is behind a firewall that doesn't allow connections on 587. The error they're getting clearly states that they can't connect to the server when sending emails. If you can't reproduce the error from your side, this usually (as in 99.99% of the time) means that it's the client's settings on their side.

If I were you I'd try using 25 instead of 587 (even though some ISP's hijack the port and route it through their own servers for some reason) as it might help with compatibility. For some reason server/router admins have this idea that 25 is the SMTP port when it's not really the standard.

You could also try Try/Catch the error on 587 and pushing it through to 25. This will slow down your application though (waiting for default timeout connection/errors, but I'm sure you can set those somewhere)
 
Code:
 Dim MyMailMessage As New MailMessage()

        'From requires an instance of the MailAddress type
        MyMailMessage.From = New MailAddress(sFrom)

        'To is a collection of MailAddress types
        MyMailMessage.To.Add(sTo)
        MyMailMessage.Subject = ""
        MyMailMessage.Body = ""
        Dim objAttach As New Attachment(sAttachmentPath)

        'Create the SMTPClient object and specify the SMTP GMail server
        MyMailMessage.Attachments.Add(objAttach)
        Dim SMTPServer As New SmtpClient(sSMTP)
        SMTPServer.Port = 587
        'SMTPServer.Port = 25   // this also works
        'SMTPServer.Port = 465
        SMTPServer.Credentials = New System.Net.NetworkCredential(sFrom, sFromPassword)
        SMTPServer.EnableSsl = True


        SMTPServer.Send(MyMailMessage)
        objAttach.Dispose()

        File.Delete(sAttachmentPath)

The client told me that emailing actually worked today, so it was only on Saturday morning it didn't.
This is good, but now we'll have to test this for a while because we plan to include more email sending in the app for different stuff.

Next time that error happens I'll let him know that he must tell me so I can run it from my side to test if the google SMTP is the problem.


Should I try a smtp other than google's(even though google should be one of the best),
even if it requires payment?
 
Used to rely on gmail for .net app I wrote for someone, very unreliable so got co.za domain with small hosting package for them, no problems since.
 
Used to rely on gmail for .net app I wrote for someone, very unreliable so got co.za domain with small hosting package for them, no problems since.

How frequently was it down? Also is it faster to use a local smtp versus gmail's smtp?
 
also as a word of advice use the web config to store your settings. it makes everything easy.

Web.config
<system.net>
<mailSettings>
<smtp>
<network host="Placeholder" port="Placeholder" userName="Placeholder" password="Placeholder"/>
</smtp>
</mailSettings>
</system.net>

then when you need to send an email
Dim mailMsg As New MailMessage
With mailMsg
.IsBodyHtml = False
.From = New MailAddress("[email protected]")
.To.Add("")
.Subject = ""
.Body = ""
End With
Dim mailClient As New SmtpClient()
mailClient.Send(mailMsg)
 
How frequently was it down? Also is it faster to use a local smtp versus gmail's smtp?

Was a while ago so don't remember specifics, just found it very unreliable: often getting SSL errors or timeouts. Sometimes it would work for a few days and then suddenly start giving errors without any reason. I would imagine that a local SMTP would provide better performance unless Google have a local server for SMTP, not sure though. Also, if this is for a commercial application (or at least one used by a business), probably more professional to have e-mails coming from a branded domain rather than a generic @gmail.com address. Also, with paid-for hosting you get technical support which is almost nonexistent for Gmail unless you're using Google Apps (paid for).
 
I'm guessing your running into some kind of spam protection i.e. Google trying to protect its smtp. How many emails is the client pushing through this smtp?

Are you using the same login for testing?

I bet mail being automated is pretty much the same every time, right?
 
I'll have to include a queue feature for failed emails and for finding which sent through which not.
My client uses a different login, they send within the daily limit (500 emails), and the size of each is v. small
Will be testing different SMTPs for now, then ask the client to test.
 
Top
Sign up to the MyBroadband newsletter
X