0

I'm writting an e-mail sender using smtp protocol. I select attachment using OpenFileDialog and then filename appears in 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. 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 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;
                        }
                    }
                }}                                                           
user9405863
  • 1,506
  • 1
  • 11
  • 16
KCPR
  • 5
  • 2

2 Answers2

1

You haven't initialized the MailMessage like:

msg = new MailMessage();
Pang
  • 9,564
  • 146
  • 81
  • 122
1

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;
                            }
                        }
                    }
}  
user9405863
  • 1,506
  • 1
  • 11
  • 16
  • msg.Attachments.RemoveAt(attachementListBoxindex); I get System.ArgumentOufOfRangeException index out of range. – KCPR Mar 12 '18 at 18:52
  • updated answer. we need to iterate with 'i' to remove attachment at index i – user9405863 Mar 12 '18 at 19:13
  • But then i got an error "The name "i" does not exist in the current context" – KCPR Mar 12 '18 at 20:02
  • I tried it some time ago but it also showed an error "index out of range". I have checked index and "i" has the same as attachmentListBoxindex I used in my code above. So my question is how attachments are storaged in MailMessage? Because I cannot find information about it. – KCPR Mar 12 '18 at 20:50
  • I had to change for loop with z variable: for (int z = nameAttachement.Count-1; z >=0; z--) and then it started to work in both cases. Thank you for help :) – KCPR Mar 12 '18 at 21:11
  • its good that you are able to solve your issue. feel free vote for my answer. – user9405863 Mar 12 '18 at 21:16