将存储在转发器中的信息发送给电子邮件收件人?

时间:2014-02-27 18:33:47

标签: c# email repeater system.net.mail

我想将存储在转发器中的所有信息都放在电子邮件正文中以便发送。基本上,它是一个产品订单表(也使用转发器),它将用户发送到订单审查页面,底部有一个表单,允许他们将订单信息发送给其他人。我被允许将订单信息设置为表格。我有一切正常,我的代码成功发送电子邮件。但是,我不知道如何将订单审查转发器的内容拉入邮件正文中。

任何建议/提示表示赞赏!谢谢!

设计师代码:

<asp:Repeater ID="orderRepeater" runat="server" >
    <itemtemplate>
        <%# Eval("LocationName") %>
        <div class="headerRow">
            <div class="header">
                <div class="thumb"> 
                    <p></p>
                </div>

                <div class="headerField name"><p class="hField">Product</p></div>
                <div class="headerField sku"><p class="hField">GOJO SKU</p></div>
                <div class="headerField size"><p class="hField">Size</p></div>
                <div class="headerField case"><p class="hField">Case Pack</p></div>
                <div class="headerField qty"><p class="hField">Quantity</p></div>
            </div>
        </div>
        <div class="table">
            <div class="row">
                <div class="thumb"><%# Eval("Thumbnail") %></div>
                <div class="field name"><p class="pField"> <%#Eval("ProductName") %> </p></div>
                <div class="field sku"><p class="pField"> <%#Eval("Sku") %></p></div>
                <div class="field size"><p class="pField"> <%#Eval("Size") %></p></div>
                <div class="field case"><p class="pField"><%#Eval("CasePack") %></p></div>
                <div class="field qty"><p class="pField"><%#Eval("Qty") %></p></div>
            </div>
        </div>
    </itemtemplate>
</asp:Repeater>



 <asp:placeholder id="form" runat="server">
        <asp:label id="msgLbl" runat="server" text=""></asp:label>
        <asp:hyperlink id="link" runat="server" visible="false" NavigateUrl="/united-states/market/office-buildings/obproductmap/ProductList" >Product Listing</asp:hyperlink><br />
        <asp:hiddenfield id="emailTbl" runat="server"></asp:hiddenfield>

        <div id="emailForm">
            <asp:ValidationSummary ID="vsEmailForm" runat="server" ForeColor="red" DisplayMode="BulletList" /><br />
            <div id="yourFields">
                <div id="yNameInfo" class="fieldBlock">
                    <asp:requiredfieldvalidator runat="server" id="rfvYName" ForeColor="red" 
                        ControlToValidate="yName" errormessage="You must enter your name" Text="*" 
                        Display="Dynamic" SetFocusOnError="True"></asp:requiredfieldvalidator>
                    Your Name:<br /> <asp:TextBox runat="server" id="yName" size="40"></asp:TextBox>
                </div>

                <div id="yEmailInfo" class="fieldBlock">
                    <asp:requiredfieldvalidator runat="server" id="rfvYEmail" ForeColor="red" 
                        ControlToValidate="yEmail" Display="Dynamic" 
                        errormessage="You must enter your email" Text="*" SetFocusOnError="True"></asp:requiredfieldvalidator>
                    <asp:regularexpressionvalidator runat="server" id="revYEmail" ForeColor="red" 
                        ControlToValidate="yEmail" 
                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                        errormessage="Your email address is not valid. Please try again." Text="*" 
                        Display="Dynamic" SetFocusOnError="True"></asp:regularexpressionvalidator>
                    Your Email: <br /> <asp:TextBox runat="server" id="yEmail" size="40"></asp:TextBox><br /><br />
                </div>
            </div>
            <div id="reFields">
                <div id="rNameInfo" class="fieldBlock">
                    <asp:requiredfieldvalidator runat="server" id="rfvRName" ForeColor="red" 
                        ControlToValidate="rName" Text="*" 
                        errormessage="You must enter a recipient's name" Display="Dynamic" 
                        SetFocusOnError="True"></asp:requiredfieldvalidator>
                    Recipient's Name: <br /><asp:TextBox runat="server" id="rName" size="40"></asp:TextBox>
                </div>

                <div id="rEmailInfo" class="fieldBlock">
                    <asp:requiredfieldvalidator runat="server" id="rfvREmail" ForeColor="red" 
                    ControlToValidate="rEmail" Display="Dynamic" Text="*" 
                    errormessage="You must enter a recipient's email" SetFocusOnError="True"></asp:requiredfieldvalidator>
                    <asp:regularexpressionvalidator runat="server" id="revREmail" ForeColor="red" 
                    ControlToValidate="rEmail" 
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                    errormessage="The recipient's email address is not valid. Please try again." 
                    Text="*" Display="Dynamic" SetFocusOnError="True"></asp:regularexpressionvalidator>
                    Recipient's Email:<br /> <asp:TextBox runat="server" id="rEmail" size="40"></asp:TextBox><br /><br />
                </div>
            </div>
            <div id="buttons">
                <asp:Button id="submit" Text="Submit" runat="server" CausesValidation="true" 
                    onclick="submitBtn" />&nbsp; &nbsp; &nbsp;
                <asp:Button id="cancel" Text="Cancel" runat="server" CausesValidation="false" /><br /><br />
                <asp:Button id="print" Text="Print" runat="server" CausesValidation="false "/>&nbsp; &nbsp; &nbsp;
                <asp:Button id="pdf" Text="Save as PDF" runat="server" CausesValidation="false" />
            </div>
        </div>
    </asp:placeholder>
    <asp:placeholder id="sentMsg" visible="false" runat="server">
        <asp:Label id="lbl" runat="server" Text="Your message has been sent!"></asp:Label>
    </asp:placeholder>

