简体   繁体   English

使用 Microsoft.Office.Interop.Outlook C# 在客户端打开 Outlook 2013 新约会窗口

[英]Open Outlook 2013 New Appointment window at Client side with Microsoft.Office.Interop.Outlook C#

I am trying to open Outlook 2013 New Appointment window at Client side with ASP.Net c# application.我正在尝试使用 ASP.Net c# 应用程序在客户端打开 Outlook 2013 新约会窗口。 my code is working fine its a simple button.我的代码工作正常,它是一个简单的按钮。 when i click the button from client machine this application open the outlook window on server/host machine but not on Client Machine.当我单击客户端机器上的按钮时,此应用程序会在服务器/主机上打开 Outlook 窗口,但不会在客户端机器上打开。

what can i use to open Outlook Appointment window on client machine by using ASP.Net C#.我可以用什么来使用 ASP.Net C# 在客户端计算机上打开 Outlook 约会窗口。

I am using following code:我正在使用以下代码:

    private void OpenOutlookAppt() {

     Microsoft.Office.Interop.Outlook.Application app = null;
     Microsoft.Office.Interop.Outlook.AppointmentItem appt = null;
     app = new Microsoft.Office.Interop.Outlook.Application();

     appt = (Microsoft.Office.Interop.Outlook.AppointmentItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);

        appt.Subject = "Customer Review";
        appt.MeetingStatus = Outlook.OlMeetingStatus.olMeeting;
        appt.Location = "36/2021";
        appt.Start = Convert.ToDateTime("3/30/2016 01:30:00 PM");
        appt.End = Convert.ToDateTime("3/30/2016 02:30:00 PM");
        Outlook.Recipient recipRequired =
            appt.Recipients.Add("abc@domain.com");
        recipRequired.Type =
            (int)Outlook.OlMeetingRecipientType.olRequired;
        Outlook.Recipient recipOptional =
            appt.Recipients.Add("abc@domain.com");
        recipOptional.Type =
            (int)Outlook.OlMeetingRecipientType.olOptional;
        Outlook.Recipient recipConf =
           appt.Recipients.Add("Conf Room 36/2021 (14) AV");
        recipConf.Type =
            (int)Outlook.OlMeetingRecipientType.olResource;
        appt.Recipients.ResolveAll();
        appt.Display(true); }

Thanks谢谢

Your code runs on the server under IIS, so of course you end up using Outlook on the server machine.您的代码在 IIS 下的服务器上运行,因此您当然最终在服务器计算机上使用 Outlook。 And Outlook cannot be used in a service (such as IIS).并且 Outlook 不能在服务(如 IIS)中使用。

Your options are:您的选择是:

  1. Client side Java Script.客户端 Java 脚本。 IE only.仅浏览器。 Your site must be trusted to be able to create COM objects ("Outlook.Applicatuion").必须信任您的站点才能创建 COM 对象(“Outlook.Applictuion”)。

  2. Create an iCal (ICS) file on the server.在服务器上创建 iCal (ICS) 文件。 When the end user opens it, saving it will create an appointment in Outlook.当最终用户打开它时,保存它会在 Outlook 中创建一个约会。

I think the best way to achieve this is by generating an ICS file on the server for the web user to download and open.我认为实现这一目标的最佳方法是在服务器上生成一个 ICS 文件,供网络用户下载和打开。

The ICS file format is quite simple: ICS 文件格式非常简单:

BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
STATUS:TENTATIVE
DTSTART:20160330T013000Z
DTEND:20160330T023000Z
SUMMARY;ENCODING=QUOTED-PRINTABLE:Customer Review
LOCATION;ENCODING=QUOTED-PRINTABLE:Conf Room 36/2021  (14) AV 
DESCRIPTION;ENCODING=QUOTED-PRINTABLE:Customer Review Meeting
END:VEVENT
END:VCALENDAR

Code for generating file content:生成文件内容的代码:

public class CalendarFile
{
    private CfStatus _status = CfStatus.Busy;
    private string _location = "";
    private string _description = "";

