Outlook Email won't display in web application - c#

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?

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.

Related

Sending E-Mail through app causes it to crash

I am using ShowComposeNewEmailAsync().
EmailRecipient sendTo = new EmailRecipient()
{
Address = "example#example.com"
};
//generate mail object
EmailMessage mail = new EmailMessage();
mail.Subject = subject.Text;
mail.Body = body.Text;
//add recipients to the mail object
mail.To.Add(sendTo);
//open the share contract with Mail only:
await EmailManager.ShowComposeNewEmailAsync(mail);
The app crashes in phone when I have more than 1 app(I had multiple Google Mail Account IDs logged in in email+accounts i.e. Google Mail,Google Mail 2,Google Mail 3) to do this or have no email set in email+accounts in Settings.
When I call this function and have more than 1 account, the app selector appears for 2 or 3 seconds (showing Google Mail,Google Mail 2,Google Mail 3 in the list) and then disappears before I can select anything closing the app along with it.
If no accounts are available then a message appears saying no apps are installed to share for 1-2 seconds and then it disappears closing the app along with it.
How to prevent this from happening?
Can you try with simple values? This works fine for me, and I have several e-mail accounts on my phone:
var msg = new EmailMessage();
msg.Subject = "Hello this is a test";
msg.Body = "Testing 123";
msg.To.Add(new EmailRecipient("address#somewhere.com"));
await EmailManager.ShowComposeNewEmailAsync(msg);
Do you have your code wrapped in a try/catch in case you are getting an exception that is being lost during the async call?

Generate Oulook Email From Server

