How to open Outlook new mail window c#

雨燕双飞 提交于 2019-11-27 06:53:46
Maciej

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 );

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());
        }
    }
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!