I want to add an image into my mail, I have everything working for the most part but my only problem is that in order to have an image I need to set the body to HTML format... which then stops me from having break lines. So I think this is a 2 part question.
Is there a way to have both normal String for the first part of a message body and then the HTML for just the picture? or if not the How can I find and change the break line of a normal String to < br>?
I believe I need to change \n to < br/> in a normal String
body.Replace("\n", "< br/>);
doesn't seem to work...
Try this:
body = body.Replace(Environment.NewLine, "<br />");
the mail format actually is written in the header of the message so its either TEXTformat or HTML format, you cant mix them both in the same message
I am assuming you are using the Mail Libray in .net ? you will need to change your format type from text to HTML with the IsBodyHtml on the mail message
You can just create a html image and use AlternateView to then get the plain text
Sending a mail as both HTML and Plain Text in .net
Related
I have an issue with an mvc4 application in witch i would like to send a generated html page created with HTML + C#. The problem is that when i recieved the email i see my c# code like the exemple bellow:
Recieved Email
But in the mail preview i can see the correct values like this:
Mail Preview
So this is my EmailTemplate method:
<pre>
public static async Task<string> EMailTemplate (string template)
{
var templateFilePath =HostingEnvironment.MapPath("~/Views/Home/") + template + ".cshtml";
StreamReader objstreamreaderfile = new StreamReader(templateFilePath);
var body = await objstreamreaderfile.ReadToEndAsync();
objstreamreaderfile.Close();
return body;
}
</pre>
Please if you have any idea how to convert my template to Html without inclouding my C# code.
Thanks,
Use Pure Html template (Not a cshtml view) with Inline CSS and Absolute image URIs for email body and fill it text templates. See the below image.
In the above image, field with ##...## are text templates.
Now read this HTML template as a string and replace ##...## fields by some dynamic information using the C# code (Actually any language code). You can use string.Replace() method to replace these fields by actual values. I have already used this method and it is working fine. I hope this will help you too.
I have a weird problem with Lotus Notes 8.5. In my project I am sending meeting invitation to the user. for that, I generate .ics file. Here is how i generate .ics file
var body = "Dear Raj, \n\n How are you? line break is not working \n\n how?";
using (TextWriter writer = File.CreateText("../test.ics"))
{
writer.WriteLine("BEGIN:VCALENDAR");
writer.WriteLine("PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN");
writer.WriteLine("VERSION:2.0");
writer.WriteLine("METHOD:REQUEST");
writer.WriteLine("BEGIN:VEVENT");
writer.WriteLine("ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:participant#company.com");
writer.WriteLine("ORGANIZER;CN="Organizer":MAILTO:organizer#test.ccc");
writer.WriteLine("(DTSTART:20141231T010000Z");
writer.WriteLine("DTEND:20141231T010000Z");
writer.WriteLine("TRANSP:OPAQUE");
writer.WriteLine("SEQUENCE:0");
writer.WriteLine("UID:Company-interview-123");
writer.WriteLine("DTSTAMP:20141223T232322Z");
writer.WriteLine("SUMMARY:Interview Scheduled for Job");
writer.WriteLine("DESCRIPTION:{0}", body.Replace("\n","<br />"));
//Adding below property actually fixed the issue.
writer.WriteLine("X-ALT-DESC;FMTTYPE=text/html:{0}", body.Replace("\n","<br />"));
writer.WriteLine("LOCATION:Test Location");
writer.WriteLine("PRIORITY:5");
writer.WriteLine("X-MICROSOFT-CDO-IMPORTANCE:1");
writer.WriteLine("CLASS:PUBLIC");
writer.WriteLine("BEGIN:VALARM");
writer.WriteLine("TRIGGER:-PT15M");
writer.WriteLine("ACTION:DISPLAY");
writer.WriteLine("DESCRIPTION:Reminder");
writer.WriteLine("END:VALARM");
writer.WriteLine("END:VEVENT");
writer.WriteLine("END:VCALENDAR");
}
But Lotus email client is displaying the content as such.
its showing
Dear Raj, <br><br> How are you? line break is not working <br><br> how?
On all other email clients, my content is displaying as
Dear Raj,
How are you? line break is not working
how?
Am i missing something here?
Updated my .ics generation code to add X-ALT-DESC;FMTTYPE=text/html: to fix the issue
I just checked with a vcard that contains your Text in Lotus Notes 8.5 and IBM Notes 9, and it worked exactly as expected. BUT: It worked with your "original" Text without the replace. In the RFC2445 it states, that Line- Breaks have to be encoded as \n:
An intentional formatted text line break MUST only be included in a
"TEXT" property value by representing the line break with the
character sequence of BACKSLASH (US-ASCII decimal 92), followed by a
LATIN SMALL LETTER N (US-ASCII decimal 110) or a LATIN CAPITAL LETTER
N (US-ASCII decimal 78), that is "\n" or "\N".
That means: use
writer.WriteLine("DESCRIPTION:{0}", body);
instead of
writer.WriteLine("DESCRIPTION:{0}", body.Replace("\n","<br>"));
And your problem should be solved
The DESCRIPTION property is not meant to contain any rich text/html content but only plain text.
Lotus Notes may use some other property (X- property) to convey rich text description. Or it may use an ALTREP parameter on the DESCRIPTION, that point to another MIME bodypart in the invitation. See https://www.rfc-editor.org/rfc/rfc5545#section-3.2.1
So what you probably want to do is to send an invitation containing rich text from Lotus Notes to some external account, and then see what the MIME message that you receive looks like.
I am facing a situation to preserve the formatting of plain text email when displaying it as virtual plain text in C sharp. This is done during receiving in Outlook 2007 using VSTO.
The code below does not do the job, instead it converts the body into Times New Roman;Font Size 10 and displays it to the user.
string Text = "<html><body><p style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body+ "</p></body></html>";
mailItem.HTMLBody = Text;
mailItem.HTMLBody = Regex.Replace(mailItem.HTMLBody, "(ASA[a-z][a-z][0-9][0-9])", "$&");
How can I rectify this problem?
EDIT:
Input:
ASAss87
ASAjj98
this is test input
Output:
ASAss87 ASAjj98 this is test input
EDIT 2:
Input:
ASAss87
ASAjj98
this is test input.
Output:
ASAss87
ASAjj98
this is test input.
*Moves one or two spaces forward. I am using tag.
Based on your feedback in the comments, try changing your first line to use Body instead of HTMLBody:
string Text = "<html><body><p style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body+ "</p></body></html>";
Edit: Since the plain text contains line-breaks, maybe you should use a <pre> tag instead of a <p> tag, to prevent it from putting everything on one line.
string Text = "<html><body><pre style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body+ "</pre></body></html>";
Edit2: Alternatively, you can replace all line-breaks with <br> tags.
string Text = "<html><body><p style=\"font-family:consolas;font-size:88%;\">" + mailItem.Body.Replace(Environment.NewLine,"<BR>") + "</p></body></html>";
I have a text like " Hi, \r\n this is test \r\n Thanks" I am sending the mail using MailMessage class. I have set the "IsBodyHtml" property to false. The issue is that I am receiving mails without line breaks. Can you let me know what I am missing?
Use Environment.NewLinemsdn instead of \r\n.
We had the same problem, but if you define your message all at once in a String, as opposed to a StringBuilder, you can define your message like this:
string message = string.Format(
#"First Line: {0}
Second Line: {1}
ThirdLine: {2}", firstValue, secondValue, thirdValue);
Defining the message body like this, and setting IsBodyHtml = false, will give you the new lines that you want.
Otherwise, use StringBuilder
var sb = new StringBuilder();
sb.AppendLine("FirstLine");
sb.AppendLine("SecondLine");
This is a feature in Outlook, you can turn it off in Outlook. Go to Options - Mail - and under "Message Format" you uncheck "Remove extra line breaks in plain text messages".
Another solution is to add three spaces at the end of each line, when you send the mail. This seems to get Outlook to accept that it is not an extra line break.
If you are reading your mails from Outlook, it may be Outlook that is removing line breaks, thinking they are extra line breaks. Did you try reading your mails from another software - or maybe a webmail?
To be able to incorporate line breaks, rather than just plain test in your mail, you will need to have the body html set to true, I think.
A more tricky reason this may happen that I just had to deal with:
Your mail server manipulates outgoing messages and adds a html version to your otherwise text only message
I was having a similar problem where most of my line breaks were working but one was not. If I hit reply to the email that wasn't showing the line breaks, the original email text below the reply would show the line break (so this indicates it is an outlook issue). I tried the recommended answer of Environment.NewLine and that DID NOT change anything. In my case, adding a period at the end of the statement where I wanted the new line and then putting in a \r\n did the trick. Per a previous link I posted in this discussion, Outlook is using some rules to filter out line feeds and in the question that started this discussion I notice you do not have a period at the end of your sentence.
I was sending E-Mail notification via PowerShell script and also did not get any line breaks when viewing the mail in Outlook.
For me the solution in the .ps1 file was this:
$emailMessage.Body = "First Line" + "`r`n" + "Second Line"
I am sending mail to the customers. And the format is like this:
Name: abc. Company:ccc. Address: sde.
So the mail is coming out in continuous format which I don't want.
I want it in this format:
Name: abc.
Company:ccc.
Address: sde.
I'm currently using string.format to populate each of the values and later on replacing '.' with '\n'.
However, it looks like that is not working.
Any suggestions on how to make it look like this?
Use BodyFormat property to format your mails as HTML format, and use <br /> for the line breaks.
MailMessage myMail = new MailMessage();
myMail.BodyFormat = MailFormat.Html;
myMail.Body = "Name: abc.<br />";
myMail.Body += "Company:ccc.<br />";
myMail.Body += "Address: sde.<br />";
Try Environment.NewLine
Have you tried "\n\r"?
Is the body set to be plain text or html?
If you are building a long string, I recommend you use StringBuilder.
StringBuilder sb = new StringBuilder();
sb.AppendLine(string1);
sb.AppendLine(string2):
sb.AppendLine(""); //empty line
mailMessage.Body = sb.ToString();
Edit: I use this to send our text only emails and it works on non-HTML emails.
#Canavar's answer is good, but not all mail clients support html and even those that do may have it turned off for security purposes.
As an alternative, I would say use MailFormat.Text and use the System.Environment.NewLine
If HTML format is ok, you could just insert <br>
If not, and you are using outlook to view the message, try one of these two:
Use \n\n
Keep your period so that the line ends with .\n
I know this seems odd, but it may make outlook happy.