C#发送多行Outlook电子邮件

C#发送多行Outlook电子邮件,c#,asp.net,email,outlook,C#,Asp.net,Email,Outlook,“我的代码”允许用户在文本框中键入内容,并使用Outlook将其邮件发送给其他用户。该程序运行良好,除非用户键入多行内容,否则它将在outlook中作为一行打开 例如: “你好,约翰 你今天好吗 “真的,马克” 将显示为“你好,约翰,今天好吗?真诚地说,马克” 我如何才能以正确的间隔发送这些消息 我的文本框的代码是: <asp:TextBox ID="txtMessage" runat="server" placeholder="Please Enter Your Message Here

“我的代码”允许用户在文本框中键入内容,并使用Outlook将其邮件发送给其他用户。该程序运行良好,除非用户键入多行内容,否则它将在outlook中作为一行打开

例如:

“你好,约翰

你今天好吗

“真的,马克”

将显示为“你好,约翰,今天好吗?真诚地说,马克”

我如何才能以正确的间隔发送这些消息

我的文本框的代码是:

<asp:TextBox ID="txtMessage" runat="server" placeholder="Please Enter Your Message Here." rows="18" style="width:100%; margin-top:20px; margin-bottom:20px" TextMode="MultiLine" MaxLength="9999" />

您正在以HTML格式发送新行没有意义的内容

  • 以纯文本形式发送:

    oMsg.Body=消息

  • 格式为HTML:

    oMsg.HTMLBody=message.Replace(“\r\n”和“
    ”)

  • 不推荐使用ASP.NET实现自动化办公,请考虑使用<代码> SMTPcli客< /代码>发送电子邮件。
    就目前情况而言,您也不在
    SendMail
    ,这本身就有问题。

    非常感谢!重新格式化HTML已经解决了这个问题。另外,我也很感激你给我的清理COM参考资料的建议。
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    
    namespace Cards
    {
        public partial class _Message : _Default
        {
            //Declaring global variables for the email function to be used in this class.
            protected string toEmail, emailSubj, emailMsg;
            protected void Page_Load(object sender, EventArgs e)
            {
                if (lblName.Text == null) return;
                lblUserId.Text = Session["userid"].ToString();
                lblName.Text = Session["name"].ToString();
                lblBirth.Text = Session["birth"].ToString();
                lblEmail.Text = Session["email"].ToString();
                lblHire.Text = Session["hire"].ToString();
    
            }
    
             protected void btnSend_Click(object sender, EventArgs e)
            {
            //Calling the parts to construct the email being sent
                toEmail = lblEmail.Text;
                emailSubj = "It's Your Special Day";
                emailMsg = txtMessage.Text;
    
                SendMail(toEmail, emailSubj, emailMsg);
                MessageBox.Show("Successfully sent message to " + lblName.Text, "Message Sent!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Response.Redirect("~/Default.aspx");
            }
    
            private static void SendMail(string toEmail, string subj, string message)
            {
                //This class will call all parts to the email functionality and generate the constructors for the email messsage.
                try
                {
                    // Create the Outlook application.
                    Outlook.Application oApp = new Outlook.Application();
                    // Create a new mail item.
                    Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
                    //Add the body of the email
                    oMsg.HTMLBody = message;
                    //Subject line
                    oMsg.Subject = subj;
                    // Add a recipient.
                    Outlook.Recipients oRecips = oMsg.Recipients;
                    // Change the recipient in the next line if necessary.
                    Outlook.Recipient oRecip = oRecips.Add(toEmail);
                    oRecip.Resolve();
                    // Send.
                    oMsg.Send();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error has occurred. Please report this error to the Development team",
                        "Error found!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }