c# open pop - save e-mail (HTML with inline attachements) to database - c#

Found a problem with saving e-mail to database(as byte[] - already tried with various methods:
saving "RawMessage",
executing Message.
Load via "MemoryStream" etc..),
retrieving it and re sending.
When I send this saved e-mail, recipient does not see inline attached image (in place where image should be is information that picture was not found or couldn't be loaded).
My actual code version:
byte[] toDB = message.RawMessage; // then it goes to DB
//later in code
OpenPop.Mime.Message raw = new OpenPop.Mime.Message(fromDB, true);
//then I fill in new Message object with new values, only body remains the same:
mailMessage.Body = System.Text.Encoding.Default.GetString(raw.FindFirstHtmlVersion().Body);
Thanks :)

Related

Mimekit how to embed svg without attach them

I have this little piece of code
var builder = new BodyBuilder();
var image = builder.LinkedResources.Add(logoPath);
image.ContentId = MimeUtils.GenerateMessageId();
textMsg = textMsg.Replace("{logo}", image.ContentId);
builder.HtmlBody = textMsg;
msg.Body = builder.ToMessageBody();
It works perfectly except that if I use a .png image it isn't attach to the mail but if it is an .svg it is attach. I tried to convert the svg to base64 but it didn't work.
As I show in the images below, if it is a .svg the image is attach at the top of the mail but if it is a.png it doesn't. Is it possible to do the same with .svg? I don't want the attached file.
The issue you are facing is a limitation of the receiving mail client, not anything you are doing wrong with the images in your email message.
The client you are using to receive the message does not support SVG and so has decided that whenever it encounters an SVG image, it will offer it to the user as an attachment.

How to add an attachment to a loaded MimeMessage (MimeKit) [duplicate]

This question already has answers here:
MimeKit add attachments to a message loaded from mht file
(2 answers)
Closed last month.
Is there any general way to add an attachment to a mail read from an stream using MimeKit?
I need something like this
var message = MimeMessage.Load(inputStream);
var newMessage = AddSomeInfoAttachment(message);
newMessage.WriteTo(outputStream)
The part in question is "AddSomeInfoAttachment(message)". The attachment (for now) is just a text file (a string in fact).
It appears to be easy if you create a new message (http://www.mimekit.net/docs/html/Creating-Messages.htm) but I suspect that it's way more complicated to start with an already created one (for instance: Where in the MIME tree is supposed to go the attachment? Do I have to copy all other parts to a newMessage or can I just modify the original message in place?)
So far the only "Attachments" collections (with an Add) I see is using a BodyBuilder and I suspect that is not that easy with an already loaded MimeMessage.
In the most common scenario, the following code snippet should do what you want/expect:
var message = MimeMessage.Load(fileName);
var attachment = new TextPart("plain") {
FileName = "attachment.txt",
ContentTransferEncoding = ContentEncoding.Base64,
Text = attachmentText
};
if (!(message.Body is Multipart multipart &&
multipart.ContentType.Matches("multipart", "mixed"))) {
// The top-level MIME part is not a multipart/mixed.
//
// Attachments are typically added to a multipart/mixed
// container which tends to be the top-level MIME part
// of the message (unless it is signed or encrypted).
//
// If the message is signed or encrypted, though, we do
// do not want to mess with the structure, so the correct
// thing to do there is to encapsulate the top-level part
// in a multipart/mixed just like we are going to do anyway.
multipart = new Multipart("mixed");
// Replace the message body with the multipart/mixed and
// add the old message body to it.
multipart.Add(message.Body);
message.Body = multipart;
}
// Add the attachment.
multipart.Add(attachment);
// Save the message back out to disk.
message.WriteTo(newFileName);

Outlook add-in disable automatic compression of JPEG attachment

I am writing an Outlook add-in to be able to insert automatically an image into an email using CID.
However, everytime I add an image as attachment (jpeg), the image is compressed automatically by Outlook and I have a big lost of quality.
Is it possible to avoid compression of images for attachment?
Here is the code that I am using so far:
var attachment = mailItem.Attachments.Add( #"D:\\image.jpg" , Outlook.OlAttachmentType.olEmbeddeditem , null , "Some image display name" );
string imageCid = "image.jpg#123";
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg"); // PR_ATTACH_MIME_TAG
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", imageCid); // PR_ATTACH_CONTENT_ID
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", true); // Hide attachment in the email
mailItem.HTMLBody = String.Format( "<body><img src=\"cid:{0}\" width='450' height='150' alt=''></body>" , imageCid);
Thanks a lot for any help
Not much you can do if the message is then displayed by Outlook. You can try to add the image immediately prior to it being sent (Aplication.ItemSend event).

Sending emails with signatures via C#

I have a the following setup to send emails from my c# Application :
SmtpClient (under System.Net.Mail namespace) to do the actual sending once everything is in place and set the 'IsBodyHtml' property of the Message object to True
Using the dll from Sautinsoft I convert a simple rtf file which contains the formatting of the email and convert it to a HTML string which I then use as the body of the mail.
It works great just as it is and I have sent a few test emails to myself and all the appropriate formatting is retained. However i am having a problem with images - The dll converts images to a img tag and uses the base 64 format of the image as the data source, this works fine if you view it as a html page, but sending it as the body of you email produces problems. Email clients such as Yahoo don't mind embedded images but Gmail does not play nice with this methodology. The only image that should appear in the emails I'm sending is the signature image located at the bottom of each email. Using signatures in the native Gmail client in your browser poses no problems since the image has a link to a actual file on a server somewhere, but sending emails with signatures via a C# Application seems to be a different story. Any suggestions?
Thank you for your time.
You may consider automating Outlook from a C# application. See How to automate Outlook and Word by using Visual C# .NET to create a pre-populated e-mail message that can be edited. Also you can find a sample code - C# app automates Outlook (CSAutomateOutlook).
If you are talking about RTF2HTML, you may add any images in a separate folder (no include in body of HTML):
string inpFile = #"..\..\..\..\example.docx";
string outFile = Path.GetFullPath(#"Result2.html");
string imgDir = Path.GetDirectoryName(outFile);
RtfToHtml r = new RtfToHtml();
// Set images directory
HtmlFixedSaveOptions opt = new HtmlFixedSaveOptions()
{
ImagesDirectoryPath = Path.Combine(imgDir, "Result_images"),
ImagesDirectorySrcPath = "Result_images",
// Change to store images as physical files on local drive.
EmbedImages = false
};
try
{
r.Convert(inpFile, outFile, opt);
}
catch (Exception ex)
{
Console.WriteLine($"Conversion failed! {ex.Message}");
}
As the result you will see HTML file with linked images in folder.

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?

Categories

Resources