Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 22-Apr-13 22:26pm
Edited 22-Apr-13 22:45pm
v2

1 solution

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

Solution 1

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.
Comments
Tadit Dash at 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.
 
Thanks,
Tadit

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

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Praneet Nadkar 197
3 Sergey Alexandrovich Kryukov 190
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
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