0

I tried to open outlook window with body to send a message. When I works with my local it's opening perfectly. But when I work with IIS the outlook window is not opening.

public static void SendMailInOutlook(Page CurrentPage, Type ScriptType,
    string Subject, string Recipients, Dictionary<string, string> emailInputs, 
    string emailTemplatePath)
{
    static Microsoft.Office.Interop.Outlook.Application outlookApp;
    static Microsoft.Office.Interop.Outlook._MailItem mailItem;
    outlookApp = new Microsoft.Office.Interop.Outlook.Application();
    mailItem = (Microsoft.Office.Interop.Outlook._MailItem)outlookApp.CreateItem(
        Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
    mailItem.Subject = Subject;
    mailItem.To = Recipients;
    mailItem.HTMLBody = SendMail.GetEmailContent(emailInputs, emailTemplatePath);
    Thread newThread = new Thread(SendEmailToOutLook);
    newThread.Start();                    
} 

static void SendEmailToOutLook()
{
    string sMsg = "Email sent successfully.";
    try
    {
        mailItem.Display(true);
    }
    catch (Exception ex)
    {
        sMsg = ex.Message;
    }
}
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
sarathkumar
  • 428
  • 3
  • 13

1 Answers1

0

And it won't. Your codebehind is executed on the web server, NOT on the client machine. So when you develop locally, and run the web app locally, that code is executed on the same machine, hence "working". But once you deploy, the "Outlook code" is executed on a different machine than the one the browser is on. It cannot work.

The most common approach to this requirement is a "mailto link", the likes of <a href='mailto:a@b.com'...

Alexander
  • 2,457
  • 1
  • 14
  • 17
  • I need to pass html form in this... How do i pass html form in Href link?? – sarathkumar Sep 24 '14 at 08:01
  • What does a HTML form do in an e-mail? – Alexander Sep 24 '14 at 10:36
  • Like I need to Pass t html forms cos i need to load some defaults design for the body.. Like Dashing Border color & Padding for the words.. So how is this possible.. previously i tried this one only but When i pass Html forms the script was not working.... – sarathkumar Sep 25 '14 at 06:31
  • Okay, it's not a HTML 'form' you're after. Check this out: http://stackoverflow.com/questions/886728/generating-html-email-body-in-c-sharp – Alexander Sep 25 '14 at 06:47
  • Actually i need to open new outlook window.. But nothing is happening when t code runs... Even No Errors..... – sarathkumar Oct 08 '14 at 06:01
  • Well then just reread my answer. You cannot open "Outlook" like that via codebehind. What is your default e-mail client? – Alexander Oct 08 '14 at 07:11