The SMTP server requires a secure connection VB.NET - vb.net

Dim smtp As New SmtpClient
Dim mail As New MailMessage
smtp.Credentials = New Net.NetworkCredential("mail#gmail", "password")
mail.From = New MailAddress("mail#gmail.com")
mail.To.Add(totxt.Text$)
mail.Body = bodytxt.Text
If Not ListBox1.Items.Count <= 0 Then
Dim d As Integer
Dim attach As New Attachment(ListBox1.Items(d))
mail.Attachments.Add(attach)
End If
mail.Subject = subjecttxt.Text
smtp.EnableSsl = True
smtp.Port = "587"
smtp.Host = "smtp.gmail.com"
smtp.Send(mail)
smtp.Dispose()
done.Text = "Mail sent"
PictureBox4.BackgroundImage = My.Resources.tickfnl
dtls.Visible = False
I am trying to send email from my gmail account.But i am getting the error "The SMTP Server requires a secure connection".I even enabled LESS-SECURE APP login in my account settings.The password and email address is correct.I tried another email but same issue.Any fix ?
I TRIED ALL THE SOLUTIONS FROM THE DUPLICATE LINK,STILL THE SAME PROBLEM
**IF I REMOVE THIS LINE
smtl.enablessl=true
then i get this error
the server resposnse was 5.7.0 **
Fixed the error using EASendMail

Fixed it using EASendmail :
Panel6.Visible = True
done.Text = "Sending..."
''''''''''''''''''''''''
Dim oMail As New SmtpMail("TryIt")
Dim oSmtp As New EASendMail.SmtpClient()
oMail.From = fromtxt.Text
oMail.To = New AddressCollection(totxt.Text)
oMail.Subject = subjecttxt.Text
If html.CheckAlign = True Then
oMail.HtmlBody = bodytxt.Text
Else
oMail.TextBody = bodytxt.Text
End If
Dim oServer As New SmtpServer(MailConfig.host.Text)
oServer.Port = MailConfig.port.Text
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
oServer.User = fromtxt.Text
oServer.Password = MailConfig.password.Text
Dim r As Integer
If ListBox1.Items.Count <= 0 Then
Else
oMail.AddAttachment(ListBox1.Items(r))
End If
oSmtp.LogFileName = Application.StartupPath & "\maillog.log"
Try
oSmtp.SendMail(oServer, oMail)
done.Text = "Mail sent !"
PictureBox4.BackgroundImage = My.Resources.tickfnl
Catch ex As Exception
aa = MsgBox(ex.Message)
done.Text = "Sending failed."
PictureBox4.BackgroundImage = My.Resources.excll
End Try

Related

Mail is not being delivered

I am sending mail using SmtpClient. while attempting to send any-mail, I am getting success response. But mail is not getting delivered.
Dim mMailMessage As New Net.Mail.MailMessage
Dim client As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("domain", 587)
client.EnableSsl = True
client.Timeout = 100000
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
client.UseDefaultCredentials = False
client.Credentials = New NetworkCredential("username***", "password")
Dim msg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mMailMessage.[To].Add(pstrToMailid)
mMailMessage.From = New System.Net.Mail.MailAddress("frommailid")
mMailMessage.Subject = pstrSubject
mMailMessage.IsBodyHtml = True
mMailMessage.Body = text
If Not pstrfilenames Is Nothing Then
Dim lint As Integer
Dim larrattachment() As String
larrattachment = Split(pstrfilenames, ",")
For lint = 0 To larrattachment.Length - 2
attachment = New Net.Mail.Attachment(larrattachment(lint))
mMailMessage.Attachments.Add(attachment)
Next
End If
client.Send(mMailMessage)

Attach all files in a folder to an email

I am trying to make a ConsoleApplication attach all files in a folder to an email and send it. I know how to do it with a single attachment, but for the life of me, I cannot figure out how to attach all items in a folder.
Current code:
Sub Main()
Try
Dim mail As New MailMessage("from", "to")
Dim client As New SmtpClient()
client.Port = 25
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.UseDefaultCredentials = False
client.Host = ""
mail.Subject = "" + DateTime.Now.AddDays(-1).ToShortDateString()
mail.IsBodyHtml = True
mail.Body = "Test"
Dim file As System.Net.Mail.Attachment
file = New System.Net.Mail.Attachment("Path to single file")
mail.Attachments.Add(file)
client.Send(mail)
Return
Catch [error] As Exception
MsgBox("error")
Return
End Try
End Sub
Thanks in advance
EDIT:
I tried the below code that I found on another post, but it just errors out (and using the ConsoleApplication, I am not sure how to view the exact error its giving)
For Each filePath As String In Directory.GetFiles(My.Settings.FileLoc1)
Dim Attach As New Net.Mail.Attachment(filePath)
mail.Attachments.Add(Attach)
Next
I got it working!
Sub Main()
Try
Dim mail As New MailMessage("from", "too")
Dim client As New SmtpClient()
client.Port = 25
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.UseDefaultCredentials = False
client.Host = ""
mail.Subject = "" + DateTime.Now.AddDays(-1).ToShortDateString()
mail.IsBodyHtml = True
mail.Body = "Test"
For Each filePath As String In Directory.GetFiles(My.Settings.FileLoc1)
Dim Attach As New Net.Mail.Attachment(filePath)
mail.Attachments.Add(Attach)
Next
client.Send(mail)
Return
Catch [error] As Exception
MsgBox("error")
Return
End Try
End Sub

