Upon completion the final event is to email the SelectedItems along with a large HTML message to the user. The SelectedValues are placed in various locations of the HTML message. A mail merge if you will.
Here is my problem: I can only get the SelectedItems into the HTML message under certain limited conditions.
1. Using a Token Replacement scheme allows me to "Tokenize" the fields within a static ASPX page. However, the tokens can't be linked to the server controls (the hidden fields) inside of the user control .ascx file.
Here is an example of my code for this situation.
.ascx
| <%@ Control Language="C#" AutoEventWireup="true" CodeFile="NeedsSurvey.ascx.cs" Inherits="NeedsSurvey_NeedsSurvey" ClassName="NeedSurvey" %> <asp:Panel runat="server" ID="panel1"> 1. Which of the following best describes the personal needs you feel are the most immediate? <br /> <br /> <asp:RadioButtonList ID="RadioButtonList1" runat="server"> <asp:ListItem>Help with everday expenses</asp:ListItem> <asp:ListItem>Paying off debt</asp:ListItem> <asp:ListItem>Taking a vacation</asp:ListItem> <asp:ListItem>Home improvments</asp:ListItem> <asp:ListItem>Better medical coverage</asp:ListItem> </asp:RadioButtonList> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="btn_Click1" /> <asp:Label ID="Label1" runat="server"></asp:Label> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RadioButtonList1" ErrorMessage="Please select an answer"></asp:RequiredFieldValidator> </asp:Panel> |
.ascs.cs
| using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net.Mail; using System.Net; using System.IO; public partial class NeedsSurvey_NeedsSurvey : System.Web.UI.UserControl { #region EventHandlers protected void btn_Click1(object sender, EventArgs e) { try { panel1.Visible = false; panel2.Visible = true; } catch (Exception ex) { Label1.Text = ex.Message; } q1.Value = RadioButtonList1.SelectedItem.Text; } #endregion public void Confirm_Send(object sender, EventArgs e) { //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress("xxx.com"); mail.To.Add("xxx.com"); //set the content mail.Subject = "Website Lead"; string html = ScreenScrapeHtml("http://localhost/demo/mycontrols/needssurvey.aspx"); mail.Body = html; mail.IsBodyHtml = true; //send the message SmtpClient smtp = new SmtpClient("xxx.com"); smtp.Credentials = new System.Net.NetworkCredential("xxx", "xxx"); smtp.Send(mail); } public string ScreenScrapeHtml(string url) { WebRequest objRequest = System.Net.HttpWebRequest.Create(url); StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream()); string result = sr.ReadToEnd(); sr.Close(); return result; } } |
2. The other method is to place the html message in the User Control within a Literal control and call the control at the SEND email event. Unfortunately you can not place server controls within a Literal control to call the values of the hidden fields, nor is it possible to use a
because System.Net.Mail returns an error that the contents of the
are not literal.
It is possible to Tokenize the fields (because the token code is used within the User Control, the hidden fields are available, unlike the external ASPX method) within the Literal control but they will only render on PageLoad. Calling the Literal control and sending through System.Net.Mail does not render the tokens only "Dear [$FirstName$], NOT Dear John.
Sorry for the length of this, but I wanted to make sure I included everything I tried.
One final Note. I do know that I can use server control fields using System.Net.Mail, but my HTML message is much more long and complicated to make that a viable solution.
Thanks.
joe
It is possible to Tokenize the fields (because the token code is used within the User Control, the hidden fields are available, unlike the external ASPX method) within the Literal control but they will only render on PageLoad. Calling the Literal control and sending through System.Net.Mail does not render the tokens only "Dear [$FirstName$], NOT Dear John.
Sorry for the length of this, but I wanted to make sure I included everything I tried.
One final Note. I do know that I can use server control fields using System.Net.Mail, but my HTML message is much more long and complicated to make that a viable solution.
Thanks.
joe
4 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Master BillaPosted Aug 11, 2009, 7:52 AM
joe bloePosted Aug 11, 2009, 1:35 PM
For anyone who stumbles across this. If you are looking for an HTML/Text Mail Merge or Mail Templating solution using System.Net.Mail you can use the MailDefinition of System.Web.UI. Here is some sample code for taking the value of a RadioButtonList and inserting it's selected value into a field within an external file. Thanks. Joe.
ascx file
1. Choose One.
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Reflection;
using System.Collections.Specialized;
public partial class MyControls_Wizard : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Create_Email(object sender, System.EventArgs e)
{
q1.Value = ListBox1.SelectedItem.Text;
CreateMessage();
}
protected void CreateMessage()
{
MailMessage message = new MailMessage();
SmtpClient client = new SmtpClient();
MailDefinition msg = new MailDefinition();
\\Had some problems using a localhost or "/" address. Still working on this.
msg.BodyFileName = @"C:\Inetpub\wwwroot\demo\MyControls\Template.htm";
msg.IsBodyHtml = true;
msg.From = "xxx";
msg.Subject = "Test Email Token";
ListDictionary replacements = new ListDictionary();
replacements.Add("<%FirstName%>", q1.Value);
message = msg.CreateMailMessage(TextBox1.Text, replacements, new LiteralControl());
//Send the e-mail message
client.Host = "mailserver";
client.Send(message);
}
}
P.S. Dont forget to change your SMTP settings in your web.Config file
joe bloePosted Aug 11, 2009, 6:15 AM
j
Master BillaPosted Aug 10, 2009, 11:13 PM
I think you can do in easy way, just create template to your email message, it's means which you have created, then get the real value from the database and replace the template string and then then you can send.
look here
http://dotnettricks.com/blogs/roberhinojosablog/archive/2006/05/12/57.aspx
thank you