How can I format C# string output to contain newlines? - c#

strEmail =
"Hi All,"
+ "<br>"
+ "The User "
+ lstDataSender[0].ReturnDataset.Tables[0].Rows[0][1].ToString()
+ " has been created on "
+ DateTime.Now.ToShortDateString()
+ "."
I am writing C# code to generate an email whenever a new user has been created. I need the body of the mail in a mail format like
hi,
The user "xxx" has been created on "todaysdate".
Thanks,
yyyy
so I need to insert linebreaks and bold for some characters. How might I achieve this?

If this is a plain text email (which it looks like it is), use \r\n for new lines.
strEmail = string.Concat("Hi All,\r\n\r\nThe User",
lstDataSender[0].ReturnDataset.Tables[0].Rows[0][1].ToString(),
"has been created on ",
DateTime.Now.ToShortDateString(),
".\r\n\r\nThanks, yyyy");
Strickly speaking this should be Environment.NewLine to support different platforms, but I doubt this is a concern.

Set
yourMessage.IsBodyHtml = true
Msdn-Reference can be found here.

In order to form, you can use like below:
Response.Write("Hi,<br/><br/>The user.......");

Related

c# Joining Strings for Email Body [duplicate]

This question already has answers here:
mailto link multiple body lines
(4 answers)
Closed 8 years ago.
I dont know whats happening here but I cant join two strings together with a newline between them. I use the following code in my Windows Store App to open the email client and prefill an email:
String recipient = "me#here.com";
String Bodymsg = "Line One" + Environment.NewLine + "Line Two";
String SubLine = "Report Fault";
String BodyText = "mailto:?to=" + recipient + "&subject=" + SubLine + "&body=" + Bodymsg;
var mailto = new Uri(BodyText);
await Windows.System.Launcher.LaunchUriAsync(mailto);
This works fine but the body comes out like this.
Line OneLineTwo
What I want to get is
Line One
Line Two
I have tried using "/r/n" and "/n" instead of Environment.NewLine but it gives the same effect. Any ideas where I'm going wrong?
The newline character is \n and not /n, have you tried:
String Bodymsg = "Line One\nLine Two";
Without seeing more of your code and how you're using the email, I'd say it might be formatted in HTML. Try concatenating the two strings with a <BR>.

Creating a newline in rich text box

I need help on creating a new line for my RichTextBox which I cant make work when using CheckBox.
It keeps overlapping instead of creating a newline of words.
Tried using the method of rtbdisplay.text = (display+envrionment.newline);
example from my code:
if (rbtnSmall.Checked == true)
{
rtbDisplay.Text = "displaytext".PadRight(20) + "size".PadRight(23) +
qty.ToString().PadRight(20) + StrongDummy;
}
Use the RichTextBox.Text property or the RichtTextBox.AppendText method to append a string with a newline.
myRichTextBox.Text += Environment.NewLine + "My new line.";
// Or
myRichTextBox.AppendText( Environment.NewLine + "My new line." );
You can use c# Environment.NewLine Property as described in http://msdn.microsoft.com/en-us/library/system.environment.newline%28v=vs.110%29.aspx. Or, the "old style" like #"\r\n".
Rgds,

How to send an email with its text/html encoded in UTF-8 using SendGrid for C#?

