Sending Email using Gmail

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
166
Location
Chicago
Programming Experience
10+
Hello again all

I have Thunderbird setup to use a gmail account, so I know I have the correct SMTP info, and sending emails in TB is no problem.

However, when I try to use the same info in my own .NET app, I just get a real long pause when attempting to email something, and finally an Operation Timed Out message. I was wondering if anyone has experience with this and can point out what I may be missing? The mail code itself is working fine if I use my own personal ISP's SMTP server without SSL, but I can't use my own personal one under the circumstances.

Here is the code that I am using:

VB.NET:
Imports Microsoft.VisualBasic
Imports System.net.Mail

Public Class SendEmail

    Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
    As String, ByVal strSubject _
    As String, ByVal strMessage _
    As String, ByVal fileList() _
    As String, ByVal intProgbar _
    As Integer, ByVal strSMTPServer As String)

        'This procedure takes string array parameters for multiple recipients and files
        Try
            For Each item As String In strTo
                'For each To address create a mail message
                Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(item))
                MailMsg.BodyEncoding = System.Text.Encoding.Default
                MailMsg.Subject = strSubject.Trim()
                MailMsg.Body = strMessage.Trim() & vbCrLf
                MailMsg.Priority = MailPriority.High
                MailMsg.IsBodyHtml = True

                'attach each file attachment
                For Each strfile As String In fileList
                    If Not strfile = "" Then
                        Dim MsgAttach As New Attachment(strfile)
                        MailMsg.Attachments.Add(MsgAttach)
                    End If
                Next

                'Smtpclient to send the mail message
                Dim SmtpMail As New SmtpClient
                ' Is this in the correct place (used to login to SMTP server)
                Dim theCredential As New System.Net.NetworkCredential(frmMain.txtLogin.Text, frmMain.txtPW.Text)

                SmtpMail.EnableSsl = True
                SmtpMail.Port = 465

                SmtpMail.Credentials = theCredential
                SmtpMail.Host = strSMTPServer
                SmtpMail.Send(MailMsg)

            Next
            ' Message Successful
        Catch ex As Exception
            ' Message Error
            'Console.Clear()
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.InnerException)
        End Try

        MessageBox.Show("Complete!")

    End Sub

End Class

TIA
 
Thank you, that worked perfect. Out of curiosity, how did you know that was the correct port to use? I've been searching all over and everything always says 465? lol
 
the sentence: Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587 is all over and it's strange if you were not able to find it on Google!!
 
I have tried both ports.

465 --> Timed out
857 --> Server actively refued connection

So? What is the next step please?

Rens
 
Back
Top