C# 从asp.net C发送电子邮件时需要新行#

C# 从asp.net C发送电子邮件时需要新行#,c#,asp.net,C#,Asp.net,嗨,我有下面几行代码从我的aspx页面发送电子邮件 protected void Button1_Click(object sender, EventArgs e) { try { //SmtpClient mail = new SmtpClient(); System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(); mm.From = new MailAdd

嗨,我有下面几行代码从我的aspx页面发送电子邮件

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        //SmtpClient mail = new SmtpClient();
        System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
        mm.From = new MailAddress(txt_email.Text);
        mm.To.Add("excelguesthouse@gmail.com");
        //MailMessage mm=new MailMessage();
        mm.Body = string.Format("{0},{1},{2},{3},{4}", txt_fname.Text, txt_lname.Text, txt_email.Text, txt_phone.Text, txt_details.Text);
        mm.IsBodyHtml = true;
        mm.Subject = cmb_type.Text;
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("excelguesthouse", "excelhome");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        object userstate = mm;
        client.Send(mm);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
我收到这封电子邮件很好


但我希望电子邮件格式正确,有换行符。那么,有谁能告诉我如何在上述代码中添加换行符,以在第二行和第三行显示电子邮件、电话和详细信息值。

在Rex的回答指出这是HTML后编辑。新行不能工作,因为它是HTML。您需要使用

标记

保存以备将来使用:

在纯文本电子邮件中,您可以使用@In
@“Some text”
,但这不适用于string.Format。相反,我这样做:

string.Format("This is one line{0}This is another {1}", Environment.NewLine, "SomeText");
在字符串中添加换行符。记住你只需要一个

string.Format("This is one line{0}This is another {1}{0}And another line{0}{0}And some more", Environment.NewLine, "SomeText");

在Rex的回答指出是HTML后编辑。新行不能工作,因为它是HTML。您需要使用

标记

保存以备将来使用:

在纯文本电子邮件中,您可以使用@In
@“Some text”
,但这不适用于string.Format。相反,我这样做:

string.Format("This is one line{0}This is another {1}", Environment.NewLine, "SomeText");
在字符串中添加换行符。记住你只需要一个

string.Format("This is one line{0}This is another {1}{0}And another line{0}{0}And some more", Environment.NewLine, "SomeText");
你已经准备好了

mm.IsBodyHtml = true;
只需在需要换行符的部分使用已设置的

mm.IsBodyHtml = true;

只需在需要换行符的部分使用

您指定消息的正文为“IsHtml”,因此请使用HTML

mm.Body = string.Format("{0} {1} <br /> {2} <br /> {3} <br /> {4}", txt_fname.Text, txt_lname.Text, txt_email.Text, txt_phone.Text, txt_details.Text);
mm.Body=string.Format(“{0}{1}
{2}
{3}
{4}”、txt_fname.Text、txt_lname.Text、txt_email.Text、txt_phone.Text、txt_details.Text);
您正在指定邮件的正文为“IsHtml”,因此请使用HTML

mm.Body = string.Format("{0} {1} <br /> {2} <br /> {3} <br /> {4}", txt_fname.Text, txt_lname.Text, txt_email.Text, txt_phone.Text, txt_details.Text);
mm.Body=string.Format(“{0}{1}
{2}
{3}
{4}”、txt_fname.Text、txt_lname.Text、txt_email.Text、txt_phone.Text、txt_details.Text);
如果我理解正确,您希望mm.Body中有断线。 改变这个

mm.Body = string.Format("{0},{1},{2},{3},{4}", txt_fname.Text, txt_lname.Text, txt_email.Text, txt_phone.Text, txt_details.Text);
为此:

mm.Body = string.Format("{0},{1}<br />{2}<br />{3}<br />{4}", txt_fname.Text, txt_lname.Text, txt_email.Text, txt_phone.Text, txt_details.Text);
mm.Body=string.Format(“{0},{1}
{2}
{3}
{4}”,txt_fname.Text,txt_lname.Text,txt_email.Text,txt_phone.Text,txt_details.Text);

这将使你在你的名字/姓氏和电子邮件、电子邮件和电话、电话和详细信息之间分线

如果我理解正确,你想在你的mm身上分线。 改变这个

mm.Body = string.Format("{0},{1},{2},{3},{4}", txt_fname.Text, txt_lname.Text, txt_email.Text, txt_phone.Text, txt_details.Text);
为此:

mm.Body = string.Format("{0},{1}<br />{2}<br />{3}<br />{4}", txt_fname.Text, txt_lname.Text, txt_email.Text, txt_phone.Text, txt_details.Text);
mm.Body=string.Format(“{0},{1}
{2}
{3}
{4}”,txt_fname.Text,txt_lname.Text,txt_email.Text,txt_phone.Text,txt_details.Text);

这会让你在你的名字/姓氏和电子邮件、电子邮件和电话、电话和详细信息之间划清界线

只要试着把
\n\n
放在你想休息的地方就行了。它对我有用

例如:

mail.Body = "\n\nHi,\n\n "  + ds.Tables[0].Rows[0][1] + " \n\nsent you a message: " + ds.Tables[0].Rows[0][4] + " \n\nYou can contact me via email:  " + ds.Tables[0].Rows[0][2] + " or\n\nphone:  " + ds.Tables[0].Rows[0][3] + " " ;

只要试着把
\n\n
放在你想休息的地方。它对我有用

例如:

mail.Body = "\n\nHi,\n\n "  + ds.Tables[0].Rows[0][1] + " \n\nsent you a message: " + ds.Tables[0].Rows[0][4] + " \n\nYou can contact me via email:  " + ds.Tables[0].Rows[0][2] + " or\n\nphone:  " + ds.Tables[0].Rows[0][3] + " " ;

如果你声称主体是HTML,你需要发送有效的HTML。如果你声称主体是HTML,你需要发送有效的HTML。好在,我没有看到它是HTML@user3280292很高兴帮助Good spot,我没有看到它是html@user3280292乐于帮助感谢Scott的快速回复并提供准确的代码:D感谢Scott的快速回复并提供准确的代码:D