# Visual Basic > Visual Basic .NET > VS 2008 Sending Email Problem

## sacramento

Hi to all,

I have this code to send emails, but unfortunately without sucess, once app retrive the message that as not send the email...I had verify credentials for email, and permissions on gmail, and seems everything is OK. Something wrong on the code?

this is the code:



```
Try

            Dim Mail As New MailMessage
            Dim SMTP As New SmtpClient("smtp-client")
            SMTP.EnableSsl = True
            SMTP.Credentials = New System.Net.NetworkCredential("XXXXXXXX", "YYYYYY")
            SMTP.Port = "587"

            Mail.Subject = "test email"
            Mail.To.Add("DDDDDD")
            Mail.From = New MailAddress("XXXXXXXXX")
            Mail.Body = "This is a test email"

            SMTP.Send(Mail)

            SMTP = Nothing

            MsgBox("Email Sended...Thanks", MsgBoxStyle.Information)


        Catch ex As Exception
            MsgBox("Email not sended...Something fail", MsgBoxStyle.Critical)
        End Try
```

----------


## OptionBase1

I don't see anywhere in that code where you are actually setting the SMTP server to connect to.

----------


## sacramento

> I don't see anywhere in that code where you are actually setting the SMTP server to connect to.


You mean this:



```
SMTP.Host = "smtp.gmail.com"
```

----------


## OptionBase1

> You mean this:
> 
> 
> 
> ```
> SMTP.Host = "smtp.gmail.com"
> ```


That line isn't in the code you initially posted.

----------


## sacramento

> That line isn't in the code you initially posted.


Yes...but i had add to the code, and the problem remain

----------


## paulg4ije

Do you have 2-step verification and an app password for the Gmail account?

----------


## sacramento

> Do you have 2-step verification and an app password for the Gmail account?


Yes I have it

----------


## paulg4ije

Not sure I can offer much more help. This is the code I have used to send emails (but not via Gmail). It looks very similar to yours:



```
Dim mail As New MailMessage

            mail.From = New MailAddress("xxxx@xxxx.co.uk", "Report attached", System.Text.Encoding.UTF8)
            mail.[To].Add("admin@mydomain.net")
            mail.Subject = "Report " & Date.Now
            mail.Body = "Please see attached report"
            mail.ReplyToList.Add("xxxx@xxxx.co.uk")

            Dim attachment As System.Net.Mail.Attachment
            attachment = New System.Net.Mail.Attachment(GsLogFile)
            mail.Attachments.Add(attachment)

            Dim smtp As New SmtpClient()
            smtp.UseDefaultCredentials = False
            smtp.Credentials = New Net.NetworkCredential("xxxx@xxxx.co.uk", "pass123")
            smtp.Port = 587
            smtp.EnableSsl = False
            smtp.Host = "mail.xxxx.co.uk"

            smtp.Send(mail)
```

----------


## sapator

First of, how do you expect to find an issue if you are not handling the exception message?
Second, try it this way so the client can be disposed properly.



```
''mail create...
 Try 
  Using smtp As New SmtpClient("theaddress",port)
  smtp .Credentials = credentials
'...etc
smtp.Send(mail)
   End Using
  catch ex As SmtpException
 MsgBox("Email not sended...Something fail: " & ex.Message, MsgBoxStyle.Critical)
end try
```

If you can send the email without exception then there is something else going on. I don't deal with gmail but there should be some kind of log of exception if you log in to you gmail account.

----------


## techgnome

See if this is any help https://www.emailarchitect.net/easen...ject_oauth.htm

-tg

----------


## techgnome

There's also this... not sure how old it is or if still relavent.

https://www.emailarchitect.net/easen...net.aspx?cat=2

-tg

----------


## sacramento

Hi guys,

Thanks for your replys and i had find that the problem concern with the authentication in Gmail, and the only way that i had find to resolve the problem it was create a password in the Definitions of the account and then security and then "Passwords of aplications".
Heaven with the "Access to less secure apps" turned ON, the code not worked.
I don't know if is the right procedure, but, until now is the only way that work for me

----------

