Omar Mir Omar Mir - 4 months ago 52 Question

Create outlook message with attachment using

I would like to create an outlook message with a subject and some attachments for the user to send when they are ready. I have the file paths for the attachments. How would I go about doing this?

Basically the user needs to click a button on the application and that open an outlook message window with a predefined message and a few attachments. They should then be able to modify and add the required "To" etc before sending via outlook.



How to send attachments in an e-mail message by using Visual Basic .NET

Here is the C# version of the code:

void Main()
    // Create an Outlook application.
    Outlook._Application oApp;
    oApp = new Outlook.Application();

    // Create a new MailItem.
    Outlook._MailItem oMsg;
    oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem);
    oMsg.Subject = "Send Attachment Using OOM in Visual Basic .NET";
    oMsg.Body = "Hello World" + vbCr + vbCr;

    // TODO: Replace with a valid e-mail address.
    oMsg.To = "";

    // Add an attachment
    // TODO: Replace with a valid attachment path.
    string sSource = "C:\\Temp\\Hello.txt";
    // TODO: Replace with attachment name
    string sDisplayName = "Hello.txt";

    string sBodyLen = oMsg.Body.Length;
    Outlook.Attachments oAttachs = oMsg.Attachments;
    Outlook.Attachment oAttach;
    oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName);

    // Send

    // Clean up
    oApp = null;
    oMsg = null;
    oAttach = null;
    oAttachs = null;