繁体   English   中英

C#使用多行发送Outlook电子邮件

[英]C# Send Outlook Email with Multiple Lines

我的代码允许用户键入文本框并使用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" />

我的电子邮件功能的代码是:

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);
            }
        }
    }
}

您将以HTML格式发送新行没有任何意义。

  1. 以纯文本格式发送:

    oMsg.Body = message;

  2. 格式为HTML:

    oMsg.HTMLBody = message.Replace("\\r\\n", "<br />");

建议不要从ASP.Net自动化Office,请考虑使用SMTPClient来发送电子邮件。

事实上,你也没有清理 SendMail 所有COM引用 ,这本身就有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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