简体   繁体   中英

Preserving line breaks from text-area when sending mail

I'm trying to make a website where you can write and costumize a mail then send it. I have a problem where the line breaks from a text-area doesn't preserve. When I send the mail with c#, there are no line breaks. (sorry for my bad english, not my first language)

Heres my code:

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web_Mail_Manager
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string YourUsername = "(Just gonna cover my Email)";
            string YourPassword = "(Just gonna cover my password)";

            string Destination = DestinationAdress.Value;
            string YourMessageSubject = MailSubject.Value;
            string YourMessageBody = MailBody.Value;

            try
            {
                using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587))
                {
                    client.EnableSsl = true;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = false;
                    client.Credentials = new NetworkCredential(YourUsername, YourPassword);
                    System.Net.Mail.MailMessage msgObj = new System.Net.Mail.MailMessage();
                    msgObj.To.Add(Destination);
                    msgObj.From = new MailAddress(YourUsername);
                    msgObj.Subject = YourMessageSubject;
                    msgObj.Body = YourMessageBody;
                    msgObj.IsBodyHtml = true;
                    client.Send(msgObj);
                }
            }
            catch (Exception)
            {

            }
        }
    }
}

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Web_Mail_Manager.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet" />
    <style>
        body {
            background-color: lightgray;
            font-family: 'Poppins', sans-serif;
        }

        .mailPreview {
            background-color: white;
            position: absolute;
            top: 10%;
            left: 50%;
            transform: translate(-50%);
            width: 45vw;
            height: fit-content;
            padding: 15px;
        }

        #MailBody {
            white-space: pre-wrap;
        }
        #MailbodyContent {
            white-space: pre-line;
        }
        #MailBody {
            white-space: pre-line;
        }
    </style>
    <script>
        function changeTo(val) {
            document.getElementById("toMail").innerHTML = val;
        }

        function changeSubject(val) {
            document.getElementById("subject").innerHTML = val;
        }

        function changeBody(val) {
            var text = val;
            document.getElementById("MailbodyContent").textContent = text;
            document.getElementById("MailbodyContent").innerHTML.replace(/\n/g, '<br>\n');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <input id="DestinationAdress" oninput="changeTo(value)" runat="server" type="text" placeholder="Recievers E-mail adress" /><br />
        <input id="MailSubject" oninput="changeSubject(value)" runat="server" type="text" placeholder="Subject" /><br />
        <textarea id="MailBody" oninput="changeBody(value)" runat="server" cols="40" rows="5" placeholder="Content"></textarea><br />
        <br />

        <div class="mailPreview" id="mailPreview">
            <h3>To: <span id="toMail"></span></h3>
            <h3>From: (Just gonna cover my email)</h3>
            <br />
            <h3>Subject: <span id="subject"></span></h3>
            <br />
            <h3>Content:</h3>
            <h4 runat="server" id="MailbodyContent"></h4>
            <asp:HiddenField ID="HiddenField1" runat="server" />
        </div>
        
        <asp:Button ID="Button1" runat="server" Text="Send mail!" OnClick="Button1_Click" />
    </form>
</body>
</html>

Any help would be greatly appriciated!

Since you are sending HTML E-Mails ( msgObj.IsBodyHtml = true; ) you need to replace newlines with the <br /> tag.

Replace the line string YourMessageBody = MailBody.Value;

with

string YourMessageBody = MailBody.Value.Replace(Environment.NewLine, "<br />");

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