By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

Sending a Carriage Retun/Line Feed frm C# to Outlook

P: 5
using System.Net.Mail; I've been unable to get a carriage return/line feed into the body of my Outlook message. Have look all over the web for a soltion with no luck. Here's what I have tried already, code is in a loop..,

//body += "%0A" + txtMessage.Text.Substring(CurPos, 1);
//body += "<CF><LF>" + " " +txtMessage.Text.Substring(CurPos, 1);
//body += "'CRLF:\r\nPost CRLF'" + txtMessage.Text.Substring(CurPos, 1);
//body += "<br>" + txtMessage.Text.Substring(CurPos, 1);
//body += "'\r\n'" + txtMessage.Text.Substring(CurPos, 1);
//body += ":::*" + txtMessage.Text.Substring(CurPos, 1);
body += "%0d%0a" + txtMessage.Text.Substring(CurPos, 1);

Feel like I'm missing something rather obvious like not escaping properly. Any help is appreciated.

Kirk
Aug 1 '07 #1
Share this Question
Share on Google+
8 Replies


Plater
Expert 5K+
P: 7,872
"\r\n" would be correct if you are sending email as textbased.
"<br/>" would be correct if you are sending email as html-based.
Have you done a debug and checked to see what the body string looks like?
The Watcher has a magnifying glass that looks you look at the string as if it were plaintext or as html (also xml, but that shouldn't apply here)
Aug 1 '07 #2

P: 5
Well, here's the big picture and thanks for replying Plater.

the initial text in the richtextbox...

looking for a

carriage return

The consistent output in the Outlook email message...

looking for a carriage return

In the watch window I see both...

body += "<br/>" + txtMessage.Text.Substring(CurPos, 1);

and

body += "'\r\n'" + txtMessage.Text.Substring(CurPos, 1);

producing

"looking for a \n\ncarriage return"


If I view the source in the Outlook email message I see, (code to follow, I'm inserting a graphic)...

<body>looking for a [][]carriage return<div style="font-family:Arial"><br /><br /><img src="cid:sigjpg@zofm" alt=""><br /><br /></div></body>

I manually replicated the two little boxes I see in Outlook view source above and display when pasted into notepad, I know at least in notepad's case it can't display or interpret the characters, When I directly paste the information from view source in Outlook to this fourm or in Word 2007 the carriage returns work as seen next...

<body>looking for a

carriage return<div style="font-family:Arial"><br /><br /><img src="cid:sigjpg@zofm" alt=""><br /><br /></div></body>



Here's the code for generating the message body...

message.IsBodyHtml = true;
message.Body = body + "<div style=\"font-family:Arial\"><br /><br /><img src=\"@@IMAGE@@\" alt=\"\"><br /><br /></div>";

also tried...

message.Body = "<body>" + body + "<div style=\"font-family:Arial\"><br /><br /><img src=\"@@IMAGE@@\" alt=\"\"><br /><br /></div>" + "</body>";

// the file I'm embedding
string contentID = Path.GetFileName(attachmentPath).Replace(".", "") + "@zofm";

//setting up the content mine type
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/jpg";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
message.Attachments.Add(inline);

// replace the tag with the correct content ID
message.Body = message.Body.Replace("@@IMAGE@@", "cid:" + contentID);

I think it has something to do with the concatenated text I'm looping through from the richtextbox looking for carriage returns, then going in to a string variable before passing it into the message body. The literal text is forming properly. I started parsing through the richtextbox for the carriage returns after assigning the richtextbox directly to the message.body initially failed to show the CR LFs too in the Outlook message. Not sure it matters but my Outlook client is 2007 and is set to display email in HTML
Aug 2 '07 #3

P: 5
/bump ? Anyone out there been through this?
Aug 7 '07 #4

Floydan
P: 24
Whats the code you use for searching for line breaks?

/bump ? Anyone out there been through this?
Aug 7 '07 #5

Plater
Expert 5K+
P: 7,872
that is very strange.
You are welcome to send a test email to my email to see if it works.
Although I have outlook 2003 (set to see as html)

You could try sending it to a webmail based or even a text based email client and see what it looks like there.
Aug 7 '07 #6

P: 5
Be happy to zip up the project and email it to you Plater. This is so weird. I've lost days on something that should take seconds. I've been all over the web and MS and find nothing even for a clue.

Have tried other formats such as my Sprint PCs email account. Interesting if I past the "...looking for a [][]carriage return..." in something like MS word the CRLF works which unlike notepad can interpet the characters.
Aug 7 '07 #7

P: 1
I got this to work using "%0D%0A", the case isn't an issue for me.
i.e.
messageBox.Text.Replace(Environment.NewLine, "%0D%0A")


This works for Outlook 2007 and Outlook Express 6 on Win XP
Aug 15 '07 #8

P: 5
Here a complete listing of the class module. I'm beginning to think it an encapsulation issue, i.e. needs to be inside <HTML></HTML> since that's the message format.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.IO;

namespace Gulfstream
{
public partial class Gulfstream : Form
{
string to = "";
string from = "";
string subject = "";
string body = @"";
string server = "";
int port;
string login = "";
string password = "";

public Gulfstream()
{
InitializeComponent();
ReadFile();
}

public void ReadFile()
{
//string DefaultValuesPath = @"DefaultValues.txt";
string DefaultValuesPath = Environment.CurrentDirectory + @"\\DefaultValues.txt";

// Open the stream and read it back.
FileStream fs = File.OpenRead(DefaultValuesPath);

{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b, 0, b.Length) > 0)
{
string strDefaultData = temp.GetString(b);

string CurChar = "";
//string StrToWrite = "";
int Instance = 0;
int OldPos = 1;
int CurPos = 2;
int LastPos = strDefaultData.Length;

while (CurPos < LastPos)
{
CurChar = strDefaultData.Substring(CurPos, 1);
if (CurChar == "=")
{
Instance++;
switch (Instance)
{
case 1:
// SMTP Server
this.txtServer.Text = strDefaultData.Substring(OldPos, (CurPos - 2) - OldPos);
OldPos = CurPos;
break;
case 2:
// SMTP Port
this.txtPort.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 3:
// Login ID
this.txtSerLogin.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 4:
// Login password
this.txtSerPassword.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 5:
// From email address
this.txtFrom.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 6:
// Subject
this.txtSubject.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 7:
// Body
this.txtMessage.Text = strDefaultData.Substring(OldPos + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;

default:
break;
}


}

CurPos++;
}
}
}
fs.Close();
}

public void WriteFile()
{
//String StrToWrite = "=" + this.txtServer.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtSerLogin.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtSerPassword.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtFrom.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtSubject.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtMessage.Text + //r//n + "=";

//string path = @"DefaultValues.txt";
string DefaultValuesPath = Environment.CurrentDirectory + @"\\DefaultValues.txt";

//delete old file first
if (File.Exists(DefaultValuesPath))
{
File.Delete(DefaultValuesPath);
}

// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
StreamWriter sw = new StreamWriter(DefaultValuesPath);
// Add some text to the file.
// sw.Write("This is the ");
sw.WriteLine("=" + this.txtServer.Text);
sw.WriteLine("=" + this.txtPort.Text);
sw.WriteLine("=" + this.txtSerLogin.Text);
sw.WriteLine("=" + this.txtSerPassword.Text);
sw.WriteLine("=" + this.txtFrom.Text);
sw.WriteLine("=" + this.txtSubject.Text);
sw.WriteLine("=" + this.txtMessage.Text);
sw.WriteLine("=");

sw.Close();
}

public void CreateTestMessage1(string server, int port)
{
try
{
MailMessage message = new MailMessage(from, to, subject, body);
//MailMessage message = new MailMessage(from, to);

// information
message.Subject = subject;
message.IsBodyHtml = true;
// message.Body = body + "<div style=\"font-family:Arial\"><br /><br /><img src=\"@@IMAGE@@\" alt=\"\"><br /><br /></div>";


message.Body = "<body>" + body + "<div style=\"font-family:Arial\"><br /><br /><img src=\"@@IMAGE@@\" alt=\"\"><br /><br /></div>" + "</body>";

string attachmentPath = Environment.CurrentDirectory + @"\\sig.jpg";


System.Net.Mail.Attachment inline = new System.Net.Mail.Attachment(attachmentPath);


string contentID = Path.GetFileName(attachmentPath).Replace(".", "") + "@zofm";
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/jpg";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
message.Attachments.Add(inline);

// replace the tag with the correct content ID
message.Body = message.Body.Replace("@@IMAGE@@", "cid:" + contentID);


SmtpClient client = new SmtpClient(server, port);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.Credentials = new System.Net.NetworkCredential(login, password);
//client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
lblError.Text = "Email sent!";
}
catch (Exception e)
{
lblError.Text = e.Message.ToString();
}


}

private void butSave_Click(object sender, EventArgs e)
{

}

private void butExit_Click(object sender, EventArgs e)
{

}

private void butRun_Click(object sender, EventArgs e)
{
lblError.Text = "";
int warn = 0;

if (txtTo.Text == "")
{
warn = 1;
}
else if(txtFrom.Text == "")
{
warn = 1;
}
else if (txtSerPassword.Text == "")
{
warn = 1;
}
else if (txtSubject.Text == "")
{
warn = 1;
}
else if (txtPort.Text == "")
{
warn = 1;
}
else if (txtSerLogin.Text == "")
{
warn = 1;
}
else if (txtMessage.Text == "")
{
warn = 1;
}

int first = 0;
first = txtFrom.Text.IndexOf("@gulfstream.com");

if (first == 0)
{
warn = 2;
}

if (warn == 1)
{
lblError.Text = "All fields must be completed!";
}
else if (warn == 2)
{
lblError.Text = "For use by Gulfstream employees only!";
}
else
{
to = txtTo.Text;
from = txtFrom.Text;
login = txtSerLogin.Text;
password = txtSerPassword.Text;
subject = txtSubject.Text;

// loop through body text looking for carriage returns /n

int CurPos = 0;
string CurChar = "";

while (CurPos < txtMessage.Text.Length)
{
CurChar = txtMessage.Text.Substring(CurPos, 1);
if (CurChar == "=")
{
//body += "%0A" + txtMessage.Text.Substring(CurPos, 1);
//body += "<CF><LF>" + " " +txtMessage.Text.Substring(CurPos, 1);
//body += "'CRLF:\r\nPost CRLF'" + txtMessage.Text.Substring(CurPos, 1);
//body += "<br/>" + txtMessage.Text.Substring(CurPos, 1);
//body += "'\r\n'" + txtMessage.Text.Substring(CurPos, 1);
//body += ":::*" + txtMessage.Text.Substring(CurPos, 1);
//body += "%0d%0a" + txtMessage.Text.Substring(CurPos, 1);
//body += "%0d%0a" + txtMessage.Text.Substring(CurPos, 1);
//body += "%0D%0A" + txtMessage.Text.Substring(CurPos, 1);

body += Environment.NewLine + "%0D%0A" + txtMessage.Text.Substring(CurPos, 1);






}
else
{
body += txtMessage.Text.Substring(CurPos, 1);
}

CurPos++;


}

//body = txtMessage.Text;
server = txtServer.Text;
port = Int32.Parse(txtPort.Text);
CreateTestMessage1(server, port);

}
}

private void butExit_Click_1(object sender, EventArgs e)
{
this.Dispose();
}

private void butSave_Click_1(object sender, EventArgs e)
{
WriteFile();
lblError.Text = "Default values updated!";
}

private void butLoad_Click(object sender, EventArgs e)
{
ReadFile();
}

private void txtTo_TextChanged(object sender, EventArgs e)
{

}

private void pnlSMTP_Paint(object sender, PaintEventArgs e)
{

}

private void lblSerLogin_Click(object sender, EventArgs e)
{

}

}
}

The output...

Aug 15 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.