0

I'm trying to send an email with my application throug SMTPS, not supported by System.Net, but supported in System.Web.Mail. If I send the mail with one attachment it works fine, but if the attachment are more than one I get an error of Invalid Attachment (when I call the new MailAttachment(filename), not when i add it to the message). here is my code

        //attachments is a List<> of FileInfo()
        if (attachments != null)
        {
            List<MailAttachment> atts = new List<MailAttachment>();
            foreach (FileInfo attachment in attachments)
            {
                try
                {
                    atts.Add(new MailAttachment(attachment.FullName));
                }
                catch (Exception ex)
                {
                    //error is in this try catch block, if attachments.Count > 1
                    string exc = ex.ToString();
                    MessageBox.Show(ex.ToString());
                }
            }
            foreach(MailAttachment att in atts)
                pec.Attachments.Add(att);

        }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
The Agony
  • 138
  • 1
  • 8
  • 1
    What is the value for `attachment.FullName` on the first and second iteration? – Equalsk Feb 02 '17 at 15:34
  • How about using [`System.Net.Mail`](https://msdn.microsoft.com/en-us/library/system.net.mail(v=vs.110).aspx) instead of the old, [deprecated `System.Web.Mail`](https://msdn.microsoft.com/en-us/library/system.web.mail(v=vs.110).aspx)? – Uwe Keim Feb 02 '17 at 15:41
  • @Equalsk the complete path with the name of each file, I checked it, it loops right – The Agony Feb 02 '17 at 15:54
  • @UweKeim because It does not support smtps on port 465 – The Agony Feb 02 '17 at 15:55
  • @Equalsk the fullname is something like: \\SERVER\Share\Folder\docs\filename.ext where filename is a `DateTime.ToString("yyyyMMddHHmmssffff")` and ext could be any extension, for each file – The Agony Feb 02 '17 at 16:11
  • I would guess that it has issues with that being a UNC path, probably because the permissions in one way or another are incorrect, maybe also it's picked up a file you didn't expect such as `Thumbs.db`. Purely as a test can you copy 3 of the files to your local machine and test the code with those? I would bet it works when the files are local. Have you got the exact true path of the one that causes the error? – Equalsk Feb 02 '17 at 16:17
  • The filenames are stored in a db and the files are created with my app, so a not expected file is improbable... I try with a local path and get back... – The Agony Feb 02 '17 at 16:23
  • @Equalsk same error with a local path... – The Agony Feb 02 '17 at 16:27
  • Don't know what to tell you. I copied/pasted your code verbatim and it works perfectly fine, only difference is my `attachments` object was a directory with several local file attachments in. – Equalsk Feb 02 '17 at 16:35
  • tryed to copy the files in a local dir before attaching them (programmatically) and then delete them, now it works... – The Agony Feb 02 '17 at 17:19

0 Answers0