TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
joe bloe
NA
10
0
System.Net.Mail Large HTML message
Aug 10 2009 2:49 AM
I have a small survey application that I put together within an ASP.NET CMS (sitefinity). The logic of the survey is to store the SelectedItems from the various button list controls in hidden fields (upon button clicks). As each question is answered a new panel of questions is made visible and the answered questions are hidden.
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>
<asp:HiddenField ID="q1" runat="server" />
Here is the code behind
.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;
}
}
The ASPX page is just an HTML formatted message that looks like, "Dear [$FirstName$], etc" with a code behind file for Tokenizing. The code for this was written by Damon Armstrong and can be found <a href="http://www.simple-talk.com/dotnet/asp.net/token-replacement-in-asp.net/">Here</a>. I used the code exactly as written. Again, the problem is I can't get my hidden fields to link to the external ASPX page nor can I tokenize them.
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 <div id="a" runat="server"> because System.Net.Mail returns an error that the contents of the <div> 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
Reply
Answers (
4
)
save details to backend
develop window based application