locked
Outlook NewMailItem with ASP.Net C# RRS feed

  • Question

  • User279938560 posted

    Hi,

    I just want to use Outlook in asp.net c# web application.

    And Now I use like this...


    try
            {            
                // Create the Outlook application.
                Outlook.Application  oApp = new Outlook.Application();
    
                // Get the NameSpace and Logon information.
                Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
    
                // Log on by using a dialog box to choose the profile.
                oNS.Logon(Missing.Value, Missing.Value, true, true); 
    
                // Alternate logon method that uses a specific profile.
                // TODO: If you use this logon method, 
                //  change the profile name to an appropriate value.
                //oNS.Logon("YourValidProfile", Missing.Value, false, true); 
    			
                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    
                // Set the subject.
                oMsg.Subject = "Testing";
    
                // Set HTMLBody.
                String sHtml;
                sHtml = "<HTML>\n" + 
                   "<HEAD>\n" +
                   "<TITLE>Sample GIF</TITLE>\n" +
                   "</HEAD>\n" +
                   "<BODY><P>\n" + 
                   "<h1><Font Color=Green>Testing</Font></h1></P>\n" +
                   "<P>Testing Testing Testing Testing</P>\n" +
                   "<P>Testing Testing Testing Testing</P>\n" +
                   "</BODY>\n" + 
                   "</HTML>";
                oMsg.HTMLBody = sHtml;
    
                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                // TODO: Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("user@outlook.com.sg");
                oRecip.Resolve();
    
                // Send.
                oMsg.Send();
    
                // Log off.
                oNS.Logoff();
    
                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oNS = null;
                oApp = null;
             }
    
             // Simple error handling.
             catch (Exception e)
             {
                Console.WriteLine("{0} Exception caught.", e);
             }  


    this code is work when I use debugging mode.


    But actually I want pop up New Mail Item from Outlook without receipents address and with body text.

    So, what should I do? Help me please... *** urgent ***

    Wednesday, May 5, 2010 1:59 AM

Answers

  • User2130758966 posted

    Interacting with Outlook or any office application in this way is only possible on the computer the program is hosted on.

    So when you are debugging the server is also the client and it looks like it worked.

    When you run it on the server it tries to open a new outlook window on the server and fails.

    This is a limitation of web development, there are just some scenarios you cannot handle.

    Your solution is to either:

    • Use the mailto: protocol parameters to define an address and simple text
    • Send the email through the website via an smtp server using .net code


    mailto protocol

    You can add extra parameters onto the end of a mailto: address to set defaults with a new email being opened. Please note this is hit and miss - if the user users hotmail or something their links might not even work. Even if they use a desktop app it is still a poorly supported protocol.

    Sample link:

    mailto:somebody@example.com?subject=hello&body=this+is+the+body

    send email with .net

    This site will teach you everything you need to know:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 5, 2010 4:11 AM

