简体   繁体   English

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

[英]Deleting selected attachment from e-mail C#

I'm writting an e-mail sender using smtp protocol. 我正在使用smtp协议编写电子邮件发件人。 I select attachment using OpenFileDialog and then filename appears in attachmentListBox. 我使用OpenFileDialog选择附件,然后文件名出现在attachmentListBox中。

I used two lists to have file name and size. 我使用两个列表来获取文件名和大小。 When I select filename in listbox and click button called " Delete attachment " that should delete selected attachment from MailMessage , its name from attachmentListBox and size from sizeListBox. 当我在列表框中选择文件名并单击名为“ Delete attachment ”的按钮时,应从MailMessage删除选定的附件,从附件列表框中删除其名称,从大小列表框中删除其大小。 Last two things I have made but I don't know how to do the first one, because the error is shown 我做了最后两件事,但我不知道该怎么做,因为显示了错误

(MailMessage is always null). (MailMessage始终为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();

Try this solution. 试试这个解决方案。 you should get rid of error. 您应该摆脱错误。

  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;
                            }
                        }
                    }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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