At a high level, I'm trying to generate an outlook email item server side, and then return it to the client so that it opens in their local Outlook allowing them to make any changes they want(this as apposed to sending the email through SMTP).
This is really my first time ever working with file streams, and I'm not exactly sure how to proceed. I have the pretty basic start of what I'm trying to do, which is initially just get Outlook to open after this controller action returns the mail item. This code is what is called in the Controller to create the mail item. I don't have any of the logic for adding the various email addresses, body, subject etc as that isn't really relevant. Also this code gets called by the controller action, just placing it here in case I'm also doing something wrong creating the mail item.
public MailItem CreateEmail(int id)
{
Application app = new Application();
MailItem email = (MailItem)app.CreateItem(OlItemType.olMailItem);
email.Recipients.Add("example@example.com");
return email;
}
Here is the controller action I would like to return this MailItem to the client. (This is what get's called via AJAX)
public ActionResult GenerateEmail(int id)
{
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter format = new BinaryFormatter();
format.Serialize(ms, logic.CreateEmail(id));
return File(ms, "message/rfc822");
}
}
The code breaks at format.Serialize giving me the error that my _COM object isn't able to be serialized. Is there a way to do what I am trying to do, or should I look for other methods to achieve this goal instead?