【问题标题】:Multiple attachment file in email using C#使用 C# 的电子邮件中的多个附件文件
【发布时间】:2018-12-12 18:37:17
【问题描述】:

如何使用 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();

此代码仅用于附加一个文件。如何在 c# 2008 中附加多个文件。??? 请给我解决方案。

【问题讨论】:

    标签: c#


    【解决方案1】:
    ...
    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;
    ...      
    

    【讨论】:

    • @David 如何添加动态数量的附件?
    【解决方案2】:

    可以将多个附件添加到Message.Attachments 集合中

    C#:

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

    VB:

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

    只需多次调用.Add,指向每个附件。

    【讨论】:

    • 非常感谢!你帮我解决这个问题。 +1 - 我编辑了你的问题(对不起),因为你的解决方案是在 vb 中,我必须将它翻译成 c#,因为问题被评论了。不过还是非常感谢!
    【解决方案3】:

    只需像上面那样向 mail.Attachments 集合添加更多附件。

    【讨论】:

      【解决方案4】:

      发送后释放附件文件怎么办?

      例如,您发送一个用于创建附件内容的临时文件。该文件被反复用于此目的。附件文件需要在附件上使用dispose() 发布。

      为此,首先创建附件,然后为其指定一个对象名称,以便稍后与 dispose() 一起使用。

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

      【讨论】:

        【解决方案5】:
        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();
                        }
                    }
                }
            }
        

        List attachments 参数由以不同格式(如 .csv 和 .pdf)导出 DataGridView 的类填充

        “附件”列表包含文件夹和文件名的字符串。

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

        【讨论】:

          猜你喜欢
          • 2012-09-03
          • 1970-01-01
          • 2011-01-16
          • 2012-10-21
          • 2013-07-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-14
          相关资源
          最近更新 更多