Courtney Stephenson
Courtney Stephenson

Reputation: 930

ASP.NET Input string was not in a correct format

I have made a control that creates an electronic invite to an event. The user inputs their information into textboxes and the system gathers that information, adds it to the HTML for the email and then sends it.

I am encountering an error when I combine all the parts. My code is below.

protected void Button1_Click(object sender, EventArgs e)
 {
    SmtpClient smtpClient = new SmtpClient();
    MailMessage message = new MailMessage();

    MailAddress fromAddress = new MailAddress(YourEmailBox.Text, YourNameBox.Text);

    message.From = fromAddress;

    message.To.Add(FriendEmailBox.Text);
    message.Subject = EmailSubject;
    message.IsBodyHtml = true;
    message.Body = "<html>" + string.Format(EmailBody, YourNameBox.Text, PersonalNoteBox.Text) + "</html>";
    smtpClient.Send(message);

    YourNameBox.Text = YourEmailBox.Text = FriendNameBox.Text = FriendEmailBox.Text = PersonalNoteBox.Text = string.Empty;
    Label1.Text = "Email Successfully sent!";

}

It is giving me the input string was not in a correct format error on this line:

message.Body = "<html>" + string.Format(EmailBody, YourNameBox.Text, PersonalNoteBox.Text) + "</body></html>";

Will you please help me out?

Upvotes: 0

Views: 4596

Answers (5)

Anas Ghanem
Anas Ghanem

Reputation: 127

In my case, String.Replace is the only thing that worked for me when it comes to replacing values insiode html string.

"

{0}".Replace("{0}",YourValue)

Upvotes: 1

shuniar
shuniar

Reputation: 2622

As XaiSoft pointed out you're using string.Format() incorrectly, replace:

message.Body =  "<html>" + string.Format(EmailBody, YourNameBox.Text, PersonalNoteBox.Text) + "</html>";

With:

message.Body = string.Format(@"<html>{0}<br /><br />{1}<br /><br />{2}</html>",
                             EmailBody, YourNameBox.Text, PersonalNoteBox.Text)

If YourNameBox.Text and PersonalNoteBox.Text are optional, you'll just end up with 4 extra line breaks at the end of your email. If that is somehow a problem, you can always do this too:

var name = string.IsNullOrEmpty(YourNameBox.Text) 
              ? string.Empty 
              : string.Format(@"<br /><br />{0}", YourNameBox.Text);

var personalNote = string.IsNullOrEmpty(YourNameBox.Text) 
                      ? string.Empty 
                      : string.Format(@"<br /><br />{0}", PersonalNoteBox.Text);

message.Body = string.Format(@"<html>{0}{1}{2}</html>", EmailBody, name, personalNote);

EDIT

Also, note the @ symbol used in the format strings. This will treat your string as a literal, ignoring any escape characters that could also be causing your issue.

Upvotes: 1

Xaisoft
Xaisoft

Reputation: 46641

Why not just do:

message.Body = string.Format("<html>{0}</html>",EmailBody.Text)

I don't get why you have 3 arguments passed to string.Format, for example, "<html>" + string.Format("3","4","5") + "</html>" will produce

<html>3</html>

Upvotes: 4

ipr101
ipr101

Reputation: 24236

I would guess that the EmailBody variable does not contain the correct number of format items for string.Format to work.

Upvotes: 0

squillman
squillman

Reputation: 13641

Should it maybe be this instead?

message.Body = "<html>" + string.Format(EmailBody.Text, YourNameBox.Text, PersonalNoteBox.Text) + "</html>";

(adding the .Text to EmailBody, assuming EmailBody is some kind of text based control)

Upvotes: 1

Related Questions