Multiple attachment files using C #

How to connect multiple files via 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 to attach only one file. How can I connect multiple files in C # 2008. ??? Plz give me a solution.

+2


source to share


5 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;
...      

      



+4


source


Message.Attachments

Multiple attachments can be added to the collection

FROM#:

Message.Attachments.Add(new System.Net.Mail.Attachment(strAttPath));

      



VB:

Message.Attachments.Add(New Net.Mail.Attachment(strAttPath))

      

Just type a .Add

few times, pointing to each attachment.

+4


source


Just add attachments to the mail.Attachments collection as you did above.

+2


source


How about releasing attachment files after submitting?

For example, you upload a temporary file used to create attachment content. This file is reused for this purpose. The attachment file must be released with dispose()

the attachment.

To accomplish this, first create an attachment to give it an object name that will be used later using the utility ().

Attachment attach = new Attachment(txtAttachments.Text.ToString());    
Message.Attachments.Add(attach);
...

attach.dispose();   

      

+1


source


protected void SendMail(List<string> attachments)
    {
        UserManagement Users = new UserManagement();
        Users.GetUserInformation();

        SmtpClient client = new SmtpClient(ip_address);
        MailMessage Message = new MailMessage();
        Message.From = new MailAddress(senderaddress);

        Message.To.Add(Users._CurUser_Destination_Email);
        Message.Subject = "Neue Umlagerung - " + cb_auflieger_limburg.SelectedItem.ToString();

        Message.Body = string.Format("Datum: {0}", DateTime.Now) + Environment.NewLine +
                                     "AufliegerNr.: " + cb_auflieger_limburg.SelectedItem.ToString() + Environment.NewLine +
                                     "Benutzer: " + Environment.UserName;

        client.UseDefaultCredentials = true;

        Attachment Attachment = null;

        try
        {
            foreach (string attachment in attachments)
            {
                Attachment = new Attachment(attachment);
                Message.Attachments.Add(Attachment);
            }

            client.Send(Message);
            Attachment.Dispose();
            Message.Dispose();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            foreach(string attachment in attachments)
            {
                //Dateien nach Versendung lรถschen
                FileInfo fi = new FileInfo(attachment);
                if (fi.Exists)
                {
                    fi.Delete();
                }
            }
        }
    }

      

The List attachments parameter is populated with a class that exports the DataGridView in various formats such as .csv and .pdf.

The "atttachments" list contains the folder and filename strings.

//Exporting to CSV.
string FileName = $"YourFileName_{datetime}.csv";
File.WriteAllText(ExportPath + FileName, csv);

AttachmentsToExport.Add(ExportPath + FileName);

      

0


source







All Articles