0

I have a web application with some functionality to allow a user to send an email using Microsoft.Office.Interop.Outlook.Application. I want to display the email to allow the user to add any text they may want before sending. It works as expected in localhost however in production it fails to display the email. My code is as follows:

OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);

mailItem.To = address;
mailItem.Subject = subject;
mailItem.HTMLBody = body;

mailItem.Importance = OlImportance.olImportanceNormal;

mailItem.Display(false);

Outlook opens just fine when I use a Response.Redirect instead of the above.

Response.Redirect("mailto:" + email + "?subject=" + subject + "&body=" + body);

Any ideas/suggestions?

maccettura
  • 10,514
  • 3
  • 28
  • 35
Quent
  • 79
  • 11
  • 1
    You absolutely cannot do this, whatever code you run is *running on the server*, so even if this did work it would show the mail on the server, not on the client. – DavidG May 11 '18 at 16:38
  • what keeps you from using the second approach, which will use the client side mail application? (except that you should add logic to urlescape `email`, `subject` and `body`). [consider also](https://stackoverflow.com/a/32675594/1132334) JavaScript `window.open('mailto:........');` – Cee McSharpface May 11 '18 at 16:40
  • Well for one, I'd really like to get it to work from a personal standpoint, but more importantly I'd like to be able to take advantage of the ability to automatically attach files, which the second option does not allow. – Quent May 11 '18 at 16:51
  • Use `MailMessage` and `SmtpMail` objects instead. If you do that, you'll want an Exchange accout for your application, but it's a cleaner way to do things in a web app. – anu start May 11 '18 at 17:17
  • @anustart You don't necessarily need an Exchange account for the application. Depends on how the server is configured. – mason May 11 '18 at 17:54
  • @mason You're saying the user's credential can be passed to the web server for the sake of e-mail? – anu start May 11 '18 at 19:15
  • @anustart No, I'm not. It depends on the use case. For example, you send an email from a non-existent account like `noreply@mycompany.com` and set the reply address up and make it clear in the body of the email it was sent on behalf of someone else. It really depends on the scenario. – mason May 11 '18 at 21:01

1 Answers1

0

Create an EML (MIME) message with attachments etc. on the server and let the user download it. Outlook on the client side will be happy to open it. Don't forget to add the X-Unsent:1 MIME header to make sure the user can actually send it.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78