I'm trying to send emails that contains special characters like á, é, ó, í, ú, etc. My code looks like this:
try
{
Mail message = Mail.GetInstance();
foreach (Users u in users)
{
message.AddBcc(u.Email);
}
message.From = new MailAddress(Microsoft.WindowsAzure.CloudConfigurationManager
.GetSetting("emailFrom"), Microsoft.WindowsAzure.CloudConfigurationManager
.GetSetting("emailFromName"));
message.Subject = Microsoft.WindowsAzure.CloudConfigurationManager
.GetSetting("emailSubject");
message.Html = "<meta charset=\"UTF-8\"/>" +
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText1") +
" " + address+ ".<br/>" +
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText2");
var transport = SMTP.GetInstance(new NetworkCredential(
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailLogin"),
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailPass")));
transport.Deliver(message);
isEmailSent = true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " \n " + ex.InnerException + " \n " + ex.StackTrace);
isEmailSent = false;
}
I tried to specify the charset in the Html I'm sending but it doesn't work, instead of sending this San Jerónimo Amanal, this is what is being sent San Jerónimo Amanal.
How can I send it in the correct encoding? Any help will be appreciated.
EDIT
I tried this two approches:
message.Html = "<meta charset=\"UTF-8\"/>" +
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText1") +
" " + address+ ".<br/>" +
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText2");
And this:
message.Html = Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting
("emailText1") + " " + address+ ".<br/>" +
Microsoft.WindowsAzure.CloudConfigurationManager.GetSetting("emailText2");
But the email is still being sent with the wrong encoding.
EDIT 2
I tried the answer on this question Converting Unicode strings to escaped ascii string, more especifically, I tried the method EncodeNonAsciiCharacters but I got this in the email San Jer\u00c3\u00b3nimo Amanal. Did I take the wrong approach in this one?
Well it took some time, but at least I found a way to send the email without the special characters, I had to normalize the string I got with Normalization Form Compatibility Decomposition, which is done through the String class' Normalize method like this:
string normalizedString = InString.Normalize(NormalizationForm.FormKD);
With this, quoting from wikipedia:
characters are decomposed by compatibility, and multiple combining
characters are arranged in a specific order.
Meaning if there's also a character whose compability is represented by two characters, then it will be replace by said characters. With characters like é, it will be replaced by e.

adding space in texbox results when sending thru email

wassup guys im new to C# coding and i did a search but couldn't find exactly what im looking for. So i have a couple of text-boxes which holds string elements and integers
what i want to do is when these boxes are filled in i want to send a summary of the email to client/customer but the format is whats getting me.
(first, one) are strings equaling different text-boxes
my code is:
emailCompseTask.Body = first + one + Enviroment.NewLine +
second + two + Enviroment.NewLine
and so on problem is which i send thru email it shows something like this:
computer service25.00
instead of:
computer service 25.00
is there a way to add spacing to make this more presentable? or even a better way perhaps thanks in advance guys
try this :
emailCompseTask.Body = first + one + " "+ second + two ;
body takes as HTML input, check here for more spacing option.
I'm a bit confused, but you just want to add some spacing in the output? Just throw some spaces in there like you would another variable.
first + " " + one + Environment.NewLine
+ second + " " + two + Environment.NewLine;
You can use a table
string tableRow = #"<tr>
<td>{0}</td>
<td>{1}</td>
</tr>";
string htmlTable = #"<table>
{0}
</table>";
string rows = "";
// Can do this in a loop
rows += string.Format(tableRow, first, one);
rows += string.Format(tableRow, first, one);
emailComseTask.Body = string.Format(htmlTable, rows);

Minor bug in plain-text to HTML conversion using Regular Expressions

I am facing a minor bug when doing the conversion from plain text to HTML. What might be the reason for this?
Input: (plain-text)
this is test input.
Output: (virtual plain-text but HTML)
this is test input.
BUG: Moves one or two spaces forward. I have no clue why is this happening.
Code for your reference
string Text = "<html><body><pre style=\"font-family:consolas;font-size:88%;\">"
+ mailItem.Body + "</pre></body></html>";
mailItem.HTMLBody = Text;
mailItem.HTMLBody = Regex.Replace(mailItem.HTMLBody,
"(ASA[a-z][a-z][0-9][0-9])", "$&");
I tested the following, and it works (eg. no spaces at beginning of output):
string mailItemBody = "ASAss87";
string oldText = "<html><body><pre style=\"font-family:consolas;font-size:88%;\">"
+ mailItemBody + "</pre></body></html>";
string newText = Regex.Replace(
oldText, "(ASA[a-z][a-z][0-9][0-9])", "$&");
Console.WriteLine("Old text is: \n\n" + oldText + "\n\n");
Console.WriteLine("New text is: \n\n" + newText + "\n\n");
I would investigate the class used to instantiate mailItem, and review at the HTMLBody property to see if anything funny is happening there.

Categories

Resources