473,326 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
8 19736
Plater
7,872 Expert 4TB
"\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
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
/bump ? Anyone out there been through this?
Aug 7 '07 #4
Floydan
24
Whats the code you use for searching for line breaks?

/bump ? Anyone out there been through this?
Aug 7 '07 #5
Plater
7,872 Expert 4TB
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
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
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
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

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

Similar topics

6
by: | last post by:
Hi, I want to send an Email using ASP (I know how to to this) The body of the Email contains several variabels How do I concat string values and separate them by carriage returns/ Line feeds? ...
1
by: Neil S. | last post by:
I am writing an ISAPI filter which is using CAPICOM to encrypt and decrypt cookie information. I've found that the encryption string being returned by CAPICOM a has carriage control and line feed....
1
by: wschaub | last post by:
Is there any way of forcing an ASMX web service not to translate a carriage return – line feed (\r\n) with a (\n\n), in other words the carriage return \r is replaced with a \n if contained as...
3
by: Tim | last post by:
How do I create a string that contains a carriage return line feed in the middle. For example: "string 1" + crlf + "string 2" would output: string1 string2 Thanks in advance for your help.
2
by: Torsten Zachert | last post by:
I would like to insert some text with embedded carriage return/line feed into a MS Access text field with OleDb and C# ADO.NET. I tried to use "\n" in combination with "\r". If I display the input...
0
by: John Dalberg | last post by:
I noticed that when I add key/value pairs to a NameValueCollection variable and send this collection through a POST using WebClient.UploadValues method, the recepient process receives the...
2
by: Enrico Sabbadin | last post by:
Hi, A few days go I fiund iut that the xmlserializer strips away /r/n out during deserialization (and just leave /n) ... I found out that you can resolve this problem using the deserialize...
4
by: whitej77777 | last post by:
I am trying to write a user defined function that will allow me to strip off the last carriage return and line feed from a text field. We have address fields stored in a text field for our ERP...
0
by: Hayduke | last post by:
I've observed various posts here and elsewhere concerning CRLFs getting stripped out of programatically generated emails. This behavior is evident when the email is viewed using various versions of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.