system.net.mail multiple attachments

I can send email with one attachment. I want to be able to send mulitple attachments. I show the files in a textbox delimited by a semi-colon and then strip it off before adding to mail message. In the messagebox.show(str) the path is correct, after stripping off the semi-colon.

I am getting System.ArgumentException: The path is not of a legal form. Details below.
What am I doing wrong or is there a better way. Thank you.

private void Attachbutton_Click(object sender, EventArgs e)

{OpenFileDialog fdlg = new OpenFileDialog();

fdlg.Title = "Attach File";

fdlg.InitialDirectory = "d:\\Dancing\\";

fdlg.Filter = "All files (*.*)|*.*";

fdlg.Multiselect = true;

fdlg.FilterIndex = 1;

if (fdlg.ShowDialog() == DialogResult.OK)

{foreach (string fName in fdlg.FileNames)

AttachTextBox.Text += fName + "; ";

}}

In the message send...

string attachments = AttachTextBox.Text;

char[] separator = {';'};

string[] myattach;

myattach = attachments.Split(separator);

if (attachments != null)

{if (attachments.Length != 0)

{foreach (string str in myattach)

{msgMail.Attachments.Add(new Attachment(Path.GetFullPath(str)));

MessageBox.Show(str);

}}}

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentException: The path is not of a legal form.
at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
at System.IO.Path.GetFullPathInternal(String path)
at System.IO.Path.GetFullPath(String path)
at CuersChoice2.EmailForm.toolStripButton1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Carol.MAIN\My Documents\Visual Studio 2005\Projects\CuersChoice2\CuersChoice2\EmailForm.cs:line 46
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)



Answer this question

system.net.mail multiple attachments

  • Wraith10

    Because of the nature of how you are dividing the name of each attachment, with a semicolon and then a space and then splitting it based on the semicolon... you are ending up with an extra item in your list which is just a space and that GetFullPath() cannot make sense of.

    If we modify your actual attachment adding block with a simple if statement ala:

    foreach (string str in myattach)
    {
    if (str.Trim() != "")
    {
    msgMail.Attachments.Add(new Attachment(Path.GetFullPath(str)));

    MessageBox.Show(str);
    }
    }

    You are then removing any leading or trailing spaces from the string name while comparing it to an empty string which will detect and not add your empty file.



  • Simon De Vries

    It works beautifully. Thank you for your detailed explanation. It is greatly appreciated!
  • system.net.mail multiple attachments