none
How to send email using gmail.smtp over a proxy server with vb.net

    Question

  • Hello

    i am working on a my project, which send email to me using gmail.smtp, it works fine on xp, vista, and + some of windows 7 versions, in few pc's i had tried that, and got a issue.

    here is detail of error i got in there, i am using visual studio 2008 & a internet with proxy server,

    in my openion its a problem with internet ? maybe ? because when i had used this on my dsl connection it worked.

    is there anything to add ? for proxy servers ?

    if i compile it & run it via F5 error shows like this

    "A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll"

    if i run it direct my.exe than error show like this

    ************** Exception Text **************
    System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'smtp.gmail.com'
       at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
       at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
       at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
       at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
       at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
       at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
       at System.Net.Mail.SmtpClient.GetConnection()
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       --- End of inner exception stack trace ---
       at System.Net.Mail.SmtpClient.Send(MailMessage message)

    here is my code

    i have imported "Imports System.Net.Mail" :D if you ask about it :)


            Dim mail As New MailMessage()
            Dim SmtpServer As New SmtpClient
            Dim Info As String
            Dim Message As String
            SmtpServer.Credentials = New  Net.NetworkCredential(ID.Text, PWD.Text)
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = True
            mail.To.Add(To.Text)
            mail.From = New MailAddress("myaddress@temp.com")
            mail.Subject = ("Welcome")
            mail.Body = Message.Text
            SmtpServer.Send(mail)

    Thanks in Advance :)


    Thursday, February 09, 2012 9:14 PM

Answers

  • Greetings, try the following and see if the same error happens

    Imports System.ComponentModel
    Imports System.Net.Mail
    Imports System.Net
    
    Module GMailCode
        Private mailSent As Boolean = False
        Private Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
            Dim token As String = CStr(e.UserState)
    
            Console.WriteLine("Call back at {0} token [{1}]", Now, token)
            If e.Cancelled Then
                Console.WriteLine("[{0}] [{1}] Send canceled.", Now, token)
            End If
            If e.Error IsNot Nothing Then
                Console.WriteLine("[{0}] [{1}] [{2}]", Now, token, e.Error.ToString())
            Else
                Console.WriteLine("Message sent at {0}", Now)
            End If
    
            mailSent = True
    
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="SenderBaseGMailAccount"></param>
        ''' <param name="GMailPassword"></param>
        ''' <param name="Check"></param>
        ''' <param name="SendToEmail"></param>
        ''' <remarks></remarks>
        Sub TestSendGMail( _
            ByVal SenderBaseGMailAccount As String, _
            ByVal GMailPassword As String, _
            ByVal Check As Boolean, ByVal SendToEmail As String)
    
            Dim Client As New SmtpClient("smtp.googlemail.com", 587)
            Client.DeliveryMethod = SmtpDeliveryMethod.Network
            Client.Credentials = New NetworkCredential(String.Format("{0}@gmail.com", _
                                                       SenderBaseGMailAccount), _
                                                       GMailPassword)
    
            Client.EnableSsl = True
            Client.Timeout = 20000
    
            Dim FromAddress As New MailAddress( _
                                String.Format("{0}@gmail.com", SenderBaseGMailAccount), _
                                "Your Name", System.Text.Encoding.UTF8)
    
            Dim TooAddress As New MailAddress(SendToEmail)
    
            Dim Message As New MailMessage(FromAddress, TooAddress)
            Message.Body = "This is a test e-mail message do not respond."
            Message.BodyEncoding = System.Text.Encoding.UTF8
            Message.Subject = "test message 1"
            Message.SubjectEncoding = System.Text.Encoding.UTF8
    
            Dim UserToken As String = "test message1"
    
            AddHandler Client.SendCompleted, AddressOf SendCompletedCallback
            Client.SendAsync(Message, UserToken)
    
            ' Do not uncomment
            'message.Dispose()
        End Sub
    End Module

    Call the above as follows where KSG would be your base email account and the last parm is who the email goes too.

    Private Sub Button1_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    
        TestSendGMail("KSG", _
                      "My Password", _
                      True, _
                      "KSG@comcast.net")
    End Sub


    KSG

    Thursday, February 09, 2012 9:46 PM
    Moderator

