How to attach pdf in mail in vb.net

To attach a PDF file to an email in VB.NET, you can use the System.Net.Mail namespace. Here's an example code snippet:

Imports System.Net.Mail
Imports System.Net.Mime

' Create a new MailMessage object
Dim mail As New MailMessage("[email protected]", "[email protected]", "Subject", "Body")

' Create a new Attachment object
Dim attachment As New Attachment("C:\Path\To\Your\PDF\file.pdf")

' Add the attachment to the MailMessage object
mail.Attachments.Add(attachment)

' Set the content type of the attachment to PDF
attachment.ContentType = New ContentType("application/pdf")

' Send the email using SmtpClient
Dim smtp As New SmtpClient("smtp.example.com")
smtp.Send(mail)

Make sure to replace the placeholders with your actual email addresses, subject, body, and PDF file path.

You can also use the MailAttachment class from the System.Net.Mail namespace to attach the PDF file:

Dim mail As New MailMessage("[email protected]", "[email protected]", "Subject", "Body")
Dim attachment As New MailAttachment("C:\Path\To\Your\PDF\file.pdf")
mail.Attachments.Add(attachment)

If you want to attach a PDF file from a byte array, you can use the MemoryStream class:

Dim mail As New MailMessage("[email protected]", "[email protected]", "Subject", "Body")
Dim pdfBytes() As Byte = File.ReadAllBytes("C:\Path\To\Your\PDF\file.pdf")
Dim ms As New MemoryStream(pdfBytes)
Dim attachment As New Attachment(ms, "file.pdf")
mail.Attachments.Add(attachment)

Make sure to dispose of the MemoryStream object when you're done with it.

Also, you can use third-party libraries like EPPlus or iTextSharp to create and attach PDF files programmatically.

Note: The above code snippet is for.NET Framework, if you're using.NET Core, you can use the System.Net.Mail namespace as well, but you might need to use the IEmailService interface to send the email.