473,652 Members | 3,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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(CurP os, 1);
//body += "<CF><LF>" + " " +txtMessage.Tex t.Substring(Cur Pos, 1);
//body += "'CRLF:\r\n Post CRLF'" + txtMessage.Text .Substring(CurP os, 1);
//body += "<br>" + txtMessage.Text .Substring(CurP os, 1);
//body += "'\r\n'" + txtMessage.Text .Substring(CurP os, 1);
//body += ":::*" + txtMessage.Text .Substring(CurP os, 1);
body += "%0d%0a" + txtMessage.Text .Substring(CurP os, 1);

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

Kirk
Aug 1 '07 #1
8 19781
Plater
7,872 Recognized Expert Expert
"\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
kcovert
5 New Member
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(CurP os, 1);

and

body += "'\r\n'" + txtMessage.Text .Substring(CurP os, 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.IsBodyH tml = 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.GetFileNam e(attachmentPat h).Replace(".", "") + "@zofm";

//setting up the content mine type
inline.ContentD isposition.Inli ne = true;
inline.ContentD isposition.Disp ositionType = DispositionType Names.Inline;
inline.ContentI d = contentID;
inline.ContentT ype.MediaType = "image/jpg";
inline.ContentT ype.Name = Path.GetFileNam e(attachmentPat h);
message.Attachm ents.Add(inline );

// replace the tag with the correct content ID
message.Body = message.Body.Re place("@@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
kcovert
5 New Member
/bump ? Anyone out there been through this?
Aug 7 '07 #4
Floydan
24 New Member
Whats the code you use for searching for line breaks?

/bump ? Anyone out there been through this?
Aug 7 '07 #5
Plater
7,872 Recognized Expert Expert
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
kcovert
5 New Member
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
geekatwork
1 New Member
I got this to work using "%0D%0A", the case isn't an issue for me.
i.e.
messageBox.Text .Replace(Enviro nment.NewLine, "%0D%0A")


This works for Outlook 2007 and Outlook Express 6 on Win XP
Aug 15 '07 #8
kcovert
5 New Member
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.Collecti ons.Generic;
using System.Componen tModel;
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()
{
InitializeCompo nent();
ReadFile();
}

public void ReadFile()
{
//string DefaultValuesPa th = @"DefaultValues .txt";
string DefaultValuesPa th = Environment.Cur rentDirectory + @"\\DefaultValu es.txt";

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

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

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(CurPo s, 1);
if (CurChar == "=")
{
Instance++;
switch (Instance)
{
case 1:
// SMTP Server
this.txtServer. Text = strDefaultData. Substring(OldPo s, (CurPos - 2) - OldPos);
OldPos = CurPos;
break;
case 2:
// SMTP Port
this.txtPort.Te xt = strDefaultData. Substring(OldPo s + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 3:
// Login ID
this.txtSerLogi n.Text = strDefaultData. Substring(OldPo s + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 4:
// Login password
this.txtSerPass word.Text = strDefaultData. Substring(OldPo s + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 5:
// From email address
this.txtFrom.Te xt = strDefaultData. Substring(OldPo s + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 6:
// Subject
this.txtSubject .Text = strDefaultData. Substring(OldPo s + 1, (CurPos - 3) - OldPos);
OldPos = CurPos;
break;
case 7:
// Body
this.txtMessage .Text = strDefaultData. Substring(OldPo s + 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.txtSerLogi n.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtSerPass word.Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtFrom.Te xt + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtSubject .Text + //r//n;
//StrToWrite = StrToWrite + "=" + this.txtMessage .Text + //r//n + "=";

//string path = @"DefaultValues .txt";
string DefaultValuesPa th = Environment.Cur rentDirectory + @"\\DefaultValu es.txt";

//delete old file first
if (File.Exists(De faultValuesPath ))
{
File.Delete(Def aultValuesPath) ;
}

// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
StreamWriter sw = new StreamWriter(De faultValuesPath );
// Add some text to the file.
// sw.Write("This is the ");
sw.WriteLine("= " + this.txtServer. Text);
sw.WriteLine("= " + this.txtPort.Te xt);
sw.WriteLine("= " + this.txtSerLogi n.Text);
sw.WriteLine("= " + this.txtSerPass word.Text);
sw.WriteLine("= " + this.txtFrom.Te xt);
sw.WriteLine("= " + this.txtSubject .Text);
sw.WriteLine("= " + this.txtMessage .Text);
sw.WriteLine("= ");

sw.Close();
}

public void CreateTestMessa ge1(string server, int port)
{
try
{
MailMessage message = new MailMessage(fro m, to, subject, body);
//MailMessage message = new MailMessage(fro m, to);

// information
message.Subject = subject;
message.IsBodyH tml = 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.Cur rentDirectory + @"\\sig.jpg" ;


System.Net.Mail .Attachment inline = new System.Net.Mail .Attachment(att achmentPath);


string contentID = Path.GetFileNam e(attachmentPat h).Replace(".", "") + "@zofm";
inline.ContentD isposition.Inli ne = true;
inline.ContentD isposition.Disp ositionType = DispositionType Names.Inline;
inline.ContentI d = contentID;
inline.ContentT ype.MediaType = "image/jpg";
inline.ContentT ype.Name = Path.GetFileNam e(attachmentPat h);
message.Attachm ents.Add(inline );

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


SmtpClient client = new SmtpClient(serv er, port);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.Credenti als = new System.Net.Netw orkCredential(l ogin, password);
//client.Credenti als = CredentialCache .DefaultNetwork Credentials;
client.Send(mes sage);
lblError.Text = "Email sent!";
}
catch (Exception e)
{
lblError.Text = e.Message.ToStr ing();
}


}

private void butSave_Click(o bject sender, EventArgs e)
{

}

private void butExit_Click(o bject sender, EventArgs e)
{

}

private void butRun_Click(ob ject 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.Tex t == "")
{
warn = 1;
}
else if (txtPort.Text == "")
{
warn = 1;
}
else if (txtSerLogin.Te xt == "")
{
warn = 1;
}
else if (txtMessage.Tex t == "")
{
warn = 1;
}

int first = 0;
first = txtFrom.Text.In dexOf("@gulfstr eam.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.Tex t;
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(CurP os, 1);
if (CurChar == "=")
{
//body += "%0A" + txtMessage.Text .Substring(CurP os, 1);
//body += "<CF><LF>" + " " +txtMessage.Tex t.Substring(Cur Pos, 1);
//body += "'CRLF:\r\n Post CRLF'" + txtMessage.Text .Substring(CurP os, 1);
//body += "<br/>" + txtMessage.Text .Substring(CurP os, 1);
//body += "'\r\n'" + txtMessage.Text .Substring(CurP os, 1);
//body += ":::*" + txtMessage.Text .Substring(CurP os, 1);
//body += "%0d%0a" + txtMessage.Text .Substring(CurP os, 1);
//body += "%0d%0a" + txtMessage.Text .Substring(CurP os, 1);
//body += "%0D%0A" + txtMessage.Text .Substring(CurP os, 1);

body += Environment.New Line + "%0D%0A" + txtMessage.Text .Substring(CurP os, 1);






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

CurPos++;


}

//body = txtMessage.Text ;
server = txtServer.Text;
port = Int32.Parse(txt Port.Text);
CreateTestMessa ge1(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(o bject sender, EventArgs e)
{
ReadFile();
}

private void txtTo_TextChang ed(object sender, EventArgs e)
{

}

private void pnlSMTP_Paint(o bject sender, PaintEventArgs e)
{

}

private void lblSerLogin_Cli ck(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
31252
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? Any help will be greatly appreciate. Regards,
1
2207
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. (Hex 0d0a). My filter adds the header in the correct format, including the entire encrypted cookie value (set-cookie: mycookie=theEncryptedValue;path=/;). However, when the isapi filter finishes running, the browser only gets part of the...
1
850
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 part of a string that is transferred to a web service method. The XML 1.1 standard is supposed to address this problem, however, we are not aware of any plans of ASMX and serializers to implement this standard in the near future.
3
2234
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
18765
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 in a text field I only see quads. Tia Torsten
0
1108
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 collection ok except that the last value has a \r\n padded, which is not expected. Is this normal behaviour? My workaround is to add another pair/value which serves as dummy and let the \r\n get padded to it.
2
900
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 overlaod that takes an xmltextreader instead of a stream .. However when it's time to Web Services you have no such a control over the deserialization phase AND I found out that WS strips out /r/n (an leave /n) ... I understand there are some XMl...
4
27814
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 system and some of them have an extra carriage return and line feed at the end of them. This causes havoc when we sync between our ERP system and CRM system. If anyone knows a way to solve this problem the help would be appreciated. Examples:...
0
1787
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 Microsoft Outlook. Outlook displays a message like "Extra line breaks in this message were removed". I've had the same problem for a long time, and found a solution quite by accident. Ensuring that each line ends with a period (.) prior to the...
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8703
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.