从电子邮件 C# 中删除选定的附件

Deleting selected attachment from e-mail C#

我正在使用 smtp 协议编写电子邮件发件人。我使用 OpenFileDialog select 附件,然后文件名出现在 attachmentListBox 中。

我使用了两个列表来获得文件名和大小。当我在列表框中 select 文件名并单击名为“Delete attachment”的按钮时,应该从 MailMessage 中删除 selected 附件,它的名称从 attachmentListBox 中删除,大小从 sizeListBox 中删除。我做了最后两件事,但我不知道如何做第一件事,因为显示错误

(MailMessage is always null).

MailMessage msg; //MailMessage is always nul
List<int> sizeAttachement = new List<int>();
List<string> nameAttachement = new List<string>();
if(ofd.ShowDialog()==DialogResult.OK) 
{

    path = ofd.FileName.ToString();
    FileInfo info = new FileInfo(ofd.FileName);
    sizeAttachement.Add(Convert.ToInt32(info.Length / (1024 * 1024)));
    nameAttachement.Add(ofd.FileName);
}
private void delAtchButton_Click(object sender, EventArgs e)
{
     if (attachementListBox.SelectedIndex == -1)
     {

     }
     else
     {
            ListBox.SelectedObjectCollection selectedItems = new 
            ListBox.SelectedObjectCollection(attachementListBox);
            selectedItems = attachementListBox.SelectedItems;
            if (attachementListBox.SelectedIndex != -1)
            {
                int attachementListBoxindex = attachementListBox.SelectedIndex;
                for (int i = selectedItems.Count - 1; i >= 0; i--)
                attachementListBox.Items.Remove(selectedItems[i]);
                msg.Attachments.RemoveAt(attachementListBoxindex); //Error always occurs     

              attachementProgressBar.Increment(-sizeAttachement[attachementListBoxindex]);
                sizeAttachement.RemoveAt(attachementListBoxindex);
                procentage = attachementProgressBar.Value * 4;
                procentageLabel.Text = Convert.ToString(procentage) + "%";


                for (int z = 0; z <= nameAttachement.Count; z++)
                {
                    foreach (Attachment attachment in msg.Attachments)
                    {
                        if (attachment.Name == Convert.ToString(nameAttachement[z]))
                        {
                            msg.Attachments.Remove(attachment); //Error to
                            break;
                        }
                    }
                }}                                                           

您还没有像这样初始化 MailMessage:

msg = new MailMessage();

试试这个解决方案。你应该摆脱错误。

  MailMessage msg  = new MailMessage();
    List<int> sizeAttachement = new List<int>();
    List<string> nameAttachement = new List<string>();
    if(ofd.ShowDialog()==DialogResult.OK) 
    {

        path = ofd.FileName.ToString();
        FileInfo info = new FileInfo(ofd.FileName);
        sizeAttachement.Add(Convert.ToInt32(info.Length / (1024 * 1024)));
        nameAttachement.Add(ofd.FileName);
    }
    private void delAtchButton_Click(object sender, EventArgs e)
    {
         if (attachementListBox.SelectedIndex == -1)
         {

         }
         else
         {
                ListBox.SelectedObjectCollection selectedItems = new 
                ListBox.SelectedObjectCollection(attachementListBox);
                selectedItems = attachementListBox.SelectedItems;
                if (attachementListBox.SelectedIndex != -1)
                {
                    int attachementListBoxindex = attachementListBox.SelectedIndex;
                    for (int i = selectedItems.Count - 1; i >= 0; i--)
                    {
                    attachementListBox.Items.Remove(selectedItems[i]);
                    msg.Attachments.RemoveAt(i); //Error always occurs                
                    attachementProgressBar.Increment(-sizeAttachement[i]);
                    sizeAttachement.RemoveAt(i);
                    procentage = attachementProgressBar.Value * 4;
                    procentageLabel.Text = Convert.ToString(procentage) + "%"; }

                    for (int z = 0; z <= nameAttachement.Count; z++)
                    {
                        foreach (Attachment attachment in msg.Attachments)
                        {
                            if (attachment.Name == Convert.ToString(nameAttachement[z]))
                            {
                                msg.Attachments.Remove(attachment); //Error to
                                break;
                            }
                        }
                    }
}