Body not shown in signed mail message - c#

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.

Related

Unwanted <end> tag appearing in email sent using SmtpClient [duplicate]

I'm using Microsoft.Office.Interop.Outlook, VB.net and Office 2013 to generate a MailItem, and then send the item to Outlook, show the email window and let the user edit it/send it from Outlook 2013. The main things I'm doing are:
I create the Microsoft.Office.Interop.Outlook.MailItem object and fill it with the relevant information, I generate an HTML constant for the body like this
Private Const mstrHTML_FORMAT As String = "<html><p style='font-size:10pt;font-family:Arial;'>{0}</p></html>"
Then I add the text I want to a string variable strBody and use String.Format to insert the text in the HTMLBody of my object:
objMailItem.HTMLBody = String.Format(mstrHTML_FORMAT, strBody)
I also change the format of the body to HTML:
objMailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML
After a few other steps I send it to the view
objMailItem.Display(True)
My problem is, when the user sends the email, the receiver will see that the email has a message with the subject as Text
any clue of why this happens?
It's an Outlook "feature". Outlook purposely puts <end> in the message preview when the body isn't long enough to fill the preview.
It's not caused by your code or any bad HTML formatting.

Get sent message in Outlook Addin

I am facing some problems trying to get the sent message from an Outlook plugin.
In onItemSend event, I open a dialog where it shows some fields, with message information such as recipients, subject, etc and a button that will save these information into our DB. Another requirement is to save a copy of the sent message and this is where I got stuck...
I could save the message using the SaveAs method but the problem is when I open the message, it shows:
This message has not been sent. This message will be sent via
Microsoft Exchange
causing some problems with users, making them think that the message was not sent.
During my searches, I found this thread where another person had the same problem and the solution was to use the message as PostItem instead of MailItem, once the PostItem is created in sent state. Also, we should set the MessageClass property to IPM.Note and delete PR_ICON_INDEX
Here is the code that I am using to do the steps above. I found this code here and changed a little bit:
PostItem postItem = this._email.Application.CreateItem(OlItemType.olPostItem);
postItem.MessageClass = "IPM.Note";
PropertyAccessor pa = postItem.PropertyAccessor;
pa.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003", -1);
postItem.Save();
NameSpace session = postItem.Session;
string postItemEntryID = postItem.EntryID;
Marshal.ReleaseComObject(postItem);
Marshal.ReleaseComObject(pa);
MailItem newMessage = session.GetItemFromID(postItemEntryID) as MailItem;
newMessage.BCC = this._email.BCC;
newMessage.Body = this._email.Body;
newMessage.BodyFormat = this._email.BodyFormat;
newMessage.CC = this._email.CC;
newMessage.HTMLBody = this._email.HTMLBody;
//Hard coded path just for illustration
newMessage.SaveAs("C:\\Temp\\MSG\test.msg", OlSaveAsType.olMSG);
The code above creates a postitem object, set some of the properties and save to the path correctly, but it has the following problems:
After executing postItem.save, to create the postitem message, it creates a read message in inbox folder
After saving the messages, I have compared the files and the size where significant, the original message size was 580kb and the postitem saved message was 52kb. It seems it did not make a copy of the message
It lost some of the of the images embedded in message, such as signature images, showing a red X in place.
How can I get/create a message, with the exact message content, recipients, attachments, properties, etc (clone kind) with sent state, without creating another message inside inbox folder?
Thank you
I would not do this to a message that Outlook is trying to send. You can
Process the Items.ItemAdd event on the Sent Items folder. By that time the message is sent and all the sender related properties are set.
You can "fix" the created MSG file by removing the unsent flag. You can do that using Redemption (I am its author) - call RDOSession.GetMessageFromMsgFile / RDOMail.Sent = true / RDOMail.Save. Keep in mind that the sender information might not yet be set.
i would not go that way with the "postitem" further, somehow it does not look the perfect way for me.
the Problem is that you are copying the item bevor it is sent. Therefore the copy says it has not been sent.
If you do not need the "normal" copy which is saved in the "sent items"-Folder, you could just Change the Folder where the item is saved with
Set mailitem.SaveSentMessageFolder = someother Folder '(which is defined as Outlook.folder)
if that is not possible, then I would make an inspection (in ThisOutlookSession) of the "sent items" Folder and make the copy-action for every new item in there. If you don't know how to let me know, then I copy you some code to bring you on the way.
another question just because Iam curious: why are you opening the form and waiting for someone to hit the ok-button, instead of saving the data into your db straight away?

C# Pdf Attachment damaged when sent to yahoo mail addresses

I'm trying to send an Email with an attached PDF file. When I send it to any other mail provider it works just fine, but when I send it to a yahoo email address, the receiver gets a damaged pdf file. The exact message it gives is:
Adobe Reader could not open 'Filename.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
Because the other email providers were working, I used the following code specifically for yahoo addresses.
if (thisItem.EmailAddress.ToUpper().Contains("YAHOO")){
ContentType contentType = new ContentType();
contentType.CharSet = Encoding.UTF8.WebName;
Attachment theFile = new Attachment(attachmentPath, contentType);
theFile.Name = theFile.Name.Replace("_","");
mm.Attachments.Add(theFile);
}
I've tried a variety of CharSets on the ContentType, hoping that would fix something, no change. I also tried different TransferEncodings on theFile, also no fix. I read somewhere that the file name could cause problems if it had special characters so I removed all the underscores in the name, all that's left is some letters and numbers. I'm not sure what else to try at this point. Any suggestions?

Line breaks ignored when sending mail as plain text

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"

MailMessage.Body loses CrLf with Encoding.UTF8

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.

Categories

Resources