Click here to Skip to main content
15,312,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
system.net.mail.smtpexception: Failure sending email.


What I have tried:

Good afternoon.
Hope you are all fine.

I just need some help regarding my project.

Everytime i try to send email using vb.net code i am receiving the error below.

"system.net.mail.smtpexception: Failure sending email. --->
System.IO.IOException: Unable to read data from transportconnection: net_io_connectionclosed. at System.Net.mail.SmtpReplyReaderFactory.ProcessRead(Byte buffer, Int32 offset, Int32 read, Boolean readline)"

Note: I am using Office365.

Really appreciate if someone can give any idea how can i resolve this issue.
Thank you very much in advance.


Sincrely,

Onel
Posted
Updated 19-Dec-21 2:23am

1 solution

Emails are a very notorious topic in software development, especially because they have different factors affecting the execution of your code—hostname, port, enable SSL, etc.

Since you've not shared the code, my guess is either you're not passing the enable SSL to your code, or the port is incorrect.

Please read the following articles that I wrote a while back that mention these challenges and (potentially) how to solve them:
Sending emails over .NET framework, and general problems – using C# code[^]
Universal SMTP Code to Send Emails in .NET Apps[^]
   
Comments
Leonel Masangcap 19-Dec-21 22:45pm
   
Hi Mr. Afzaal. Thank you very much for your reply. I will check the link you sent. Please check below code that i use for my project, please let me know if i did something wrong.

Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential(usrEmailID, usrEmailpw)
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.office365.com"

e_mail = New MailMessage()
e_mail.From = New MailAddress(usrEmailID)

e_mail.To.Add("sample@email.com")
e_mail.Subject = "Approval"
e_mail.IsBodyHtml = True
'attachment 1
Dim fileTXT As String = "c:\attach\" + invNo + ".pdf"
Dim data As Net.Mail.Attachment = New Net.Mail.Attachment(fileTXT)
e_mail.Attachments.Add(data)

'attachment 2
Dim fileTXT2 As String = "c:\attach\" + invNo + " - PL.pdf"
Dim data2 As Net.Mail.Attachment = New Net.Mail.Attachment(fileTXT2)
e_mail.Attachments.Add(data2)
e_mail.Body = "INFORMATION" + "" + "" + "<font-family:arial>Invoice No.: " + "" & invNo & "" + "<font-family:arial> needs to review. " + "" + "" & _
"Please log in to the system to check what are the points that needs to modify." + "" + "" & _
"For any assistance, contact the system administrator." + "" + "" + "Thank you." + "" + "" + "" & _
"This is an auto-generated email – please do not reply."
Smtp_Server.Send(e_mail)

Please note that the code above sometimes work, sometimes not.
Do you have any idea what's wrong with my code?

Again, thank you very much.

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