Expression >
Forums Home
>
Expression Studio Forums
>
Expression Studio
>
ToString() Causes problem in asp.net form
ToString() Causes problem in asp.net form
- I am working on a form with numerous fields that I want include as the body of the message in the email sent by the form. For some reason this has been giving me problems.
I have tried several different methods to "gather" the fields for the email, to no avail. I thought I had it solved, but when I include the msg1.Body line below, my email fails to send. If I remove it, the email sends just fine - however I do not get anything in the body of the message. (note: I only included 2 of the fields for brevity's sake)
msg1.From = Email.Text; msg1.To = tbTo.Value; msg1.Subject = tbSubject.Value; msg1.Body = sb.ToString(); msg1.Priority = MailPriority.Normal; SmtpMail.SmtpServer = "relay-hosting.secureserver.net";
The full code looks like this:
<script runat="server"> private void btnSend_Click(System.Object sender, System.EventArgs e) { //Creates the message StringBuilder sb = new StringBuilder(); MailMessage msg1 = new MailMessage(); sb.Append("Parent1 First Name: " + Parent1_Fname + "\n"); sb.Append("Parent1 Last Name: " + Parent1_Lname + "\n"); //Sets the from, to, subject, etc... msg1.From = Email.Text; msg1.To = tbTo.Value; msg1.Subject = tbSubject.Value; msg1.Body = sb.ToString(); msg1.Priority = MailPriority.Normal; SmtpMail.SmtpServer = "relay-hosting.secureserver.net"; msg1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver","relay-hosting.secureserver.net"); msg1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25); msg1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing",2); msg1.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1); try { SmtpMail.Send(msg1); Response.Redirect("mbrform_thx.aspx"); } catch { lblStatus.Text = "Error sending email"; } } </script>And I have a web.config with the email server information. One last note. I am using <%@ Import Namespace="System.Web.Mail" %> instead of System.Net.Mail because I found with the latter I had significantly more/worse problems.
Anyone have ideas here?
All Replies
I think you would probably get a quicker in a C# programming forum.
I haven't sent any email via code in a long time and then it was using VB.Net. However, being that you are receiving an error message where you say, I would guess that instead of just a line feed, which I think \n indicates, try a \r\n, which, I believe, is a carriage return and a line feed.- Thanks for answering William. You actually got me going on the right track. When I added \r I at least received some feedback in the email - although it was something like a System.Web.UI... string. But that got me to realize that my append lines did not include .Text on the fields - which was the problem.
I had:sb.Append("Parent1 First Name: " + Parent1_Fname + "\r\n"); sb.Append("Parent1 Last Name: " + Parent1_Lname + "\r\n");
Instead of:
Now I just need to figure out how to get my ListBox to return all items checked, rather than just the first item selected...sb.Append("Parent1 First Name: " + Parent1_Fname.Text + "\r\n"); sb.Append("Parent1 Last Name: " + Parent1_Lname.Text + "\r\n");<br/>
Thanks again for your help!sb.Append("Volunteer Choice(s): " + VolunteerChoice.Text + "\r\n");<br/>
- Proposed As Answer byJD_MSFTMSFTTuesday, November 10, 2009 11:06 PM

