简体   繁体   中英

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?

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>

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)

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.

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)

我猜想, EmailBody变量不包含的格式项正确数量string.Format工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM