How to assign default From Account in eml files using c# for a MVC application

1.4k views Asked by At

In my current MVC project there is a requirement to open a new outlook mail item by clicking a button with pre populated body and subject.

I have used the following method to achieve that: 1. Create a MailItem with requried details like Subject, Mail body etc 2. Using SmtpDeliveryMethod as SpecifiedPickupDirectory and create an eml file 3. Force the user to download and open the file in outlook

So far so good and EML file is correctly opening in outlook but the user has to manually select his mail account from the "From dropdown".

My question is there any way to assign the detault From account in EML files in order to open in Outlook 2010

1

There are 1 answers

0
s2conway On

The way I got around it, and it is way "hacky" to say the least, is to open the saved .eml in C# and delete all the from/sender values before streaming it down to the browser.

I have an aspx page that accepts a bunch of querystring parameters that allow you to customize the .eml output. You can't save the .eml file without a "sender" or "from" email address, so you need to save that out first - I use a dummy email account. You can use the SMTP pickup method or the MailMessage extension class to save the .eml file. Then I open the saved file, look for my placeholder email address (in the "X-Sender" header and "From" values), replace that with an empty string, then send the altered stream to the browser. The user never gets the "from" dropdown, it just uses the default account. Works like a charm.