Reputation: 53
My code allows a user to type into a Text Box and send their message to another user using Outlook. The program works perfectly fine except if the user types something with multiple lines it is opened in outlook as a single line.
For example:
"Hello John,
How are you today?
Sincerely, Mark"
Will be displayed as "Hello John, How are you today? Sincerely, Mark"
How am I able to get these messages sent with the correct spacing?
The code for my text box is:
<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" />
The code for my email function is:
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);
}
}
}
}
Upvotes: 3
Views: 1135
Reputation: 175976
You are sending as HTML where new lines have no meaning.
Send as plain text:
oMsg.Body = message;
Format as HTML:
oMsg.HTMLBody = message.Replace("\r\n", "<br />");
Automating Office from ASP.Net is not recommended, consider using an SMTPClient
to send email instead.
As things stand you are also not cleaning up all your COM references in SendMail
, which can itself be problematical.
Upvotes: 2