代码背后:

    protected void getOrder(Item CurrentItem)
    {
        Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoMarket");
        if (Session["orderComplete"] != null && Session["orderComplete"] != "")
        {

            if (HomeItem != null)
            {
                Item ProductGroup = HomeItem.Axes.SelectSingleItem(@"child::*[@@templatename='gojoMarketOfficeBuildigProductMap']/*[@@templatename='gojoOrderReview']");

                Database db = Sitecore.Context.Database;
                DataSet dset = new DataSet();


                if (ProductGroup != null)
                {
                    string InFromSession = Session["orderComplete"].ToString();

                    try
                    {
                        DataTable summary = dset.Tables.Add("summary");

                        summary.Columns.Add("LocationName", Type.GetType("System.String"));
                        summary.Columns.Add("Thumbnail", Type.GetType("System.String"));
                        summary.Columns.Add("ProductName", Type.GetType("System.String"));
                        summary.Columns.Add("Sku", Type.GetType("System.String"));
                        summary.Columns.Add("Size", Type.GetType("System.String"));
                        summary.Columns.Add("CasePack", Type.GetType("System.String"));
                        summary.Columns.Add("Qty", Type.GetType("System.String"));
                        summary.Columns.Add("Location", Type.GetType("System.String"));

                        Label qL = (Label)FindControl("qty");
                        string[] orders = InFromSession.Split(';');

                        string tempheader = "";

                        foreach (string order in orders)
                        {
                            DataRow drow = summary.NewRow();
                            if (order != "")
                            {
                                string[] things = order.Split(',');

                                string skus = things.GetValue(0).ToString();

                                if (skus != "")
                                {
                                    Item locName = db.Items[skus];

                                    Item LocationItem = ScHelper.FindAncestor(locName, "gojoProductLocation");

                                    if (LocationItem != null)
                                    {                                     
                                        string LocationName = LocationItem.Fields["Header"].ToString();

                                        if (tempheader != LocationName)
                                        {
                                            drow["LocationName"] = "<div><h1>" + LocationName + "</h1></div>";
                                            tempheader = LocationName;
                                        }
                                    }
                                }
                            }

                            string purchase = order;
                            int total = orders.GetUpperBound(0);
                            if (purchase != "")
                            {
                                string[] infos = purchase.Split(',');

                                string ids = infos.GetValue(0).ToString();

                                string qtys = infos.GetValue(1).ToString();

                                if (ids != "")
                                {
                                    Item anItem = db.Items[ids];

                                    Item orderItem = db.Items[anItem.Fields["Reference SKU"].Value];

                                    if (orderItem != null)
                                    {
                                        Item marketItem = db.Items[orderItem.Fields["Master Product"].Value];
                                        if (marketItem != null)
                                        {
                                            Item CPNItem = db.Items[marketItem.Fields["Complete Product Name"].Value];

                                            drow["Thumbnail"] = "";

                                            Sitecore.Data.Fields.XmlField fileField = marketItem.Fields["Thumbnail"];
                                            drow["Thumbnail"] = "<image src=\"" + ScHelper.GetCorrectFilePath(fileField) + "\" border=\"0\" alt=\"\">";

                                            if (CPNItem != null)
                                            {
                                                var name = CPNItem["Complete Name"];
                                                drow["ProductName"] = name;
                                            }

                                            drow["Sku"] = marketItem.Fields["SKU"].Value;
                                            drow["CasePack"] = marketItem.Fields["Case Pack"].Value;
                                            if (marketItem.Fields["Size"] != null)
                                            {
                                                drow["Size"] = marketItem.Fields["Size"].Value;
                                            }
                                            else
                                            {
                                                drow["Size"] = "N/A";

                                            }

                                            drow["Qty"] = qtys.ToString();
                                            summary.Rows.Add(drow);
                                        }
                                    }

                                }

                            }
                        }
                        orderRepeater.DataSource = dset;
                        orderRepeater.DataMember = "summary";
                        orderRepeater.DataBind();
                    }
                    catch (Exception x)
                    {
                        //

                    }
                }

            }
        }
        else
        {
            HyperLink none = (HyperLink)FindControl("link");
            Label msg = (Label)FindControl("msgLbl");
            none.Visible = true;
            msg.Text = "You have not selected any items for purchase. To purchase items, please visit our complete product listing: ";
        }
    }



    protected void sendEmail()
    {
        Item CurrentItem = Sitecore.Context.Item;
        Item HomeItem = ScHelper.FindAncestor(CurrentItem, "gojoOrderReview");
        if (HomeItem != null)
        {
            TextBox rTxt = (TextBox)FindControl("rEmail");
            TextBox fName = (TextBox)FindControl("yName");

                try
                {
                    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
                    msg.To.Add(rTxt.Text);
                    msg.Subject = "GOJO product order summary";
                    msg.From = new System.Net.Mail.MailAddress("donotreply@GOJO.com");
                    msg.Body = "Here is a list of the products your friend, " + fName.Text + ", has selected: ";
                    msg.IsBodyHtml = true;

                    System.Net.Mail.SmtpClient sc = new System.Net.Mail.SmtpClient("mail.innismaggiore.com");
                    sc.Send(msg);
                }
                catch (Exception EX)
                {
                    GOJOHelper.WriteLog("GOJO Order Review - sendEmail()", EX.ToString());
                }

        }
    }

    protected void submitBtn(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Database db = Factory.GetDatabase("master");
            PlaceHolder form = (PlaceHolder)FindControl("form");
            PlaceHolder sent = (PlaceHolder)FindControl("sentMsg");

            using (new SecurityDisabler())
            {
                sendEmail();
                form.Visible = false;
                sent.Visible = true;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我认为这个答案有你正在寻找的东西。基本上,您可以使用javascript将html发送到服务器,以便将其包含在您的电子邮件中:

asp.net get html controls in code behind

另外,请确保在页面上添加ValidateRequest="false"以便发回html

答案 1 :(得分:0)

如果其他人有同样的问题并且转发器对象没有图像,这个解决方案简单易用,而不是信任客户端的javascript反馈!!

System.IO.StringWriter stringWrite = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter htmlWrite =new HtmlTextWriter(stringWrite);

    Repeater1.RenderControl(htmlWrite);string emailContent = stringWrite.ToString();