How to send mail with asp.net in vb.net

When I send an email
By pressing the button
No error will not be
Dim mm As MailMessage = New MailMessage("ashkanramedani1370#gmail.com", "ashkanramedani1370#gmail.com", "HELLO", "test")
Dim smtp As New SmtpClient("gmail.com")
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New System.Net.NetworkCredential()
NetworkCred.UserName = "ashkanramedani1370#gmail.com"
NetworkCred.Password = "123456789#"
smtp.UseDefaultCredentials = False
smtp.Credentials = NetworkCred
smtp.Port = 587
Try
smtp.Send(mm)
Catch ex As Exception
'----
End Try
But did not receive the email
Please help me
gmail.com is not an SMTP server.
You want smtp.gmail.com.

Email not send using Gmail smtp on host

I've used this code to sending emails. When I test this code in LOCALHOST, it works correctly, but in host, I get this error:
Failure sending mail.few
My code is:
Dim MailMsg As New MailMessage(New Net.Mail.MailAddress("MYUSERNAME#gmail.com"), New Net.Mail.MailAddress("myname#gmail.com"))
MailMsg.Subject = "test"
MailMsg.Body = "test"
MailMsg.IsBodyHtml = True
Dim SmtpMail As New Net.Mail.SmtpClient
Dim SMTPUserInfo As New Net.NetworkCredential()
SMTPUserInfo.UserName = "MYUSERNAME"
SMTPUserInfo.Password = "MYPASSWORD"
SmtpMail.UseDefaultCredentials = False
SmtpMail.Credentials = SMTPUserInfo
SmtpMail.Host = "smtp.gmail.com"
SmtpMail.Port = 587
SmtpMail.EnableSsl = True
SmtpMail.Send(MailMsg)
'Start by creating a mail message object
Dim MyMailMessage As New MailMessage()
'From requires an instance of the MailAddress type
MyMailMessage.From = New MailAddress("USERNAME#gmail.com")
'To is a collection of types
MyMailMessage.To.Add("DESTINATION#EXAMPLE.COM")
MyMailMessage.Subject = "XXXXXXX" & x
MyMailMessage.Body = "XXXXXXXXXXXXXXXXXXX "
Dim attach As New Attachment(fileName)
MyMailMessage.Attachments.Add(attach)
'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("USERNAME#gmail.com", "PASSWD")
SMTPServer.EnableSsl = True
SMTPServer.Send(MyMailMessage)
SMTPServer.Dispose()
MyMailMessage.Dispose()

Mailing Attempt Object Reference?

I am trying to make a simple e-mail sending program but it has confused me when it said object reference not set to an instance of an object
Try
Dim mail As MailMessage
Dim client As SmtpClient
Dim SmtpServer As New SmtpClient()
Dim fromAddress As String
Dim toAddress1 As String
Dim toAddress2 As String
Dim toAddress3 As String
Dim subject As String
Dim message As String
SmtpServer.Credentials = New Net.NetworkCredential("*********#gmail.com", "**********")
client.UseDefaultCredentials = False
fromAddress = FromEmail.Text
If ToEmail1.Visible = True Then
toAddress1 = ToEmail1.Text
ElseIf ToEmail1.Visible = True And ToEmail2.Visible = True Then
toAddress1 = ToEmail1.Text
toAddress2 = ToEmail2.Text
ElseIf ToEmail1.Visible = True And ToEmail2.Visible = True And ToEmail3.Visible = True Then
toAddress1 = ToEmail1.Text
toAddress2 = ToEmail2.Text
toAddress3 = ToEmail3.Text
End If
subject = "Subject"
Message = "Message"
mail = New MailMessage(fromAddress, toAddress1, subject, Message)
client = New SmtpClient("Smtp.live.com")
client.Port = 587
Dim SMTPUserInfo As New System.Net.NetworkCredential("user#hotmail.com", "password")
client.Credentials = SMTPUserInfo
client.Send(mail)
MsgBox("sent")
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
you're declaring two smtpclients
Dim client As SmtpClient
Dim SmtpServer As New SmtpClient()
you instantiated SmtpServer, but not client. Then you do
client.UseDefaultCredentials = False
I guess this is where the exception is raised.
the code looks like having some lines to send some mail through SmtpServer from a google account and other lines to send some mail through client from a hotmail account. Besides, SmtpServer is using some server defined in config.sys.