Solved

How can I send form results in an email using asp.net?

Posted on 2009-05-11
11
209 Views
Last Modified: 2013-11-08
I produce a results page for my users after they complete a .net wizard.  What is the best way to have the results page emailed to them?

0
Comment
Question by:devnewbee
  • 6
  • 3
  • 2
11 Comments
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24356947
Do you want to use a button or capture the Post Stream?

Here is button Code below
// Form

<Form id="form1" runat="server">

Email: <asp:TextBox id="txtEmail" runat="server" />

<asp:Button id="btnEmail" Text="Email Form" onclick="doEmail()" runat="server" />

</Form>
 

<%@ Import Namespace="System.Web.Mail" %> //Use Mail API 
 

// Do email 

private function void doEmail(Source as Object, E as EventArgs)

{

	MailMessage objEmail =  New MailMessage ()

	objEmail.To=txtEmail.text;

	objEmail.BCC="wc@ee.com";

	objEmail.FROM="me@here.com";
 

	objEmail.SUBJECT="I spammed you";

	objEmail.BODY="body of message";

	objEmail.BodyFormat = MailFormat.Text;

	SmtpMail.SmtpServer ="mail.Wherever.com";

	SmtpMail.Send(objEmail);

}

Open in new window

0
 
LVL 7

Expert Comment

by:svelluto
ID: 24356968
You could use System.Net.Mail

Here's a good website:

http://www.systemnetmail.com/

and I've attached a quick snippet of code I tend to use more often.

Hope that Helps!

Steve
MailMessage mail = new MailMessage();
 

//From E-mail Address

mail.From = new MailAddress("from@yourdomain.com");
 

//To E-mail Address

mail.To.Add(new MailAddress("to@userdomain.com"));
 

//Subject of E-mail

mail.Subject = "Subject Line";
 

//E-mail Message

mail.Body = "Message Body";
 

//Now the fun part...
 

try

{

//if you are using an Internal Mail Server

//you can use this:

SmtpClient smtp = new SmtpClient("internal.mailserver.com");

smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
 

//Or if you're using an external mail server

//You can use this:

SmtpClient smtp = new SmtpClient();

smtp.Host = "external.mailserver.com";

smtp.Port = 587;
 
 

//If you are not using default Credentials:

System.Net.NetworkCredential SmtpUserInfo = new System.Net.NetworkCredential("username", "password");
 

smtp.UseDefaultCredentials = false;

smtp.Credentials = SmtpUserInfo;
 

//SSL

smtp.EnableSsl = true;
 

//The most important line of all... Sending the e-mail!

smtp.Send(mail);

}

catch (Exception ex)

{
 

}

Open in new window

0
 

Author Comment

by:devnewbee
ID: 24357305
Awesome guys.  The only question i have is how do I save the .aspx page as text in the body tag of your code and use an address the user enters?  Do I call a textbox in the "TO" section?
0
 

Author Comment

by:devnewbee
ID: 24357319
I would like ...objEmail.BODY="body of message" to equal the rendered .aspx page to be save in text.
0
 

Author Comment

by:devnewbee
ID: 24357335
I figured out the TO problem.  No all I have left is to assign the rendered results to the body of the message.  How is this best done?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:svelluto
ID: 24357566
Where are the rendered results coming from?

Is this something you have coded?
0
 

Author Comment

by:devnewbee
ID: 24358015
They are just some static and dynamically generated tables that show on the page.  is there a way of just sending what is visible in an email to the user?  
0
 

Author Comment

by:devnewbee
ID: 24358082
if I save in a text format it works.  How can I do this prior to sending the email and call for txt file to send as an attachement or just send embedded?
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24358293
You will have to intercept the rendering stream and save it to a text file.

Attached a snippet you can play with
private void Page_Load(object sender, System.EventArgs e)

{

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

System.Web.UI.HtmlTextWriter oHtmlTextWriter = new

System.Web.UI.HtmlTextWriter(oStringWriter);

Page.RenderControl(oHtmlTextWriter);

oHtmlTextWriter.Flush();

System.IO.FileStream fs = new

System.IO.FileStream(@"C:\temp\test.htm",System.IO .FileMode.Create);

string s= oStringWriter.ToString();

byte[] b = System.Text.Encoding.UTF8.GetBytes(s);

fs.Write(b,0,b.Length);

fs.Close();

Response.End();

}

Open in new window

0
 

Author Comment

by:devnewbee
ID: 24358632
This is great.  The only concern I have is that is the final step of my wizard that the results happen on.  iIsee the above code is initiated on page_load.  is there another placa I should call the above code instead?
0
 
LVL 12

Accepted Solution

by:
williamcampbell earned 500 total points
ID: 24358827
Try doing it in the section outlined
 
            private void Page_Load(object sender, System.EventArgs e)

            {

                  if(IsPostBack)

                  {

                        OnPostBackLoad(e); <-- after the email button is pressed you will end up here

                  }

                  else

                  {

                        OnFirstLoad(e);

                  }

            }

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now