1
votes

I have a program that is using Outlook to send messages with attachments. It is working ok, sending emails with attachments but in outbox there is no attachment in the message. When somebody receive the message the attachment is visible but in outbox not. Here is some code:

        Outlook.MailItem mail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
        mail.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
        int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;  
        mail.Attachments.Add(Application.StartupPath+"/"+attachment, iAttachType, null, attachment);
        mail.To = email;
        mail.Subject = "Something";
        mail.Body = "Some body";
        mail.Send();

Before this I use:

    private Outlook.Application outlookApp;
    private Outlook._NameSpace outlookNameSpace;
    private Outlook.MAPIFolder outbox;

and

            outlookApp = new Outlook.Application();
            outlookNameSpace = outlookApp.GetNamespace("MAPI");
            outlookNameSpace.Logon(null, null, false, false);
            outbox = outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox);

My outlook program is connected with Microsoft Exchange Serwer. When I was using an application written in C++ it saved attachment in messages in outbox.

Thx for help!

1

1 Answers

0
votes

You could be working with an old version of the outlook item.

This can happen if you keep references to your mail items, rec-patterns, inspectors and some other types [that I now forgot] longer than you need them.

Your reference will often point to the old version of the item and keeping it can also prevent you from getting a reference to the updated one (the one with the attachment), even when events (Folder.BeforeItemMove) are triggered.

Also, have you tried if mail.Save() would do anything for you?

This is what I use as soon as I am done with an item.

public static void NullAndRelease(object o)
{
    if (o == null) {
        return;
    }

    try {
        int releaseResult = 0;
        do {
            releaseResult = System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
        } while (releaseResult >= 0);
    } catch {
    } finally {
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

The catch has no message and is not important in my case. It is there if someone would pass in a reference that leads to something other than a com object. You can also try FinalReleaseComObject(o).