Sicos1977 / MsgKit

A .NET library to make MSG files without the need for Outlook
204 stars 56 forks source link

how to save the msg file as Stream. #50

Closed dpakgithub82 closed 5 years ago

dpakgithub82 commented 5 years ago

Can you provide some example to save as file stream insted of saving in the target location.

ChrisKoenig commented 4 years ago

I tried to do this today, and ran into errors. Would love to see how this would work, for example, inside an Azure Function, where the function returns the newly crafted MSG file. When I try to do it, the file that is created returns this error when opened:

We can't open 'c:\users\me\desktop\sample.msg'. It's possible the file is already open, or you don't have permissions to open it. To check your permissions, right-click the file folder, then click Properties

When I save the msg with a local file path, it works, so I must be doing something wrong with the MemoryStream, or something like that...

Sicos1977 commented 4 years ago

You do it just like this.

            using (var email = new Email(
                new Sender(SenderTextBox.Text, string.Empty),
                SubjectTextBox.Text, 
                DraftMessageCheckBox.Checked,
                ReadReceiptCheckBox.Checked))
            {
                email.Recipients.AddTo(ToTextBox.Text);
                email.Recipients.AddCc(CcTextBox.Text);
                email.Recipients.AddBcc(BccTextBox.Text);
                email.Subject = SubjectTextBox.Text;
                email.BodyText = TextBodyTextBox.Text;
                email.BodyHtml = HtmlBodyTextBox.Text;
                email.SentOn = SentOnDatePicker.Value.ToUniversalTime();

                switch (ImportanceComboBox.Text)
                {
                    case "Low":
                        email.Importance = MessageImportance.IMPORTANCE_LOW;
                        break;

                    case "High":
                        email.Importance = MessageImportance.IMPORTANCE_HIGH;
                        break;

                    default:
                        email.Importance = MessageImportance.IMPORTANCE_NORMAL;
                        break;
                }

                email.IconIndex = MessageIconIndex.UnsentMail;
                email.Attachments.Add("Images\\peterpan.jpg");
                email.Attachments.Add("Images\\tinkerbell.jpg", -1, true, "tinkerbell.jpg");
                using(var ms = new MemoryStream())
                    email.Save(ms);
            }
ChrisKoenig commented 4 years ago

That worked, thanks!

Sicos1977 commented 4 years ago

Your welcome