How to send Email with multiple attachments in asp.net
Complete Code to send the single attachment in email you can use the below code
public static bool SendEMail(string to, string from, string subject, string body, string attachments)
{
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("info@infoa2z.com");
mail.To.Add(to);
//set the content
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
if (attachments.Length > 0)
{
if (System.IO.File.Exists(attachments.Trim()))
mail.Attachments.Add(new Attachment(attachments.Trim()));
}
//Authenticate we set the username and password properites on the SmtpClient
SmtpClient client = new SmtpClient("smtp.cmyfleet.com");
NetworkCredential credential = new NetworkCredential("info@infoa2z.com", "XXXXXXXX");
client.UseDefaultCredentials = false;
client.Credentials = credential;
client.Send(mail);
return true;
}
To send the Multiple attachment in email ASP.Net
public bool SendEMail(string to, string from, string subject, string body, string attachments)
{
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("info@infoa2z.com");
mail.To.Add(to);
//set the content
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
if (attachments.Length > 0)
{
if (attachments != string.Empty)
mail = AddAttachments(attachments, message);
}
//Authenticate we set the username and password properites on the SmtpClient
SmtpClient client = new SmtpClient("smtp.cmyfleet.com");
NetworkCredential credential = new NetworkCredential("info@infoa2z.com", "XXXXXXXX");
client.UseDefaultCredentials = false;
client.Credentials = credential;
client.Send(mail);
return true;
}
Add Multiple attachment in email ASP.Net
private MailMessage AddAttachments(string attachments, MailMessage mail)
{
if (attachments.IndexOf(",") != -1 || attachments.IndexOf(";") != -1)
{
char sep = ',';
if (attachments.IndexOf(",") != -1)
sep = ',';
else
sep = ';';
string[] temp = attachments.Split(sep);
int count= 0;
while (count< temp.Length)
{
if (System.IO.File.Exists(temp[count].ToString().Trim()))
mail.Attachments.Add(new Attachment(temp[count].ToString().Trim()));
else
throw new IOException("Attachment file not found");
count++;
}
}
else if (attachments.Length > 0)
{
if (System.IO.File.Exists(attachments.Trim()))
mail.Attachments.Add(new Attachment(attachments.Trim()));
else
throw new IOException("Attachment file not found");
}
return mail;
}
Related Alrticles