Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
VB
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("alvikashif29@yahoo.com", "alvi_kashif@rocketmail.com", "Test Email", "Vb .net Appliction sent this mail", "smtp.gmail.com", False, 465, , "alvikashif29@gmail.com", "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))
            Next
        End If
        MailClient.Send(MailMessage)
        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 = smtp.gmail.com
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
Updated 22-Apr-13 21:45pm
v2

1 solution

You have to use 587 port.
Follow my answer sending email to gmail from asp.net[^] 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 https://accounts.google.com/DisplayUnlockCaptcha[^], click continue button and try to send mail after that from the application.
 
Share this answer
 
Comments
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.

Thanks,
Tadit

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900