All replies

  • Hello Ashfaq Ahmed Solangi.

    it's Simple

    Imports System.Net.Mail Private Sub SendButton_Click(sender As System.Object, e As System.EventArgs) Handles SendButton.Click message.From = New MailAddress(Me.FormEmailIDTextBox.Text) message.Body = Me.MessageBodyTextBox.Text message.Subject = Me.EmailSubjectTextBox.Text message.To.Add(Me.ToSendEmailIDTextBox.Text) message.Priority = MailPriority.Normal 'smtp client setting' smtp.EnableSsl = True smtp.Port = "587" 'Gmail / Hotmail Port smtp.Host = "smtp.gmail.com" 'this is gmail, smtp.live.com is hotmail smtp.Credentials = New Net.NetworkCredential(Me.FormEmailIDTextBox.Text, Me.PassWordFromEmailTextBox.Text) Try smtp.Send(message) MsgBox("Send Message Successful") Catch ex As Exception MessageBox.Show("Could not send email...check your settings.") End Try End Sub

    with regards
    Thursday, February 09, 2012 9:42 PM
  • Greetings, try the following and see if the same error happens

    Imports System.ComponentModel
    Imports System.Net.Mail
    Imports System.Net
    
    Module GMailCode
        Private mailSent As Boolean = False
        Private Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
            Dim token As String = CStr(e.UserState)
    
            Console.WriteLine("Call back at {0} token [{1}]", Now, token)
            If e.Cancelled Then
                Console.WriteLine("[{0}] [{1}] Send canceled.", Now, token)
            End If
            If e.Error IsNot Nothing Then
                Console.WriteLine("[{0}] [{1}] [{2}]", Now, token, e.Error.ToString())
            Else
                Console.WriteLine("Message sent at {0}", Now)
            End If
    
            mailSent = True
    
        End Sub
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="SenderBaseGMailAccount"></param>
        ''' <param name="GMailPassword"></param>
        ''' <param name="Check"></param>
        ''' <param name="SendToEmail"></param>
        ''' <remarks></remarks>
        Sub TestSendGMail( _
            ByVal SenderBaseGMailAccount As String, _
            ByVal GMailPassword As String, _
            ByVal Check As Boolean, ByVal SendToEmail As String)
    
            Dim Client As New SmtpClient("smtp.googlemail.com", 587)
            Client.DeliveryMethod = SmtpDeliveryMethod.Network
            Client.Credentials = New NetworkCredential(String.Format("{0}@gmail.com", _
                                                       SenderBaseGMailAccount), _
                                                       GMailPassword)
    
            Client.EnableSsl = True
            Client.Timeout = 20000
    
            Dim FromAddress As New MailAddress( _
                                String.Format("{0}@gmail.com", SenderBaseGMailAccount), _
                                "Your Name", System.Text.Encoding.UTF8)
    
            Dim TooAddress As New MailAddress(SendToEmail)
    
            Dim Message As New MailMessage(FromAddress, TooAddress)
            Message.Body = "This is a test e-mail message do not respond."
            Message.BodyEncoding = System.Text.Encoding.UTF8
            Message.Subject = "test message 1"
            Message.SubjectEncoding = System.Text.Encoding.UTF8
    
            Dim UserToken As String = "test message1"
    
            AddHandler Client.SendCompleted, AddressOf SendCompletedCallback
            Client.SendAsync(Message, UserToken)
    
            ' Do not uncomment
            'message.Dispose()
        End Sub
    End Module

    Call the above as follows where KSG would be your base email account and the last parm is who the email goes too.

    Private Sub Button1_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    
        TestSendGMail("KSG", _
                      "My Password", _
                      True, _
                      "KSG@comcast.net")
    End Sub


    KSG

    Thursday, February 09, 2012 9:46 PM
    Moderator
  • Hello Mind-Reader

    it looks great and related to the issue, but for network credential on a proxy internet connection i have just only host name and port, there is not username and password, so how do i use that ?

    actually the problem is to link the internet in my application, when i use my app on my 2nd pc where the internet is connected through proxy server this error shows up,  but while on a dsl connection which is directly connected to internet it works fine.

    any idea to set a internet source settings in my app to access internet ? while on proxy server

    Thanks

    Saturday, February 18, 2012 5:09 PM
  • Hello Kevininstructo

    your code also works but not on a proxy :(

    actually the problem is to link the internet in my application, when i use my app on my 2nd pc where the internet is connected through proxy server this error shows up,  but while on a dsl connection which is directly connected to internet it works fine.

    any idea to set a internet source settings in my app to access internet ? while on proxy server


    Thanks


    Saturday, February 18, 2012 5:10 PM
  • Hello Kevininstructo

    your code also works but not on a proxy :(

    actually the problem is to link the internet in my application, when i use my app on my 2nd pc where the internet is connected through proxy server this error shows up,  but while on a dsl connection which is directly connected to internet it works fine.

    any idea to set a internet source settings in my app to access internet ? while on proxy server


    Thanks


    From firsthand experience my company had to setup proxies to allow SMTP messages to be sent, otherwise it would not be possible as they have absolute control. My suggestion for you is to discuss this with those who are responsible for the proxy to allow SMTP message to be sent. If there were, a work around that would breach their security and would not be a wise idea to do so.


    KSG

    Saturday, February 18, 2012 5:30 PM
    Moderator
  • Thanks Kevininstructor

    i got it now thats the issue i am not able to send :) i did'nt think about it that the proxy server are setuped here to allow just http and https requests inside the isa server, means that i can not access the smtp untill they allow smtp request, am i right ?

    Thanks for suggestion one more thing i would like to know, if smtp is not allows so anyother way to send email over server like this ? socks /proxy internet, if yes then what is it ?

    or i have a solution for that, can send email using php, thorogh my web server, so i need to send http request to the mail file, if i do this way so how to request my mail.php file over my website from my application ?

    Thanks


    Ashfaq Ahmed Web Programmer + Software Engineer of the world where nothing is impossible :)

    Saturday, February 18, 2012 5:49 PM
  • The issue is not SMTP but instead how security is setup by whomever is responsible for security of the network and individual computers.

    As mentioned before if you figure out something and the security team is on their toes they will find out what you are doing and stop it. Not knowing policies for where this app runs you could get a slap on the wrist or worst case a heavy fine and time in jail.

    Bottom line is that how things are setup stopping email message outbound as done a) for security b) network security did not consider an app sending outbound messages thus goes back to my suggestion, talk to those responsible for an exception to the current rules.


    KSG

    Saturday, February 18, 2012 6:07 PM
    Moderator