Attachments.Add throws Exception

F

Ferdinand Zaubzer

I have a Problem of creating a large number of emails with attachments.
Everything works fine, but when the 100th mail is created and the
attachment is added

mail.Attachments.Add(fileName, (int)OlAttachmentType.olByValue,
mail.Body.Length + 1, "My Attachment");

throws an Exception telling me the following: (Translated from German)
The file <filename> cannot be created. Check the permissions of the
folder where you want to create the file.

The code for creating all the mails is the following:

foreach (Recipient rec in Recipients){
_MailItem mail = outlookApp.CreateItem(OlItemType.olMailItem);

mail.To = rec.address;
mail.Body = rec.body;
mail.Subject = rec.subject;

mail.Attachments.Add(rec.pathToAttachment,
(int)OlAttachmentType.olByValue,
mail.Body.Length + 1, "My Attachment");

mail.Save();
mails.Add(mail); //save mails references in an Arraylist to be
able to send them later, after checking
mails in Draftfolder.
}

Can anyone help me with this problem
Thanks
 
S

Sue Mosher [MVP-Outlook]

Check the contents of the %temp% folder. As you add attachments, Outlook creates temporary files there, but only up to fileName (99). You may, therefore, need to delete those temporary files if you're creating more than 100 messages containing the same attachment.

Alternatively, create one message containing the attachment, then use MailItem.Copy to return a copy of that message and send the copy.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
F

Ferdinand Zaubzer

Sue said:
Check the contents of the %temp% folder. As you add attachments, Outlook creates temporary files there, but only up to fileName (99). You may, therefore, need to delete those temporary files if you're creating more than 100 messages containing the same attachment.

Alternatively, create one message containing the attachment, then use MailItem.Copy to return a copy of that message and send the copy.

Thanks, that clarifies the problem a little bit.

There Is no Way of copying the mails, since the mails can contain
several attachments. Some are personalized attachments (with different
Filenames of course) and some attachments are the same for each
recipient. So is there any other way of adding one attachment to each
email (always the same attachment) without copying the mails, and
without getting one temp file created for each email.

Thanks, Ferdinand
 
S

Sue Mosher [MVP-Outlook]

Not that I know of.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
F

Ferdinand Zaubzer

I found a way with copying an initial Mailobject and customizing it
afterwards. The common Attachment remains the same.
The problem now is, that if I save such a mail (one that is not newly
created but copied from an initial mail) it is saved in the outbox
instead of drafts. Anything I can do to solve that problem?

Cheers
Ferdinand
 
S

Sue Mosher [MVP-Outlook]

A message winds up in the Outbox either because it was sent or because it was moved there from some other folder. Did you send instead of save? Did you copy a message that was already sent? Is there any reason why you can't just move it to Drafts if that's where you ultimately want it to be?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
F

Ferdinand Zaubzer

I fixed the problem.
A copy of an unsaved message is saved in the outbox when calling msg.Save()
If the message is a copy of a saved message then it is saved to drafts
when calling msg.Save()

The next strange problem I found is, that when outlook is not started
before interacting with it, messages are by default saved in the
Inbox(!!!) when calling the msg.Save() method.
When it is already started before creating mails messages are saved in
the drafts folder.
Is there any way of starting outlook and waiting until it is started
before creating any emails?

By the time I am getting used to all the strange behaviour of Outlook.
temp filenames that are restricted to 100 files with the same original
filename, all that strange folder behaviour...
 
S

Sue Mosher [MVP-Outlook]

That depends on what you mean by "started." If you want to wait for the main Outlook window to appear, keep track of Explorers.Count.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top