Omar Mir Omar Mir - 3 months ago 34
Vb.net Question

Create outlook message with attachment using vb.net

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.

Thanks!

Answer

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

http://support.microsoft.com/kb/313803

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 = "user@example.com";

    // 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
    oMsg.Send();

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