Error Sending email





    Please go through my code and tell me where i am making mistake.


    Dim dConn As New OleDbConnection

    Dim dContactsDA As New OleDbDataAdapter

    Dim dReader As OleDbDataReader

    Dim s As String()


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

    dConn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & txtDatabase.Text


    dContactsDA.SelectCommand = New OleDbCommand("SELECT * FROM kishan", dConn)

    dContactsDA.SelectCommand.CommandType = CommandType.Text

    dReader = dContactsDA.SelectCommand.ExecuteReader

    Send("smtp.gmail.com", "kishanhathiwala@hotmail.com")' dummy email address

    End Sub


    Public Function Send(ByVal server As String, ByVal dTo As String) As Boolean


    Dim client As New SmtpClient(server)

    Dim txt As String = Now.Millisecond

    Dim mail As New MailMessage()

    'set the addresses

    mail.From = New MailAddress("khathiwala@gmail.com")

    'set the content

    mail.Subject = "This is an email"

    mail.Body = "this is the body content of the email."


    client.Credentials = New Net.NetworkCredential("khathiwala@gmail.com", "********")

    'client.Port = 465

    client.EnableSsl = True

    client.Timeout = 1000 * 60 * 10

    AddHandler client.SendCompleted, AddressOf client_SendCompleted

    client.SendAsync(mail, txt)

    Return True

    Catch ex As Exception

    txtLog.AppendText("Invalid Operation : " & Chr(10) & "" + ex.ToString() & ControlChars.CrLf)

    Return False

    End Try

    End Function


    Private Sub client_SendCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

    Dim d As String = e.UserState.ToString

    If Not e.Error Is Nothing Then

    txtLog.AppendText(e.Error.ToString & ControlChars.CrLf)


    txtLog.AppendText(d & ControlChars.CrLf)

    End If


    End Sub


    Public Sub dSend()

    While dReader.Read


    Send("smtp.gmail.com", dReader(0))


    End While

    End Sub




    After sending 1 to 3 emails, i receive the following errors and then my program stop sending email

    "System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.Sockets.SocketException: An invalid argument was supplied
       at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)
       at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
       --- End of inner exception stack trace ---"


    I've done lots of search on net in many search engines and many forums but i am not able to figure it out the reason of error. I've changed my coding in many ways but still all in-vain. Kindly help me with the error.





    Thursday, December 20, 2007 1:43 PM

All replies



    Firstly, I'd like to say that it is a pity nobody has gotten back to you in regard to this.


    Now I have had a similiar problem, but I think in a different environment. My environment is ASP.NET on IIS.

    I think yours is Winforms or Console.


    What I have discovered is that the error is occurring because the Smtp server:"smtp.gmail.com", in your case cannot be connected. But you probably knew that.


    I don't know whether this will help you but here is what I did in IIS


    In the IIS "Default SMTP Virtual Server", Properties -> Delivery tab->Advanced there is a field for "Smart Host"

    I set that field to my remote Smtp server:"smtp.gmail.com", in your case.


    Then in the config file I changed the host="" under Network. As in the section below




    <smtp deliveryMethod="Network" from="MyAddress@MyServer.com.au">

    <network host="" port="25" userName="MyAddress@MyServer.com.au" password="MyPassword" defaultCredentials="true"/>






    I suspect that you are not using IIS or ASP.NET. But you can get your Network Administrator to help you.

     He may be able to route your traffic through another SMTP server. You can then address that local SMTP server and allow it to re-route your smtp.gmail.com traffic.

     Also get him to read up on the Help that goes with the IIS dialog, IIS "Default SMTP Virtual Server", Properties -> Delivery tab. It may give him more hints. especially the bit on Smart Host.





    Monday, May 12, 2008 5:59 AM