从asp.net中的代码打开Outlook

时间:2017-04-26 21:06:28

标签: c# asp.net outlook

我正试图从代码背后打开Outlook但是当我从本地机器上运行它时,一切正常,但是当我部署到服务器时我收到错误。我收到了这个错误:

"404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable."

我似乎无法弄清楚到目前为止的问题。我需要使用onrow命令事件,这样我就可以根据用户被选中的命令按钮发送不同的电子邮件。我只需要打开Outlook,我就会以编程方式分配主题和正文内容,甚至是mailto。 这是我的代码:

<asp:GridView ID="gvDetail" runat="server" CssClass="table table-hover table-bordered table-responsive" ForeColor="#333333" GridLines="None"
                CellPadding="4" PageSize="200" OnRowCommand="GridViews_RowCommand" OnPageIndexChanging="OnPaging_gvDetail"
                AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" EnableViewState="True">
                <AlternatingRowStyle BackColor="#F3F3F3" />
                <Columns>


                  <asp:BoundField DataField="contract_name" HeaderText="Category" />
                  <asp:BoundField DataField="vendor_name" HeaderText="Vendor Name" />


                  <asp:ButtonField HeaderText="Reconciliation of Spend Metrics" ButtonType="Image" ImageUrl="~/img/mail.png"  ControlStyle-Width="30px" ControlStyle-Height="30px" CommandName="Select" />
                  <asp:ButtonField HeaderText="Request Call/Meeting with Supplier" ButtonType="Image" ImageUrl="~/img/mail.png" ControlStyle-Width="30px" ControlStyle-Height="30px" CommandName="Select1" />

                </Columns>
                <HeaderStyle CssClass="GridviewScrollHeader" BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                <RowStyle CssClass="GridviewScrollItem" ForeColor="#000066" />
              </asp:GridView>

背后的代码

 protected void GridViews_RowCommand(object sender, GridViewCommandEventArgs e)
    {

      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ToString());


      Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
      Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);


        if (e.CommandName == "Select")
        {

          int rowIndex = Convert.ToInt32(e.CommandArgument);
          string contractname = gvDetail.Rows[rowIndex].Cells[0].Text;
          string vendorname = gvDetail.Rows[rowIndex].Cells[1].Text;

           oMsg.Subject = "test";
          oMsg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
          oMsg.HTMLBody = "<p> "test..." + Environment.NewLine + "</p>"; //Here comes your body;

          oMsg.Display(false); //In order to display it in modal inspector change the argument to true
        }
    }

1 个答案:

答案 0 :(得分:2)

您的代码将在没有交互式用户访问Outlook的服务器上运行。更糟糕的是,Outlook无法从服务(例如IIS)中使用。

创建一个EML文件并提供指向您网站用户的链接 - Outlook(在客户端)将很乐意打开并显示它。