3

I have to following code:

Try
    Dim mail As New MailMessage()
    Dim SmtpServer As New SmtpClient("smtp.gmail.com")
    mail.From = New MailAddress(txtid.Text)
    mail.[To].Add(TextBox1.Text)
    mail.Subject = txtsub.Text
    mail.Body = txtmess.Text
    ' mail.Attachments.Add(New Attachment(OpenFileDialog1.FileName))
    SmtpServer.Port = 587
    SmtpServer.Credentials = New System.Net.NetworkCredential(txtid.Text, txtpass.Text)
    SmtpServer.EnableSsl = True

    SmtpServer.Send(mail)
    MsgBox("E-mail Has Been Send Successfully !")
Catch ex As Exception
    MsgBox(ex.Message)
End Try

on the step

SmtpServer.Send(mail)

I'm always getting an error : Failure sending mail

Any idea how to fix it?

Note that I'm using VB.NET

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
User7291
  • 1,095
  • 3
  • 29
  • 71
  • What is the exception type? – Karl Anderson Sep 13 '13 at 13:43
  • @KarlAnderson it's a SMTPException – User7291 Sep 13 '13 at 13:46
  • i tried testing the ports but still nth :/ – User7291 Sep 13 '13 at 14:35
  • 1
    gmail is pretty reliable (if you account for the port issue) and I have a code sending emails without any problem doing virtually the same than yours. I can only come up with: either you are inputting the wrong login info or the accounted is blocked. Bear in mind that new accounts might get blocked when testing this kind of code (they ask you to input your phone number); the best way to make sure that everything is fine is loging into this account and confirming that there is no warning/message asking you for further info; if there is anything like this, fix it such that this code can work. – varocarbas Sep 13 '13 at 14:42
  • The ports you should try are 587 and 25 (I wrote a typo in my previous comment): https://support.google.com/mail/answer/78775?hl=en – varocarbas Sep 13 '13 at 14:46
  • @varocarbas i'm sure 200% of my credentials and the email adress is not blocked.. it's working fine and i'm still getting this error ! it's getting me insane ! – User7291 Sep 13 '13 at 14:47
  • (please try the port 25, previously I wrote it wrongly) Bear in mind that the account can get blocked suddenly (while you are doing tests). It is not a real blocking, just a prompt asking you for confirming some information but the application cannot pass through this (you need to go there and input the requested info manually). – varocarbas Sep 13 '13 at 14:49
  • @varocarba i know it's 25 i'm testing it and i'm opening the email account and it's not asking for any confirmations... – User7291 Sep 13 '13 at 14:52
  • I cannot be of further help then. But it is a really weird issue, this code should work without any problem with gmail and thus the problem has to be in your exact conditions (your gmail account, your program or your computer (antivirus/firewall)). – varocarbas Sep 13 '13 at 14:54
  • @varocarbas i'm also trying hotmail server : smtp.live.com with the port 587 and still nth ! i'm running out of solutions here :/ – User7291 Sep 13 '13 at 15:05
  • Each server/email provider requires a special configuration (you should look in the hotmail help pages to know the port and the special conditions). I have experience with gmail and I know that (once the aforementioned issues are accounted for) your code should work fine; but if you say that it does not work, I cannot be of further help. – varocarbas Sep 13 '13 at 15:08

3 Answers3

2

Try configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), works for me.

Jason Hughes
  • 748
  • 6
  • 18
1

Since you are receiving an SmtpException, the SmtpClient.Send Method (MailMessage) documentation states that the following reasons could be the cause:

  • The connection to the SMTP server failed.
  • Authentication failed.
  • The operation timed out.
  • EnableSsl is set to true, but the DeliveryMethod property is set to SpecifiedPickupDirectory or PickupDirectoryFromIis.
  • EnableSsl is set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.

I would use Trim() with your username and password text values to remove any potential leading or trailing spaces, like this:

SmtpServer.Credentials = New System.Net.NetworkCredential(txtid.Text.Trim(), 
                                                          txtpass.Text,Trim())

I would also recommend forcing the DeliveryMethod to use the SMTP server, like this:

SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
0

My own experiments suggest that .NET's SmtpClient class doesn't actually support SMTP over SSL (SMTPS). Toggling the .EnableSsl flag on your client object will instead cause the client to use STARTTLS.

This isn't surprising given that SMTPS has been deprecated in favor of STARTTLS for a number of reasons, most of which have to do with seamless interoperability between new and old clients and servers.

SMTPS: The client opens a TCP connection with the target server. The two start an SSL handshake immediately and then proceed to communicate through that SSL tunnel using the regular SMTP protocol (EHLO, AUTH, etc).

STARTTLS: The client opens a TCP connection with the target server. The client issues an EHLO and then a STARTTLS. This is done using plain text. The STARTTLS command prompts the client and server to start an SSL handshake over the already open socket. All future communication (AUTH command and beyond) is done through the now-established SSL tunnel.

This presents a problem when the SmtpClient object is connecting to a server that is expecting SMTPS. The client will send an EHLO after opening its socket instead of starting an SSL handshake immediately which will cause the server to consider the SSL handshake to have failed and the connection will stall or die. The client will timeout and you will receive an SmtpException.

Any SMTP server listening on port 465 is probably expecting SMTPS. SMTPS has its own dedicated port and cannot optionally support STARTTLS because STARTTLS by definition requires the server to accept and respond to the opening EHLO and STARTTLS commands before using SSL.

SMTP servers on traditional ports like 587 or 25 however have the option of supporting STARTTLS should the server software support it and service administrator choose to enable it. This is why many people on the Internet posting about this problem have noted that they are able to get the SmtpClient to work by changing the port from 465 to 587.


The short version: The SmtpClient class supports STARTTLS and not SMTP over SSL (SMTPS). If you want to use the .EnableSsl flag on the client object, make sure the server and port you are connecting to supports STARTTLS. If the server and port are instead listening for and expecting true SMTPS, the client object will be unable to connect successfully and the send will fail. Port 465 is almost exclusively used for SMTPS. Ports 25 and 587 are used for regular SMTP or SMTP w/ STARTTLS support when STARTTLS is available (optional).

Geoff
  • 1