简体   繁体   中英

add multiple attachements to one e-mail

i'm sending mails using following code.but i can add only one attachement. i want add more than 30 attachements with one mail. how can i solve this problem. please help me.

string fromEmail = "abcd@gmail.com ";
mail_add=recever@gmail.com
MailMessage mailMessage = new MailMessage(fromEmail, mail_add, subject, subject);
System.Net.Mail.Attachment attachement;

attachement = new System.Net.Mail.Attachment("path_with_xl_file");

mailMessage.Attachments.Add(attachement);
mailMessage.Body = body;
MailAddress copy = new MailAddress(fromEmail );
mailMessage.CC.Add(copy);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail, "mis123");

try
{
    smtpClient.Send(mailMessage);   
}

You can simply execute mailMessage.Attachments.Add multiple times:

mailMessage.Attachments.Add(new System.Net.Mail.Attachment("path_with_xl_file"));
mailMessage.Attachments.Add(new System.Net.Mail.Attachment("path_with_xl_file_2"));
mailMessage.Attachments.Add(new System.Net.Mail.Attachment("path_with_xl_file_3"));
mailMessage.Attachments.Add(new System.Net.Mail.Attachment("path_with_xl_file_4"));

Of course, you can simplify this code by utilizing arrays, loops and other language instruments. It depends on how you store these 30 files in your program.
For example, if you store all pathes in a string array, you can do:

string[] pathes = /* ... */;
foreach (var path in pathes)
{
    mailMessage.Attachments.Add(new System.Net.Mail.Attachment(path));
}

Here is a sample code to do it. This code assumes that you already have a collection of file paths to be converted in to Attachments .

        private static void SendEmail(IEnumerable<string> attachmentFilePaths)
        {
            string fromEmail = "abcd@gmail.com ";
            string mail_add = "recever@gmail.com";
            string subject = "hello";
            string body = "How are you?";

            try
            {
                using (MailMessage mailMessage = new MailMessage(fromEmail, mail_add, subject, body))
                {
                    foreach (var attachmentFilePath in attachmentFilePaths)
                    {
                        if (File.Exists(attachmentFilePath))
                        {
                            Attachment attachement = new Attachment(attachmentFilePath);
                            mailMessage.Attachments.Add(attachement);
                        }
                    }

                    MailAddress copy = new MailAddress(fromEmail);
                    mailMessage.CC.Add(copy);
                    using (SmtpClient smtpClient = new SmtpClient())
                    {
                        smtpClient.Host = "smtp.gmail.com";
                        smtpClient.Port = 587;
                        smtpClient.EnableSsl = false;
                        smtpClient.UseDefaultCredentials = false;
                        smtpClient.Credentials = new NetworkCredential(fromEmail, "mis123");
                        smtpClient.Send(mailMessage);
                    }
                }
            }catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM