The System.Net Classes Uses To Communicate With Other ...

VB.NET Email Attachment By: Rajesh P.S.

The System.Net classes offer a range of functionalities for communication with other applications, including HTTP, TCP, UDP, and Socket protocols. In the context of sending emails, Microsoft .NET languages rely on the System.Net.Mail namespace. In previous chapters, we learned how to send emails containing only text content. However, in this section, our focus is on sending emails with attachments, which adds an extra layer of complexity.

System.Net.Mail namespace

When sending an email with an attachment, we need to utilize the classes and methods provided by the System.Net.Mail namespace to handle the attachment-related functionality. This includes selecting and attaching the desired file or files to the email message.

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

By employing the appropriate methods and properties, such as the Attachment class, we can associate one or more files with the email. This allows for the transmission of not only text content but also additional files or documents. The attachments could be in various formats, such as images, documents, or compressed files.

To send an email with attachments, it is crucial to follow the correct syntax and use the appropriate classes and methods provided by the System.Net.Mail namespace. By understanding the concepts and utilizing the available tools, developers can effectively incorporate attachment functionality into their email sending applications, enabling the seamless transmission of both text content and additional files.

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 .

NetworkCredential for password based authentication

SmtpServer.Port = 587 SmtpServer.Credentials = New System.Net.NetworkCredential("username", "password") SmtpServer.EnableSsl = True Full Source VB.NET 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("[email protected]") 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 Next > How to VB.Net Web Browser Related Topics
  1. How to send email from VB.NET
  2. VB.NET Send email using CDOSYS
  3. How to find IP Address of Host
  4. How to read a URL Content
More Related Topics.....
  1. VB.NET Socket Programming
  2. VB.NET Server Socket Program
  3. VB.NET Client Socket Program
  4. VB.NET MultiThreaded Socket Programming
  5. VB.NET MultiThreaded Server Socket Programming
  6. VB.NET MultiThreaded Client Socket Programming
  7. VB.NET Chat Server Program
  8. VB.NET Chat Server
  9. VB.NET Chat Client
  10. How to VB.Net Web Browser
Related Links
  1. VB.NET Checking Internet Connection
  2. How to validate email address in VB.NET
  3. VB.NET URL Parsing

Từ khóa » Visual Basic Send Email With Attachment