Attaching multiple image as attachement in mail using asp.net

A classic question!

To attach multiple images as attachments in an email using ASP.NET, you can use the System.Net.Mail namespace and the MailMessage and Attachment classes. Here's an example:

Step 1: Create a MailMessage object

Create a new instance of the MailMessage class and set the From, To, Subject, and Body properties as needed.

MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Multiple Image Attachments";
mail.Body = "This is a test email with multiple image attachments.";

Step 2: Create an array of Attachment objects

Create an array of Attachment objects, each representing a separate image file. You can use the Attachment constructor to specify the file path and name.

string[] imagePaths = new string[] { @"C:\Path\To\Image1.jpg", @"C:\Path\To\Image2.jpg", @"C:\Path\To\Image3.jpg" };
Attachment[] attachments = new Attachment[imagePaths.Length];

for (int i = 0; i < imagePaths.Length; i++)
{
    attachments[i] = new Attachment(imagePaths[i], "image/jpeg");
}

Step 3: Add the attachments to the MailMessage object

Use the Attachments property of the MailMessage object to add the array of Attachment objects.

mail.Attachments.AddRange(attachments);

Step 4: Send the email

Use the SmtpClient class to send the email. You'll need to specify the SMTP server settings, such as the server address, port, and credentials.

SmtpClient client = new SmtpClient();
client.Host = "your-smtp-server.com";
client.Port = 587;
client.Credentials = new NetworkCredential("[email protected]", "your-password");

client.Send(mail);

Here's the complete code example:

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

public class SendEmailWithMultipleAttachments
{
    public void SendEmail()
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("[email protected]");
        mail.To.Add("[email protected]");
        mail.Subject = "Multiple Image Attachments";
        mail.Body = "This is a test email with multiple image attachments.";

        string[] imagePaths = new string[] { @"C:\Path\To\Image1.jpg", @"C:\Path\To\Image2.jpg", @"C:\Path\To\Image3.jpg" };
        Attachment[] attachments = new Attachment[imagePaths.Length];

        for (int i = 0; i < imagePaths.Length; i++)
        {
            attachments[i] = new Attachment(imagePaths[i], "image/jpeg");
        }

        mail.Attachments.AddRange(attachments);

        SmtpClient client = new SmtpClient();
        client.Host = "your-smtp-server.com";
        client.Port = 587;
        client.Credentials = new NetworkCredential("[email protected]", "your-password");

        client.Send(mail);
    }
}

Note that you'll need to replace the placeholders ([email protected], your-password, your-smtp-server.com, etc.) with your actual email settings.