Link to home
Start Free TrialLog in
Avatar of adamhealy
adamhealyFlag for United States of America

asked on

VB.Net Email Send with GMAIL SMTP Server

Hello,
I am working on creating a "Contact Us" form on a website. I have the below code and it works on SMTP relays that do not require authentication however, I want to use a GMail SMTP server as we use Google Apps for email. I know I need to start with StartTLS somewhere but am unable to make any forward movement as I am unfamiliar with VB.Net 3.5. I have google-ed this extensively but find that mostly all the examples and explanations utilize older frameworks and the syntax is different. I have also found that some people use ChilKat, is this needed or can VB.Net handle it without an additional tool?

Thanks for the patience, any help is greatly appreciated.

-Adam
Private Sub SendMail(ByVal from As String, ByVal body As String)
        Dim mailServerName As String = "smtp.gmail.com"
        Dim message As MailMessage = New MailMessage(from, "info@domain.com", "Website Contact", body)
        Dim mailClient As SmtpClient = New SmtpClient
        Dim SmtpUser As New System.Net.NetworkCredential()
   
       
        mailClient.UseDefaultCredentials = True
 
        mailClient.Host = mailServerName
        mailClient.Send(message)
        message.Dispose()
 
    End Sub

Open in new window

Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Avatar of adamhealy

ASKER

Thanks for the links...

I have the below code and incorporated a few things from the links but still no luck.

I set it to prompt me w/ a msg box when the email sends but again, it only works on an SMTP relay that doesn't require authentication.

Any ideas?

Thanks in advance....
Private Sub SendMail(ByVal from As String, ByVal body As String)
        'Set the HostName
        Dim host As String = "smtp.google.com"
        Dim [To] As String = "info@domain.com"
        Dim Subject As String = "Web Contact"
 
        Dim Email As New System.Net.Mail.MailMessage([from], [To])
        Email.Subject = Subject
        Email.Body = body
 
        Dim mailClient As New System.Net.Mail.SmtpClient()
        mailClient.Host = host
        mailClient.EnableSsl = True
        
        Dim port As Int32 = 587
              
        Dim authenticationInfo As _
          New System.Net.NetworkCredential( _
           "user@domain.com", _
           "password")
 
        mailClient.UseDefaultCredentials = False
       
        Try
          
            mailClient.Send(Email)
 
            MsgBox("Email Sent!")
        Catch ex As Exception
 
        End Try
 
        Email.Dispose()
        mailClient = Nothing
        GC.Collect()
 
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jmwheeler
jmwheeler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I just noticed that too and added it. Same issue though....It doesn't seem to authenticate. Before anyone suggests it I will go and check my username/password again but I am pretty sure that isnt the issue.:)

Updated code is below....

Thanks....
Private Sub SendMail(ByVal from As String, ByVal body As String)
        'Set the HostName
        Dim host As String = "smtp.google.com"
        Dim [To] As String = "info@domain.com"
        Dim Subject As String = "Web Contact"
 
        Dim Email As New System.Net.Mail.MailMessage([from], [To])
        Email.Subject = Subject
        Email.Body = body
 
        Dim mailClient As New System.Net.Mail.SmtpClient()
        mailClient.Host = host
        mailClient.EnableSsl = True
        
        Dim port As Int32 = 587
              
        Dim authenticationInfo As _
          New System.Net.NetworkCredential( _
           "user@domain.com", _
           "password")
 
        mailClient.UseDefaultCredentials = False
        mailClient.Credentials = authenticationInfo
       
        Try
          
            mailClient.Send(Email)
 
            MsgBox("Email Sent!")
        Catch ex As Exception
 
        End Try
 
        Email.Dispose()
        mailClient = Nothing
        GC.Collect()
 
    End Sub

Open in new window

username and password are good....no typos
JPaulino,
I will try that but does GMail use basic SMTP authentication or something else...? Forgive my limited knowledge on this area.

Jpaulino,
Question on the link you sent and the mechanism they used....

I used "Dim Email As New System.Net.Mail.MailMessage" so mine should email.fields.add("...... not mail.Fields.Add?

Is that right? I get the error "Fields is not a member or System.Net.Mail.MailMessage"
Humm, I now saw your latest code snippet and it should work. You don't need nothing more ...
 
What error do you got ? Can you add a msgbox on your Catch ex As Exception and see the error ?
I was going to add something like below...But I think I am missing something as MessageBox.Show and ex don't appear to be valid, I get them underlined with an error. Am I missing a imports statement?

MeesageBox.Show = MessageBox not declared

ex = hides a variable in an enclosing block



Try
  mailClient.Send(Email)
  MsgBox("Email Sent!")
  Catch ex As Exception
        
MessageBox.Show("Unable to send email, because the following error has occurred:" & _
     vbCrLf & vbCrLf & _
     ex.Message, _
     "Error sending email", _
      MessageBoxButtons.OK, MessageBoxIcon.Error)
 
End Try

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well that seemed to get me the error. Thanks...

The error is:

"The remote certificate is invalid according to the validation certificate. "

I guess my question is what validation certificate is it looking for and why is it seeing it as invalid.
Check if the antivirus or firewall are blocking or read this
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!138.entry 
I am looking at it and does these settings need to be made in web.config? I have downloaded the code and trying to understand it but does this apply as my proj is vb adn this example is c#?

Yes that are changes in the webconfig.

I have never had that problem, and I have only googled for that. You can use this tool to convert what you don't understand
http://www.developerfusion.com/tools/convert/vb-to-csharp/
What I am thinking is the issue is I have it set to:
Dim mailClient As New System.Net.Mail.SmtpClient()
mailClient.EnableSsl = True

Do I need to have a SSL cert on my machine? Is this the right option or is there something for the mail server...enablessl = true?
Ok....I am working on converting it...I will keep you posted but this will take me a while...thanks.......
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok....so I decided that recoding it in C# was a bad idea so I stuck with vb. I DID get the email working but only after I switched my authentication credentials to an actual gmail account vs the google apps account I was using. Not sure why I was getting the cert validation error with a google apps account as smtp.gmail.com is the same address google apps users use for smtp connections. Maybe some reverse lookup?...IDK

I am getting another issue but I will repost in another question as JPaulino deserves these points as he greatly helped in real time.

For those curious, my other issue is below....I will post the hyperlink to the new question.

It seems GMail (my smtp relay) and GoDaddy (my hosting provider) cannot interchange emails. I am not sure who or where the block occurs but when I upload my site to GoDaddy the email does not go through to the GMail smtp server. It works fine in development but once uploaded it doesn't work and DOES NOT seem to error. I would assume the email is getting block by either or both of them. I read the below link and they reference an issue with the reply-to not being set correctly....I will work on it and see what I can do but if anyone has a fix please let me know.
http://forums.asp.net/t/1324542.aspx