2016-02-26 46 views
2

我的代碼允許用戶鍵入文本框並使用Outlook將其消息發送給其他用戶。該程序工作得很好,除非用戶使用多行代碼輸入東西作爲單行打開。C#使用多行發送Outlook電子郵件

例如:

「你好,約翰,

你今天怎麼

真誠的,馬克?」

將顯示爲「你好,約翰,你今天好嗎真誠? ,Mark「

我怎樣才能以正確的間距發送這些消息?

的代碼爲我的文本框爲:

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

回答

2

您作爲HTML發送,其中新線沒有任何意義。

  1. 發送純文本:

    oMsg.Body = message;

  2. 格式爲HTML:

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

不建議

自動化從ASP.Net辦公室,可以考慮使用一個SMTPClient到nd電子郵件代替。

按照事實你也不是cleaning up all your COM referencesSendMail,它本身可能是有問題的。

+0

非常感謝!重新格式化HTML已經解決了這個問題。此外,我很欣賞清理我的COM參考的建議。 – Dominick

相關問題