Click here to Skip to main content
14,695,406 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I create exe for sending the email parameter
to outlook(likeTo,Subject and path of the attachement file ),its working fine for outlook 2013 but when i use it for outlook 2010 then ,if outlook 2010 is open then it woking fine but when outlook 2010 is closed then at that time my exe is not working. I also mention my code in below
so, please check and if possible then give me the solution.
static void Main(string[] args)
        {
            string to, sub,  path;
            to = sub = path = string.Empty;

            Console.WriteLine("Enter to");
            to = Console.ReadLine();
            Console.WriteLine("Enter sub");
            sub = Console.ReadLine();
            
            Console.WriteLine("Enter path");
            path = Console.ReadLine();
            Console.WriteLine("Please Wait....");
            CreateMessageWithAttachment(path, to, sub);

        }
        public static void CreateMessageWithAttachment(string path, string to, string subject)
        {
            try
            {

                Outlook.Application oApp = new Outlook.Application();
                Outlook.MailItem email = (Outlook.MailItem)(oApp.CreateItem(Outlook.OlItemType.olMailItem));

                #region set email recipients
                {


                    email.Recipients.Add(to);
                }
                #endregion

                //email subject                 
                email.Subject = subject;

                #region set email Text
                {

                   // email.Body = body;
                }
                #endregion

                #region email attachment
                {
                    if (File.Exists(path))
                    {
                       // int iPosition = (int)email.Body.Length + 1;
                        int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
                        Outlook.Attachment oAttach = email.Attachments.Add(path, iAttachType);
                    }
                }
                #endregion

                email.Display();

            }
            catch (Exception e)
            {
            }

        }



Thanks
hasan
Updated 6-Dec-13 10:32am
v3
Comments
Sebastian T Xavier 6-Dec-13 6:45am
   
does it throw some exception? Why you have left the 'catch' unhandled?
   
It actually means "handled", with complete blocking of propagation of the exception. Really bad idea, in most cases.
—SA
Sebastian T Xavier 10-Dec-13 3:36am
   
Yes: sometimes, it may take days to figure out the exact problem.

1 solution

You don't need Outlook to send emails, see here : http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900