    public CalendarFile(string title, DateTime startDate, DateTime endDate)
    {
        if (startDate > endDate)
            throw new Exception("Attept to initialize a calendar event with start date after end date");

        Summary = title;
        StartDate = startDate;
        EndDate = endDate;
    }

    public enum CfStatus { Free, Busy, Tentative, OutOfTheOffice };

    override public string ToString()
    {
        var calEvent = new StringBuilder();
        calEvent.AppendLine("BEGIN:VCALENDAR");
        calEvent.AppendLine("VERSION:2.0");
        calEvent.AppendLine("BEGIN:VEVENT");

        switch (Status)
        {
            case CfStatus.Busy:
                calEvent.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY");
                break;
            case CfStatus.Free:
                calEvent.AppendLine("TRANSP:TRANSPARENT");
                break;
            case CfStatus.Tentative:
                calEvent.AppendLine("STATUS:TENTATIVE");
                break;
            case CfStatus.OutOfTheOffice:
                calEvent.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:OOF");
                break;
            default:
                throw new Exception("Invalid CFStatus");
        }

        if (AllDayEvent)
        {
            calEvent.AppendLine("DTSTART;VALUE=DATE:" + ToCFDateOnlyString(StartDate));
            calEvent.AppendLine("DTEND;;VALUE=DATE:" + ToCFDateOnlyString(EndDate));
        }
        else
        {
            calEvent.AppendLine("DTSTART:" + ToCFString(StartDate));
            calEvent.AppendLine("DTEND:" + ToCFString(EndDate));
        }

        calEvent.AppendLine("SUMMARY;ENCODING=QUOTED-PRINTABLE:" + ToOneLineString(Summary));

        if (Location != "") calEvent.AppendLine("LOCATION;ENCODING=QUOTED-PRINTABLE:" + ToOneLineString(Location));
        if (Description != "") calEvent.AppendLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + ToCFString(Description));

        calEvent.AppendLine("END:VEVENT");
        calEvent.AppendLine("END:VCALENDAR");

        return calEvent.ToString();
    }

    public string Summary { get; set; }

    public DateTime StartDate { get; private set; }

    public DateTime EndDate { get; private set; }

    public string Location
    {
        get { return _location; }
        set { _location = value; }
    }

    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }

    public bool AllDayEvent { get; set; }

    public CfStatus Status
    {
        get { return _status; }
        set { _status = value; }
    }

    private static string ToCFString(DateTime val)
    {
        //format: YYYYMMDDThhmmssZ where YYYY = year, MM = month, DD = date, T = start of time character, hh = hours, mm = minutes,
        //ss = seconds, Z = end of tag character. The entire tag uses Greenwich Mean Time (GMT)
        return val.ToUniversalTime().ToString("yyyyMMddThhmmssZ");
    }

    private static string ToCFDateOnlyString(DateTime val)
    {
        //format: YYYYMMDD where YYYY = year, MM = month, DD = date, T = start of time character, hh = hours, mm = minutes,
        //ss = seconds, Z = end of tag character. The entire tag uses Greenwich Mean Time (GMT)
        return val.ToUniversalTime().ToString("yyyyMMdd");
    }

    private static string ToCFString(string str)
    {
        return str.Replace("r", "=0D").Replace("n", "=0A");
    }

    private static string ToOneLineString(string str)
    {
        return str.Replace("r", " ").Replace("n", " ");
    }
}

ASP.NET WebForms page: ASP.NET WebForms 页面:

public partial class Appointment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var calFile = new CalendarFile("My Event", new DateTime(2016,3, 30, 5),
            new DateTime(2016,3, 30, 6));
        calFile.Location = "Conf Room 36/2021";
        calFile.Description = "Review meeting";
        calFile.Status = CalendarFile.CfStatus.Busy;
        calFile.allDayEvent = false;
        Response.AddHeader("content-disposition", "attachment; filename=PTO%20Request.ics");
        Response.ContentType = "text/x-vCalendar";
        Response.Write(calFile.ToString());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM