使用c#.net中的Microsoft.Office.Interop.Outlook使用gridview发送电子邮件

时间:2016-05-22 03:56:34

标签: c# asp.net email gridview outlook

我想使用GridView通过电子邮件从我的Exchange(Outlook)帐户发送asp:button

我的代码工作正常,但电子邮件中的GridView格式松散(颜色,列重等)。

这是代码:

using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;

protected void Send_Click(object sender, EventArgs e)
        {
            try
            {
                // Create a Outlook Application and connect to outlook 
                Outlook.Application OutlookApplication = new Outlook.Application();

                // create the MailItem which we want to send 
                Outlook.MailItem message = (Outlook.MailItem)OutlookApplication.CreateItem(Outlook.OlItemType.olMailItem);

                MailAddress toAddress = new MailAddress("myemail@gmail.com");
                MailAddress ccAddress = new MailAddress("myemail@mycompany.com");

                message.To = toAddress.ToString();
                message.CC = ccAddress.ToString();
                message.Subject = "Mail Subject";
                message.HTMLBody = GridViewToHtml(HourlyReportGV1);
                message.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

                //Send email
                message.Send();
            }
            catch (System.Exception ex)
            {
                Aviso.Text = ex.ToString();
            }
        }

        private string GridViewToHtml(GridView gv)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gv.RenderControl(hw);
            return sb.ToString();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }

它的外观如下:

enter image description here

您知道如何使“电子邮件视图”与“网络视图”完全相同吗?

<asp:GridView style="margin:0 auto; margin-bottom:50px;" HeaderStyle-CssClass="tituloshandoff" RowStyle-CssClass="contenidohandoffbatch" ID="HourlyReportGV1" runat="server" AutoGenerateColumns="False" DataSourceID="Vacio">
            <Columns>
                <asp:BoundField DataField="IDBatch" HeaderText="ID" SortExpression="IDBatch" HeaderStyle-CssClass="TituloInvisible" ItemStyle-CssClass="TituloInvisible"/>
                <asp:BoundField DataField="DealerCodigo" HeaderText="CODE" SortExpression="DealerCodigo"/>
                <asp:BoundField DataField="DealerDescripcion" HeaderText="NAME" SortExpression="DealerDescripcion"/>
                <asp:TemplateField HeaderText="00:00"><ItemTemplate><asp:TextBox runat="server" ID="Cero" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="01:00"><ItemTemplate><asp:TextBox runat="server" ID="Una" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="02:00"><ItemTemplate><asp:TextBox runat="server" ID="Dos" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="03:00"><ItemTemplate><asp:TextBox runat="server" ID="Tres" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="04:00"><ItemTemplate><asp:TextBox runat="server" ID="Cuatro" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="05:00"><ItemTemplate><asp:TextBox runat="server" ID="Cinco" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="06:00"><ItemTemplate><asp:TextBox runat="server" ID="Seis" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="07:00"><ItemTemplate><asp:TextBox runat="server" ID="Siete" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="08:00"><ItemTemplate><asp:TextBox runat="server" ID="Ocho" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="09:00"><ItemTemplate><asp:TextBox runat="server" ID="Nueve" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="10:00"><ItemTemplate><asp:TextBox runat="server" ID="Diez" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
                <asp:TemplateField HeaderText="11:00"><ItemTemplate><asp:TextBox runat="server" ID="Once" Width="70px"></asp:TextBox></ItemTemplate></asp:TemplateField>
            </Columns>
        </asp:GridView>

1 个答案:

答案 0 :(得分:1)

您的GridView(HeaderStyle-CssClass="tituloshandoff"RowStyle-CssClass="contenidohandoffbatch")使用的CSS样式类不包含在gv.RenderControl(hw)生成的HTML代码中。因此,它们的样式属性无法应用于电子邮件中的GridView表。

您可以在GridView标记中设置单独的样式属性(例如HeaderStyle-BackColor="Silver"RowStyle-Height="24")。然后,这些样式属性将嵌入到GridView HTML输出中,电子邮件应用程序将有机会兑现它们。