Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple attachment file in email using C#

Tags:

c#

How to attach the multiple files in an email using c#.

        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

        //get the userID, Pass
        userID= register.userName;
        password = register.pass;


        string aa=txtTo.Text;
        mail.From = new MailAddress(userID);
        mail.To.Add(aa);
        mail.Subject = txtsubject.Text;
        mail.Body = txtComments.Text;

        //Attach file
        mail.Attachments.Add(new Attachment(txtAttachments.Text.ToString()));       
        SmtpServer.Port = 587;
        SmtpServer.UseDefaultCredentials = false;
        SmtpServer.Credentials = new System.Net.NetworkCredential(userID, password);
        SmtpServer.EnableSsl = true;
        SmtpServer.Send(mail);
        MessageBox.Show("Email sent successfully");
        this.Cursor = Cursors.Default;

        //close the page
        Email email = new Email();
        email.Close();

this code is used for attach one file only. How can i attach multiple files in c# 2008.??? Plz give me the solution.

like image 925
nithi Avatar asked Jun 04 '26 20:06

nithi


1 Answers

...
mail.Body = txtComments.Text;
//Attach file
mail.Attachments.Add(new Attachment(txtAttachments.Text.ToString()));
mail.Attachments.Add(new Attachment(txtAttachments2.Text.ToString()));
mail.Attachments.Add(new Attachment(txtAttachments3.Text.ToString()));
mail.Attachments.Add(new Attachment(txtAttachments4.Text.ToString()));
SmtpServer.Port = 587;
...      
like image 130
David Avatar answered Jun 07 '26 11:06

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!