Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Imports System.Net.Mail
Public Class Form1
    Public Property EnableSsl As Boolean
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()

            SmtpServer.Credentials = New Net.NetworkCredential("username", "pwd")
            SmtpServer.Port = 993
            SmtpServer.Timeout = 30000
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = True
            mail = New MailMessage()
            mail.From = New MailAddress("username.com")
            mail.To.Add("toadress")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from GMAIL"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class



Unable to read data transport layer error c
Posted
Updated 18-Aug-14 11:29am
v2
Comments
Kschuler 18-Aug-14 17:30pm    
Which line of code causes the error? And did you try googling the error message?

https://www.google.com/search?q=Unable+to+read+data+transport+layer+error+c&rlz=1C1EODB_enUS581US581&oq=Unable+to+read+data+transport+layer+error+c&aqs=chrome..69i57&sourceid=chrome&es_sm=122&ie=UTF-8
Sergey Alexandrovich Kryukov 18-Aug-14 18:45pm    
"toaddress" is not a valid mail address, so what would you expect? :-)
—SA
[no name] 18-Aug-14 19:00pm    
And you would need to use the correct port.

1 solution

Imports System.IO
Imports System.Net
Imports System.Net.Mail

Public Class Mail

	Public Shared Sub Send(fromEmail As String, toEmail As String, subject As String, body As String)

		Using mm As New MailMessage(fromEmail, toEmail)
			mm.Subject = subject
			mm.IsBodyHtml = True
			mm.Body = body
			'If fuAttachment.HasFile Then
			'	Dim FileName As String = Path.GetFileName(fuAttachment.PostedFile.FileName)
			'	mm.Attachments.Add(New Attachment(fuAttachment.PostedFile.InputStream, FileName))
			'End If
			mm.IsBodyHtml = False
			Dim smtp As New SmtpClient()
			smtp.Host = "smtp.gmail.com"
			smtp.EnableSsl = True
			Dim NetworkCred As New NetworkCredential(fromEmail, "ThatPasswordOfYours")
			smtp.UseDefaultCredentials = True
			smtp.Credentials = NetworkCred
			smtp.Port = 587
			smtp.Send(mm)
		End Using
	End Sub

End Class
 
Share this answer
 

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