I have an ASP.NET site that needs to be able to dynamically generate an email that will get sent back to the users local machine to then be sent via Outlook. The code below does just that but it uses the Outlook Interop to create the message and I was a bit hesitent to use Interop on a Web App. I looked into OpenXML but couldnt seem to find much on Outlook.
// Creates a new Outlook Application Instance
Microsoft.Office.Interop.Outlook.Application objOutlook = new Microsoft.Office.Interop.Outlook.Application();
// Creating a new Outlook Message from the Outlook Application Instance
Microsoft.Office.Interop.Outlook.MailItem mic = (Microsoft.Office.Interop.Outlook.MailItem)(objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem));
mic.To = "to#email.com";
mic.CC = "cc#email.com";
mic.Subject = "Test Subject";
mic.HTMLBody = "Test Message Body";
string strNewEmailPath = strEmailPath + "\\EmailMessages\\" + strUser + "_Message_PEI.msg";
mic.SaveAs(strNewEmailPath, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);
HttpContext.Current.Response.ContentType = "application/vnd.ms-outlook";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=Message.msg");
HttpContext.Current.Response.TransmitFile(strNewEmailPath);
HttpContext.Current.Response.End();
Can anyone help with perhaps a better suggestion for automating an Outlook message using ASP.NET?
Update:
I did find the Javascript code which seems do have similar functionality.
var theApp //Reference to Outlook.Application
var theMailItem //Outlook.mailItem
//Attach Files to the email, Construct the Email including
//To(address),subject,body
var subject = sub
var msg = body
//Create a object of Outlook.Application
try
{
var theApp = new ActiveXObject("Outlook.Application")
var theMailItem = theApp.CreateItem(0) // value 0 = MailItem
//Bind the variables with the email
theMailItem.to = to
theMailItem.Subject = (subject);
theMailItem.Body = (msg);
//Show the mail before sending for review purpose
//You can directly use the theMailItem.send() function
//if you do not want to show the message.
theMailItem.display()
}
catch(err)
{
alert("Error");
}
I have had a similar need before. I shelved the idea because of the complexity of generating .msg files (though I think there's some commercial libraries that can do it). One alternative you may consider is installing an Outlook add-in on the user's machine. Poll the web server (or this would be a great case for SignalR so the server could push data) and have the web server send the details of the email to the user's machine. Then the add-in could generate the email based on the details it received from the server. This would avoid running Interop on the server (which is a bad idea). However, you'll now have the complexity of deploying an Outlook Add-In, but if this is a corporate environment it shouldn't be too difficult.
If you didn't want to do it as an add-in, you could still do it by just writing a service application that runs on the user's machine and uses Interop, but that still has all the complexities of the add-in technique.
Alternatively, if your email is really simple, you could just use a mailto URI. But I find them very limiting, since it's difficult or impossible to send an HTML message that way.
And lastly, you could have the server just send the email on the user's behalf, not involving code running on the user's machine at all.
I did find the Javascript code which seems do have similar functionality. I was hoping someone might have an OpenXML solution but this JS solution can work and is better than Outlook Interop
var theApp //Reference to Outlook.Application
var theMailItem //Outlook.mailItem
//Attach Files to the email, Construct the Email including
//To(address),subject,body
var subject = sub
var msg = body
//Create a object of Outlook.Application
try
{
var theApp = new ActiveXObject("Outlook.Application")
var theMailItem = theApp.CreateItem(0) // value 0 = MailItem
//Bind the variables with the email
theMailItem.to = to
theMailItem.Subject = (subject);
theMailItem.Body = (msg);
//Show the mail before sending for review purpose
//You can directly use the theMailItem.send() function
//if you do not want to show the message.
theMailItem.display()
}
catch(err)
{
alert("Error");
}

"Send e-mail to" with Microsoft.Office.Interop.Outlook

in my development machine I developed a method that work without problem. Its a simple button and when I clicked it open an outlook email with a email adress and some content But when I sent the code to the production enviroment... all users are getting an error on page_load of the page where the following method are set.
protected void btnSendEmail_Click(object sender, ImageClickEventArgs e)
{
//...
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._MailItem oMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
string name = someDataTable.Rows[0]["NAME"].ToString();
string url = HttpContext.Current.Request.Url.AbsoluteUri;
oMailItem.To = someEmail#Gmail.com;
oMailItem.Body = string.Format("hi {0}, \n \n [Insert the content mail here.] \n \n {1} ", name, url);
oMailItem.Subject = "Some Title";
oMailItem.Display(false);
}
The Microsoft Office Interop assemblies would open Outlook on the server (assuming Outlook is installed on the server), not on the client. Is that your intention?
The Microsoft Office Interop assemblies are not supported on the server side. See KB Article.
I suggest you switch to some other technology. If you're just trying to have the server send an email, try taking at look at the System.Net.Mail namespace. See How To Send An Email With C#.
If your intention is to launch an Outlook new email window on the client side, it's easy to accomplish that with a simple HTML anchor link. See Sending HTML in mailto anchor tag.

Gmail, Hotmail does not send images in mail content using imap or pop3

I am using HigLabo library to get inbox messages from Gmail (imap) or Hotmail (pop3).
My Code is similar to this for gmail;
ImapClient client = new ImapClient(ServerName);
client.UserName = UserName;
client.Password = Password;
client.Port = Port;
client.Ssl = Ssl;
MailMessage mailMessage = client.GetMessage(1);
Console.WriteLine(mailMessage.BodyText);
lets assume this message is a HTML mail coming from newegg. So BodyText property has whole content as html but img elements coming as [image: ] because gmail and hotmail does not send images to my application. to see images user has to go their real inbox and click on "show all images" (which is not the case for my client app)
I am wondering is it a solid rule from mail providers to not to send images coming from "untrusted source" or is there a workaround to get also images and show inbox mails to user properly?
I am not sure what's wrong with all other API's lastly I gave a shot to this library today;
limilabs mail.dll
and it is fetching mails exactly how it appears on browsers.

To open outlook new email window with content already filled

How can I implement email functionality, where:
When I click on an asp button, a new email window opens with all the content already added from code behind( including To address, From address, Subject and Body).
But the email should not be sent automatically. It requires user to click send button.
The purpose is, admin can modify the email content before sending to users.
Has anyone worked on similar functionality and can help or give me idea on how to implement it ?
Outlook.Application outlookApp = new Outlook.Application ();
Outlook._MailItem mailItem = (Outlook._MailItem)outlookApp.CreateItem ( Outlook.OlItemType.olMailItem );
mailItem.To = address;
// body, bcc etc...
mailItem.Display ( true );
I think you do not need ASP.NET for this. All you need is mailto with the right parameters.
Example:
<a href="mailto:someone#example.com?Subject=Hello%20again&body=This%20is%20body">
Send Mail</a>
This will send email to someone#example.com with subject "Hello" and body "This is body". You can also use CC parameter to add CC emails.
More info on this link
A related question that may help How do I properly encode a mailto link?

Categories