Imports System.Net
Imports System.Net.Mail
Public Class Form1
Sub sendMail()
Try
Dim AnEmailMessage As New MailMessage
AnEmailMessage.From = New MailAddress(some email address)
AnEmailMessage.To.Add(some email address)
AnEmailMessage.Subject = ("ho")
AnEmailMessage.Body = ("what")
AnEmailMessage.Priority = MailPriority.High
Dim SimpleSMTP As New SmtpClient("smtp.gmail.com")
With SimpleSMTP
.Port = 587
.EnableSsl = True
.Credentials = _
New NetworkCredential(username,password)
.Send(AnEmailMessage)
End With
MsgBox("Email sent to : emails ", MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
End class
I use this code to send an email through gmail account but the thing I want to do is, I want to check whether the given email and password exists or not and if it exists then pop out a text box with the body of the mail and type the text and then send it as a mail. But in the above code you have to log in and type body and when you send the mail , It verifies for the email and password while sending and if anything goes wrong says there is no such gmail account.but i want to check it whether the email and password exists or not, when the user enters the email and password at the beginning itself.So that he enters the right email to log on to compose a mail.Is there any way to check whether the given email and password is correct at the beginning only ?
Might not be the best way of doing it, but I suppose you could just send an email to a broken recipient when you want to validate the username/password.
I think that if you get an
SmtpFailedRecipientsException, then it has passed authentication, but if you get anSmtpExceptionthen check the StatusCode, and if it’sClientNotPermitted, the username or password are probably wrong.Otherwise you could probably just connect with TcpClient and send the commands as specified in RFC 4954, but that would be a bit more low level so might be a bit more work.