All replies

  • User2130758966 posted

    Interacting with Outlook or any office application in this way is only possible on the computer the program is hosted on.

    So when you are debugging the server is also the client and it looks like it worked.

    When you run it on the server it tries to open a new outlook window on the server and fails.

    This is a limitation of web development, there are just some scenarios you cannot handle.

    Your solution is to either:

    • Use the mailto: protocol parameters to define an address and simple text
    • Send the email through the website via an smtp server using .net code


    mailto protocol

    You can add extra parameters onto the end of a mailto: address to set defaults with a new email being opened. Please note this is hit and miss - if the user users hotmail or something their links might not even work. Even if they use a desktop app it is still a poorly supported protocol.

    Sample link:

    mailto:somebody@example.com?subject=hello&body=this+is+the+body

    send email with .net

    This site will teach you everything you need to know:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 5, 2010 4:11 AM
  • User279938560 posted

    Hi rtpHarry,


    Thanks for your help..

    Now I have already solved like this

    MailMessage mail = new MailMessage();
    
            mail.From = new MailAddress("sample@sample.com", "Sample");
            mail.To.Add(new MailAddress("sample@sample.com", "Sample"));
            mail.To.Add(new MailAddress("sample@sample.com", "Sample"));
            mail.To.Add(new MailAddress("sample@sample.com", "Sample"));
            mail.CC.Add(new MailAddress("sample@sample.com", "Sample"));
    
            mail.Subject = "Testing From POV2";
    
            String sHtml;
                sHtml = "<HTML>\n" + 
                   "<HEAD>\n" +
                   "<TITLE>Testing _POV2_</TITLE>\n" +
                   "</HEAD>\n" +
                   "<BODY><P>\n" + 
                   "<h1><Font Color=Red>Testing For POV2</Font></h1>\n" +
                   "<Font Color=Purple>Testing For POV2</Font></P>\n" +
                   "</BODY>\n" + 
                   "</HTML>";
    
                mail.Body = sHtml;
    
                SmtpClient smtp = new SmtpClient("htcas.sample.com");
                smtp.Send(mail);


    But I want to choose the email address from database so I think it will be okay with pop up window. How do you think?

    Suggest for me and I have no idea how to pass the data from pop up window to parent window.

    If you have any idea... let me know... thx.

    Wednesday, May 5, 2010 6:13 AM
  • User-276214393 posted

    Hi,

    I just want to use Outlook in asp.net c# web application.

    And Now I use like this...



    1. try  
    2.         {              
    3.             // Create the Outlook application.  
    4.             Outlook.Application  oApp = new Outlook.Application();  
    5.   
    6.             // Get the NameSpace and Logon information.  
    7.             Outlook.NameSpace oNS = oApp.GetNamespace("mapi");  
    8.   
    9.             // Log on by using a dialog box to choose the profile.  
    10.             oNS.Logon(Missing.Value, Missing.Value, truetrue);   
    11.   
    12.             // Alternate logon method that uses a specific profile.  
    13.             // TODO: If you use this logon method,   
    14.             //  change the profile name to an appropriate value.  
    15.             //oNS.Logon("YourValidProfile", Missing.Value, false, true);   
    16.               
    17.             // Create a new mail item.  
    18.             Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);  
    19.   
    20.             // Set the subject.  
    21.             oMsg.Subject = "Testing";  
    22.   
    23.             // Set HTMLBody.  
    24.             String sHtml;  
    25.             sHtml = "<HTML>\n" +   
    26.                "<HEAD>\n" +  
    27.                "<TITLE>Sample GIF</TITLE>\n" +  
    28.                "</HEAD>\n" +  
    29.                "<BODY><P>\n" +   
    30.                "<h1><Font Color=Green>Testing</Font></h1></P>\n" +  
    31.                "<P>Testing Testing Testing Testing</P>\n" +  
    32.                "<P>Testing Testing Testing Testing</P>\n" +  
    33.                "</BODY>\n" +   
    34.                "</HTML>";  
    35.             oMsg.HTMLBody = sHtml;  
    36.   
    37.             // Add a recipient.  
    38.             Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;  
    39.             // TODO: Change the recipient in the next line if necessary.  
    40.             Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("user@outlook.com.sg");  
    41.             oRecip.Resolve();  
    42.   
    43.             // Send.  
    44.             oMsg.Send();  
    45.   
    46.             // Log off.  
    47.             oNS.Logoff();  
    48.   
    49.             // Clean up.  
    50.             oRecip = null;  
    51.             oRecips = null;  
    52.             oMsg = null;  
    53.             oNS = null;  
    54.             oApp = null;  
    55.          }  
    56.   
    57.          // Simple error handling.  
    58.          catch (Exception e)  
    59.          {  
    60.             Console.WriteLine("{0} Exception caught.", e);  
    61.          }    
    try
            {            
                // Create the Outlook application.
                Outlook.Application  oApp = new Outlook.Application();
    
                // Get the NameSpace and Logon information.
                Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
    
                // Log on by using a dialog box to choose the profile.
                oNS.Logon(Missing.Value, Missing.Value, true, true); 
    
                // Alternate logon method that uses a specific profile.
                // TODO: If you use this logon method, 
                //  change the profile name to an appropriate value.
                //oNS.Logon("YourValidProfile", Missing.Value, false, true); 
    			
                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    
                // Set the subject.
                oMsg.Subject = "Testing";
    
                // Set HTMLBody.
                String sHtml;
                sHtml = "<HTML>\n" + 
                   "<HEAD>\n" +
                   "<TITLE>Sample GIF</TITLE>\n" +
                   "</HEAD>\n" +
                   "<BODY><P>\n" + 
                   "<h1><Font Color=Green>Testing</Font></h1></P>\n" +
                   "<P>Testing Testing Testing Testing</P>\n" +
                   "<P>Testing Testing Testing Testing</P>\n" +
                   "</BODY>\n" + 
                   "</HTML>";
                oMsg.HTMLBody = sHtml;
    
                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                // TODO: Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("user@outlook.com.sg");
                oRecip.Resolve();
    
                // Send.
                oMsg.Send();
    
                // Log off.
                oNS.Logoff();
    
                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oNS = null;
                oApp = null;
             }
    
             // Simple error handling.
             catch (Exception e)
             {
                Console.WriteLine("{0} Exception caught.", e);
             }  




    this code is work when I use debugging mode.


    But actually I want pop up New Mail Item from Outlook without receipents address and with body text.

    So, what should I do? Help me please... *** urgent ***

    You code gave me some good start Thanks !!!!

    Thursday, May 20, 2010 3:15 AM
  • User279938560 posted

    Ur welcome! :)

    Sunday, May 23, 2010 9:32 PM