36

I'm looking for a way to open a New mail in Outlook window.

I need programically fill: from, to, subject, body information, but leave this new mail window open so user can verify content / add something then send as normal Outlook msg.

Found that:

Process.Start(String.Format(
 "mailto:{0}?subject={1}&cc={2}&bcc={3}&body={4}", 
  address, subject, cc, bcc, body))

But there is no "From" option (my users have more than one mailbox...)

Any advice(s) ?

Ric .Net
  • 5,540
  • 1
  • 20
  • 39
Maciej
  • 10,423
  • 17
  • 64
  • 97
  • 1
    Is there a reason you aren't using vbscript for applications within outlook itself? Ultimately, if you wanted to, you could easily start the outlook application from c#, have a rule within outlook that runs on startup that populates this for you from a vbscript macro. As you are expecting to edit it in a gui, my question remains: do you need a c# specific solution, or are you only asking as you think you have to use c#? – tjborromeo May 01 '13 at 00:08

3 Answers3

57

I've finally resolved the issue. Here is piece of code resolving my problem (using Outlook interops)

Outlook.Application oApp    = new Outlook.Application ();
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem ( Outlook.OlItemType.olMailItem );
oMailItem.To    = address;
// body, bcc etc...
oMailItem.Display ( true );
Dowlers
  • 1,434
  • 16
  • 26
Maciej
  • 10,423
  • 17
  • 64
  • 97
  • 1
    When I run something similar when my Outlook is not open, then Outlook opens and I see the modal email dialog, however Outlook immediately closes as soon as user hits send and email is stuck in the Outbox. Has anyone had this problem? – user1198049 Sep 04 '13 at 22:17
  • Which assemblies/references are required for this? Thanks. – Taersious Jan 06 '15 at 21:58
  • Just Outlook interops – Maciej Feb 04 '16 at 22:26
  • 1
    @Taersious On my machine the interop file is here: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Outlook.dll which I added as reference by searching the Framework assemblies for Outlook in the reference manager in VS. X – TinyRacoon Jun 10 '21 at 09:49
  • Thanks, this helps. The other question which I have is, that the next statement of the application doesnt run until and unless I rather send the email or close the new email window. What if I want multiple new email window at once ? – Ram Mehta Jan 10 '23 at 11:28
10

Here is what i have tried. It's working as expected.

This application Adding Recipients, adding cc and adding subject and opening a new mail window.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using Outlook = Microsoft.Office.Interop.Outlook;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonSendMail_Click(object sender, EventArgs e)
    {
        try
        {
            List<string> lstAllRecipients = new List<string>();
            //Below is hardcoded - can be replaced with db data
            lstAllRecipients.Add("sanjeev.kumar@testmail.com");
            lstAllRecipients.Add("chandan.kumarpanda@testmail.com");

            Outlook.Application outlookApp = new Outlook.Application();
            Outlook._MailItem oMailItem = (Outlook._MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
            Outlook.Inspector oInspector = oMailItem.GetInspector;
           // Thread.Sleep(10000);

            // Recipient
            Outlook.Recipients oRecips = (Outlook.Recipients)oMailItem.Recipients;
            foreach (String recipient in lstAllRecipients)
            {
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient);
                oRecip.Resolve();
            }

            //Add CC
            Outlook.Recipient oCCRecip = oRecips.Add("THIYAGARAJAN.DURAIRAJAN@testmail.com");
            oCCRecip.Type = (int)Outlook.OlMailRecipientType.olCC;
            oCCRecip.Resolve();

            //Add Subject
            oMailItem.Subject = "Test Mail";

            // body, bcc etc...

            //Display the mailbox
            oMailItem.Display(true);
        }
        catch (Exception objEx)
        {
            Response.Write(objEx.ToString());
        }
    }
}
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
  • How do you cancel the send email if user cancels it using outlook cancel button ? – singhswat Apr 13 '17 at 11:57
  • The code just generates a new email window with the To: and Subject fields populated and displays it. It's up to the user to send the email (or not), so if the email window is closed (not sent), the email won't be sent. – Steve Goossens Sep 03 '17 at 17:36
0

You can't do this with mailto. Either your client will have to select the account they are sending from, which defaults to their default account or you will have to provide a mail form and set the headers when you send the e-mail.

Ben
  • 1,525
  • 11
  • 19
  • I know I cannot do that - that's why I'm looking different option - saw some apps managing that so must be some way... – Maciej May 27 '11 at 07:39