Click here to Skip to main content
11,928,078 members (28,540 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB Windows Application VB.NET , +
Imports System.Web
Imports System.IO
Imports System.Net.Mail
Imports System.Net.Mail.Attachment
Imports System.Runtime
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                SendMail("", "", "Test Email", "Vb .net Appliction sent this mail", "", False, 465, , "", "xxxxxxxxx")
    End Sub
    ''one static method for sending e-mails
    Public Sub SendMail(ByVal [From] As String, ByVal [To] As String, _
                        ByVal Subject As String, ByVal Body As String, ByVal MailServer _
                        As String, Optional ByVal IsBodyHtml As Boolean = True, _
                        Optional ByVal MailPort As Integer = 25, _
                        Optional ByVal Attachments() As String = Nothing, Optional _
                        ByVal AuthUsername As String = Nothing, Optional ByVal _
                        AuthPassword As String = Nothing)
        ''create a SmtpClient object to allow applications to send 
        ''e-mail by using the Simple Mail Transfer Protocol (SMTP).
        Dim MailClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient(MailServer, MailPort)
        ''create a MailMessage object to represent an e-mail message
        ''that can be sent using the SmtpClient class
        Dim MailMessage = New System.Net.Mail.MailMessage([From], [To], Subject, Body)
        ''sets a value indicating whether the mail message body is in Html.
        MailMessage.IsBodyHtml = IsBodyHtml
        ''sets the credentials used to authenticate the sender
        If (AuthUsername IsNot Nothing) AndAlso (AuthPassword IsNot Nothing) Then
            MailClient.UseDefaultCredentials = False
            MailClient.EnableSsl = True
            MailClient.Credentials = New System.Net.NetworkCredential(AuthUsername, AuthPassword)
        End If
        ''add the files as the attachments for the mailmessage object
        If (Attachments IsNot Nothing) Then
            For Each FileName In Attachments
                MailMessage.Attachments.Add(New System.Net.Mail.Attachment(FileName))
        End If
        MessageBox.Show("mail sent")
    End Sub
End Class

This my code on a new window form with a button on it on click event the sendmail is called with appropriate parameters but when running it an exception occurs
"The operation has timed out"
I am using my gmail account
the smtp server is =
and the port for ssl is 465
(now dont tell me to use 587 as the port because it also does not works. It gives an error
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.")
Please help me out of this situation please.
Posted 22-Apr-13 22:26pm
Edited 22-Apr-13 22:45pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You have to use 587 port.
Follow my answer sending email to gmail from[^] and check whether you are doing the same or not, else format the code like that.

Make sure the username and password are correct.

For the exception you are getting...
Google may prevent an application from accessing your account
Here you can enable applications to access google account with your credentials:
Access[^], click continue button and try to send mail after that from the application.
Tadit Dash 26-Apr-13 1:51am
Hello Kashif Alvi,

You have accepted the answer and again rejected it. Is there any problem ? Or this is done by mistake ?
Please accept the answer if it has helped you.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100