I had an interesting bug today when sending plain text emails from our system. We format messages like this:
-1st something happened blab bla.
-2nd something else happened blab blab blaa bla.
Today we had an email that looked like this:
-1st something happened blab bla.
-2nd something else happened blab blab blaa bla $1000.00 -3rd Something happened.
So above, we see that we lost the CrLf but only on the message that didn't have a period and ended in 0. I went through the code and found the CrLf is intack up until we send the email. I tracked the code down below, I am guessing it applies to C# as well:
NOTE: We use Environment.NewLine when building the string for the body.
Building the string:
If Not errorList Is Nothing Then
If errorList.Count > 0 Then
strBldrBody.Append(EMailHelper.CrLf)
strBldrBody.Append(EMailHelper.CrLf)
strBldrBody.Append("Response Error List:")
For Each itm As String In errorList
strBldrBody.Append(EMailHelper.CrLf)
strBldrBody.Append(DataHelper.DASH)
strBldrBody.Append(itm)
Next
End If
End If
Email encoding setting:
Try
If Not String.IsNullOrEmpty(recipient) Then
Using mailMsg As MailMessage = New MailMessage()
mailMsg.From = New MailAddress(_configHelper.EmailFrom)
mailMsg.Subject = subject
mailMsg.Body = body
mailMsg.BodyEncoding = Encoding.UTF8
EMailHelper.SetToAddress(recipient, mailMsg)
Dim smtpClient As SmtpClient = New SmtpClient(_configHelper.EmailRelayServer)
smtpClient.Send(mailMsg)
End Using
End If
Catch ex As System.Exception
'logs error
End Try
I want to know what happened in the string translation during the UTF-8 encoding/decoding that removes the CrLf?!
The problem was Outlook, see below:
outlook screen shot
We just had a problem similar to this, but it turned out to be a conversion we did from a bytestream to a string. I have also seen this happen when getting encodings mixed up. From the code posted, I don't see how this could happen.
Oh, and to Hans - he meant 'intact' not 'in tack'
You say that the message is intact (including that newline) up to the point where you send the message, but when the message was received it didn't contain the newline? Can you duplicate this error?
Whereas it's possible that the SmtpClient control somehow lost the newline, it's also possible that one of the relay servers lost it in translation somewhere, or that your email client didn't render it correctly.
Messages go through an incredible number of different translation steps between servers, and some of those translations are ... less than rigorously debugged. In particular, the quoted-printable encoding has some interesting edge cases that lots of implementations don't get correct.
If your relay server is local, I would suggest turning on logging so that it will save a copy of the message as it's received from you. And on the receiving end, get the actual bits of the message (if possible), without any kind of translation by client software.
Related
I have a problem with the email Subject. I always get an email when there is an error somewhere. So when I look at the email, everything seems fine. But when I watch the characteristics off it, I can see a few errors.
For example:
Subject: Text: Alarm Text about an error
For some reason a carriage return and a space at the start of the new line:
It’s still the same Subject again with a carriage return*
As you can see, for some reasons there are carriage returns (CRLF) which are completely random.
What I’ve tried so far is to check when this happens. Found out that it is Random. When I’m trying to read it out (C#), the Subject is technically right. But instead off a carriage return, there is just nothing. To clarify this: the Subject should be like this:
Subject: Text: Alarm Text about an error for some reason a carriage return and a space at the start of the new line: It’s still the same Subject with a carriage return*
But I get it in the Code like this:
Subject: Text: Alarm Text about an errorfor some reason a carriage return and a space at the start of the new line:It’s still the same Subject with a carriage return
As you can see there is just nothing and there should be a space. So far I’ve tried to change the CRLF to space. Which obviously did not work ‘cause there is no carriage return in the Code. Then I tried to change the encoding to base64. Reason for that was:
http://www.kodokmarton.eu/desktop-application-web-programming/41-web-programming/129-random-newline-and-spaces-inserted-in-emails
So then I tried it like this:
How do I encode and decode a base64 string?
So far nothing worked. This is how I read the mails out:
imap = new AE.Net.Mail.ImapClient(mailServer, login, password, AuthMethods.Login, port, ssl);
var msgs = imap.SearchMessages(SearchCondition.Subject("text Subject"));
for (int i = 0; i < msgs.Length; i++)
{
MailMessage msg = new MailMessage();
msg = msgs[i].Value;
string Subject = msg.Subject;
NOTE: I have to fix it in the Code. For some reasons I can not change the way I send Emails or other things. It has to be in the Code.
Thanks for any advice!
I have a programm, interpreting the attachement of incoming mails and writing the results of my findings in the body of the received email.
No problem at all so far...the problem comes with mails that are signed. I am able to get the attachements of the signed mail by interpreting the .p7m-File that is attached, and writing into the body of the message like this:
emailMessage.Body += string.Format("</br></br>Erste Abweichung ({0} Fahrplan):</br>{1} - {2}",
kind, pos.FromTime.ToString("dd.MM.yyyy HH:mm:ss"),
pos.ToTime.ToString("dd.MM.yyyy HH:mm:ss"));
emailMessage.Update(ConflictResolutionMode.AutoResolve);
I can see that the body property is set in Visual Studio, but in Outlook I don't see any body text. It works great when the message is not signed.
The problem now is that I don't know if this is a problem with outlook, or if I somehow have to sign the body text that I have created.
Any hint would be appreciated, thanks!
For the signed/encrypted messages, the body is always extracted from the p7m attachment. PR_BODY, PR_HTML or PR_RTF_COMPRESSED are not used.
Think about it - the whole pointy of signing a message is to prevent anybody from tampering with its contents. That is precisely what you are trying to do.
You can of course turn the signed/encrypted message into a regular message by setting the MessageClass property to "IPM.Note" and extracting the data from the p7m file, but I doubt your users will appreciate that.
I have some code that sends an email.
The code is generally along the lines of :
Stringbuilder sb = new StringBuilder();
sb.Append("Field1:" + Field1.Text);
sb.Append(System.Environment.NewLine);
.
.
.
The string is built, then converted to a string and passed to a function that sends the email.
This works for most of the fields in the form. However, there are a couple of fields that never get a line break. It's really weird that the same code is behaving differently.
I've read there are numerous ways to add a newline in a stringBuilder, and I've also tried
sb.AppendLine()
But that didn't work either.
Suggestions?
Various mail clients will try to mess around with emails to try to make them look more like they think you want them to.
One common example is that sometimes plain text emails are artificially line broken in the sending process after a certain number of characters and a mail client might try to detect this and strip out those artificial line breaks. Most often it will do this by thinking that a single line break is a "fake" and that a double line break represents a new paragraph and should be preserved.
The best way around this is to send mail as HTML. Mail clients may have settings to turn off this undesired behaviour but there is no way to disable it in the mail itself as far as I am aware.
Use an HTML line break tag. Make sure the email is set to allow HTML.
Stringbuilder sb = new StringBuilder();
sb.Append("Field1:" + Field1.Text);
sb.AppendLine("<br />"); //use AppendLine so that source looks pretty. Use HTML break so that the rendered text has a new line
public static void SendMail(MailMessage mail)
{
mail.IsBodyHtml=true;
//send the email
}
I am sending a mail to an email address which forwards messages to a pager. The message I am sending is
"Oth info; Thankyou testing now complete just be aware that 34a door
will open and shut when pager messages are sent with the CFSRES lt1".
but client receives it on his pager as
"Oth info; Thankyou testing now complete just be aware that 34a door
will op= en and shut when pager messages are sent with the CFSRES
lt1".
Does anyone know why the equals sign shows up in open changed to op= en. I know that special characters may some times be changed, like spaces can become %20, but open does not contain any special character, so as far as I know, nothing should happen to it.
This is a sign of Quoted-printable encoding
QP works by using the equals sign "=" as an escape character. It also limits line length to 76, as some software has limits on line length.
So you may try to split the message into multiple lines in an attempt to prevent escape characters being added by the forwarder.
I search for net and found the solution, this links solve my problem. Here is some userfull code, i need to provide the CreateAlternateViewFromString.
MailMessage emailmsg = new MailMessage("from#address.co.za", "to#address.co.za")
emailmsg.Subject = "Subject";
emailmsg.IsBodyHtml = false;
emailmsg.ReplyToList.Add("from#address.co.za");
emailmsg.BodyEncoding = System.Text.Encoding.UTF8;
emailmsg.HeadersEncoding = System.Text.Encoding.UTF8;
emailmsg.SubjectEncoding = System.Text.Encoding.UTF8;
emailmsg.Body = null;
var plainView = AlternateView.CreateAlternateViewFromString(EmailBody, emailmsg.BodyEncoding, "text/plain");
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);
SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);
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"