How to mail pdf in asp.net

There are a few ways to mail a PDF file in ASP.NET. Here are a few approaches:

Method 1: Using the System.Net.Mail namespace

You can use the System.Net.Mail namespace to send an email with a PDF attachment. Here's an example:

using System.Net.Mail;
using System.Net;

// Create a new MailMessage object
MailMessage mail = new MailMessage();

// Set the sender and recipient
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");

// Set the subject and body of the email
mail.Subject = "PDF Attachment";
mail.Body = "This is a test email with a PDF attachment";

// Create a new attachment
Attachment attachment = new Attachment("path/to/your/pdf/file.pdf", "application/pdf");

// Add the attachment to the email
mail.Attachments.Add(attachment);

// Send the email
SmtpClient client = new SmtpClient();
client.Send(mail);

Method 2: Using a third-party library

You can use a third-party library such as MailKit or MimeKit to send emails with attachments. Here's an example using MailKit:

using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

// Create a new email message
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Your Name", "[email protected]"));
message.To.Add(new MailboxAddress("Recipient Name", "[email protected]"));
message.Subject = "PDF Attachment";
message.Body = new TextPart("plain") { Text = "This is a test email with a PDF attachment" };

// Create a new attachment
var attachment = new MimePart("application/pdf");
attachment.Content = new MimeContent(File.OpenRead("path/to/your/pdf/file.pdf"));
attachment.FileName = "your_pdf_file.pdf";

// Add the attachment to the email
message.Attachments.Add(attachment);

// Send the email
using (var client = new SmtpClient())
{
    client.Connect("smtp.example.com", 587, SecureSocketOptions.StartTls);
    client.Authenticate("[email protected]", "your_password");
    client.Send(message);
    client.Disconnect(true);
}

Method 3: Using a file upload control

You can also use a file upload control to allow users to upload a PDF file, and then send the file as an email attachment. Here's an example:

<asp:FileUpload ID="fileUpload" runat="server" />

// In the code-behind
protected void Button1_Click(object sender, EventArgs e)
{
    if (fileUpload.HasFile)
    {
        // Save the uploaded file to a temporary location
        string filePath = Server.MapPath("~/temp/") + fileUpload.FileName;
        fileUpload.SaveAs(filePath);

        // Send the email with the PDF attachment
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("[email protected]");
        mail.To.Add("[email protected]");
        mail.Subject = "PDF Attachment";
        mail.Body = "This is a test email with a PDF attachment";
        Attachment attachment = new Attachment(filePath, "application/pdf");
        mail.Attachments.Add(attachment);
        SmtpClient client = new SmtpClient();
        client.Send(mail);

        // Delete the temporary file
        File.Delete(filePath);
    }
}

Note that in all of these examples, you'll need to replace the placeholders with your own email address, password, and PDF file path. Additionally, you may need to adjust the code to fit your specific requirements and environment.