Net-informations.com
SiteMap  | About    

VB.NET Email Attachment

The System.Net classes uses to communicate with other applications by using the HTTP, TCP, UDP, Socket etc. Microsoft .Net languages uses System.Net.Mail namespace for sending email . From the previous chapters we learned how to send an email with a text body. Here we are trying to send an eamil with attachment.

  Dim attachment As System.Net.Mail.Attachment
  attachment = New System.Net.Mail.Attachment("your attachment file")
  mail.Attachments.Add(attachment)

The following VB.NET source code shows how to send an email with an attachment from a Gmail address . The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 . Here using NetworkCredential for password based authentication.

  SmtpServer.Port = 587
  SmtpServer.Credentials = New System.Net.NetworkCredential("username", "password")
  SmtpServer.EnableSsl = True



Imports System.Net.Mail

	Public Class Form1

		Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
			Dim mail As New MailMessage()
			Dim SmtpServer As New SmtpClient("smtp.gmail.com")
			mail.From = New MailAddress("your_email_address@gmail.com")
			mail.[To].Add("to_address")
			mail.Subject = "Test Mail - 1"
			mail.Body = "mail with attachment"

			Dim attachment As System.Net.Mail.Attachment
			attachment = New System.Net.Mail.Attachment("your attachment file")
			mail.Attachments.Add(attachment)

			SmtpServer.Port = 587
			SmtpServer.Credentials = New System.Net.NetworkCredential("username", "password")
			SmtpServer.EnableSsl = True

			SmtpServer.Send(mail)
			MessageBox.Show("mail Send")
		End Sub
	End Class






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.