Exemplo n.º 1
0
 //<Snippet1>
 private void AddAppointment()
 {
     try
     {
         Outlook.AppointmentItem newAppointment =
             (Outlook.AppointmentItem)
             this.Application.CreateItem(Outlook.OlItemType.olAppointmentItem);
         newAppointment.Start    = DateTime.Now.AddHours(2);
         newAppointment.End      = DateTime.Now.AddHours(3);
         newAppointment.Location = "ConferenceRoom #2345";
         newAppointment.Body     =
             "We will discuss progress on the group project.";
         newAppointment.AllDayEvent = false;
         newAppointment.Subject     = "Group Project";
         newAppointment.Recipients.Add("Roger Harui");
         Outlook.Recipients sentTo     = newAppointment.Recipients;
         Outlook.Recipient  sentInvite = null;
         sentInvite      = sentTo.Add("Holly Holt");
         sentInvite.Type = (int)Outlook.OlMeetingRecipientType
                           .olRequired;
         sentInvite      = sentTo.Add("David Junca ");
         sentInvite.Type = (int)Outlook.OlMeetingRecipientType
                           .olOptional;
         sentTo.ResolveAll();
         newAppointment.Save();
         newAppointment.Display(true);
     }
     catch (Exception ex)
     {
         MessageBox.Show("The following error occurred: " + ex.Message);
     }
 }
Exemplo n.º 2
0
        public void Button_Click(Office.IRibbonControl control)
        {
            try
            {
                Outlook.Recipient  recipient  = null;
                Outlook.Recipients recipients = null;

                Outlook.Application application = new Outlook.Application();
                Outlook.Explorer    explorer    = application.ActiveExplorer();
                Outlook.Inspector   inspector   = application.ActiveInspector();
                inspector.Activate();
                Outlook._MailItem mailItem = inspector.CurrentItem;

                //Outlook.Application outlookApplication = new Outlook.Application();
                //Outlook.MailItem mail = (Outlook.MailItem)outlookApplication.ActiveInspector().CurrentItem;

                if (mailItem != null)
                {
                    recipients = mailItem.Recipients;
                    recipients.ResolveAll();
                    String       StrR            = "";
                    const string PR_SMTP_ADDRESS =
                        "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
                    foreach (Outlook.Recipient recip in recipients)
                    {
                        Outlook.PropertyAccessor pa = recip.PropertyAccessor;
                        string smtpAddress          =
                            pa.GetProperty(PR_SMTP_ADDRESS).ToString();

                        string[] strsplit = smtpAddress.Split('@');
                        if (!StrR.Contains(strsplit[1]))
                        {
                            StrR += strsplit[1] + Environment.NewLine;
                        }
                    }
                    if (StrR != string.Empty)
                    {
                        MyMessageBox ObjMyMessageBox = new MyMessageBox();
                        ObjMyMessageBox.ShowBox(StrR);
                    }



                    //recipient.Resolve();
                    //                DialogResult result = MessageBox.Show("Are you sure you want to send emails to the following domains " + StrR, "Varify Domains ",
                    //MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    //                if (result == DialogResult.Yes)
                    //                {
                    //                    //code for Yes
                    //                }
                    //                else
                    //                {

                    //                }
                }
            }
            catch (Exception ex)
            {
            }
        }
Exemplo n.º 3
0
        //gavdcodeend 08

        //gavdcodebegin 09
        private void btnCreateAppointment_Click(object sender, RibbonControlEventArgs e)
        {
            Outlook.Application myApplication = Globals.ThisAddIn.Application;

            Outlook.AppointmentItem newAppointment = (Outlook.AppointmentItem)
                                                     myApplication.CreateItem(Outlook.OlItemType.olAppointmentItem);

            newAppointment.Start       = DateTime.Now.AddHours(1);
            newAppointment.End         = DateTime.Now.AddHours(2);
            newAppointment.Location    = "An Empty Room";
            newAppointment.Body        = "This is a test appointment";
            newAppointment.AllDayEvent = false;
            newAppointment.Subject     = "My test";
            newAppointment.Recipients.Add("Somebody, He Is");

            Outlook.Recipients sentAppointmentTo = newAppointment.Recipients;
            Outlook.Recipient  sentInvite        = null;
            sentInvite      = sentAppointmentTo.Add("b, bbb b");
            sentInvite.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
            sentInvite      = sentAppointmentTo.Add("c, ccc c");
            sentInvite.Type = (int)Outlook.OlMeetingRecipientType.olOptional;
            sentAppointmentTo.ResolveAll();
            newAppointment.Save();
            newAppointment.Display(true);
        }
Exemplo n.º 4
0
        /// <summary>
        ///Refere to where the code was taken from
        ///https://www.add-in-express.com/creating-addins-blog/2011/09/08/outlook-fill-recipients-programmatically/
        /// </summary>
        /// <param name="mail"></param>
        /// <returns></returns>
        private bool AddRecipients(Outlook.MailItem mail)
        {
            bool retValue = false;

            Outlook.Recipients recipients   = null;
            Outlook.Recipient  recipientTo  = null;
            Outlook.Recipient  recipientCC  = null;
            Outlook.Recipient  recipientBCC = null;
            try
            {
                recipients = mail.Recipients;
                // first, we remove all the recipients of the e-mail
                while (recipients.Count != 0)
                {
                    recipients.Remove(1);
                }
                // now we add new recipietns to the e-mail
                foreach (IEmailAddress address in this.ToAddesses)
                {
                    recipientTo      = recipients.Add((((EmailAddress)address).Address));
                    recipientTo.Type = (int)Outlook.OlMailRecipientType.olTo;
                }
                foreach (IEmailAddress address in this.CcAddresses)
                {
                    recipientTo      = recipients.Add((((EmailAddress)address).Address));
                    recipientTo.Type = (int)Outlook.OlMailRecipientType.olCC;
                }
                foreach (IEmailAddress address in this.BccAddress)
                {
                    recipientTo      = recipients.Add((((EmailAddress)address).Address));
                    recipientTo.Type = (int)Outlook.OlMailRecipientType.olBCC;
                }
                retValue = recipients.ResolveAll();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
            finally
            {
                if (recipientBCC != null)
                {
                    Marshal.ReleaseComObject(recipientBCC);
                }
                if (recipientCC != null)
                {
                    Marshal.ReleaseComObject(recipientCC);
                }
                if (recipientTo != null)
                {
                    Marshal.ReleaseComObject(recipientTo);
                }
                if (recipients != null)
                {
                    Marshal.ReleaseComObject(recipients);
                }
            }
            return(retValue);
        }
Exemplo n.º 5
0
        private void AddAppointment(string subject, string body, DateTime startdatum, DateTime sluttatum)
        {
            try
            {
                Microsoft.Office.Interop.Outlook.AppointmentItem newAppointment =
                    (Microsoft.Office.Interop.Outlook.AppointmentItem)
                    _application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
                newAppointment.Start       = startdatum;
                newAppointment.End         = sluttatum;
                newAppointment.AllDayEvent = false;
                newAppointment.BusyStatus  = Microsoft.Office.Interop.Outlook.OlBusyStatus.olTentative;
                newAppointment.Location    = "Platsangivelse...";

                // Bifoga ett dokument om så önskas
                OpenFileDialog attachment = new OpenFileDialog();
                attachment.Title = appointheader.Text;
                attachment.ShowDialog();
                if (attachment.FileName.Length > 0)
                {
                    newAppointment.Attachments.Add(attachment.FileName,
                                                   Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue,
                                                   1,
                                                   attachment.FileName);
                }

                Microsoft.Office.Interop.Outlook.Recipients sentTo     = newAppointment.Recipients;
                Microsoft.Office.Interop.Outlook.Recipient  sentInvite = null;
                sentInvite      = sentTo.Add("Holly Holt");
                sentInvite.Type = (int)Microsoft.Office.Interop.Outlook.OlMeetingRecipientType
                                  .olRequired;
                sentInvite      = sentTo.Add("David Junca ");
                sentInvite.Type = (int)Microsoft.Office.Interop.Outlook.OlMeetingRecipientType
                                  .olOptional;
                sentTo.ResolveAll();

                newAppointment.Subject = subject;
                newAppointment.Body    = body;
                newAppointment.Save();
                // newAppointment.Display(true);
                newAppointment.Close(OlInspectorClose.olSave); // Detta gör att man inte behöver stänga dialogen manuellt
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("The following error occurred: " + ex.Message);
            }
        }
Exemplo n.º 6
0
        public static bool SendMail(string htmlBody, string toEmails, string subject, string ccEmails)
        {
            MsOutlook.Application outlookApp = new MsOutlook.Application();
            if (outlookApp == null)
            {
                return(false);
            }

            // create a new mail item.
            MsOutlook.MailItem   mail         = (MsOutlook.MailItem)outlookApp.CreateItem(MsOutlook.OlItemType.olMailItem);
            MsOutlook.Recipients recipients   = mail.Recipients as MsOutlook.Recipients;
            MsOutlook.Recipient  recipientTo  = null;
            MsOutlook.Recipient  recipientCC  = null;
            MsOutlook.Recipient  recipientBCC = null;
            // set html body.
            // add the body of the email
            mail.HTMLBody   = htmlBody;
            mail.BodyFormat = MsOutlook.OlBodyFormat.olFormatHTML;
            mail.Subject    = subject;
            mail.Importance = MsOutlook.OlImportance.olImportanceNormal;
            var to = toEmails.Split(';');

            foreach (string tempTO in to)
            {
                recipientTo      = recipients.Add(tempTO);
                recipientTo.Type = (int)MsOutlook.OlMailRecipientType.olTo;
            }
            var cc = ccEmails.Split(';');

            foreach (string tempCC in cc)
            {
                recipientCC      = recipients.Add(tempCC);
                recipientCC.Type = (int)MsOutlook.OlMailRecipientType.olCC;
            }
            recipients.ResolveAll();
            mail.Send();

            mail       = null;
            outlookApp = null;
            recipients = null;
            return(true);
        }
Exemplo n.º 7
0
        //http://msdn.microsoft.com/en-us/library/office/ff184647.aspx
        private void GetSMTPAddressForRecipients(Outlook.MailItem mail)
        {
            const string PR_SMTP_ADDRESS =
                "http://schemas.microsoft.com/mapi/proptag/0x39FE001E";

            Outlook.Recipients recips = mail.Recipients;
            recips.ResolveAll();
            foreach (Outlook.Recipient recip in recips)
            {
                try {
                    Outlook.PropertyAccessor pa = recip.PropertyAccessor;
                    string smtpAddress          =
                        pa.GetProperty(PR_SMTP_ADDRESS).ToString();
                    //if (!emails.Keys.Contains(smtpAddress)) emails.Add(smtpAddress, recip.Name );
                    SaveRecipient(smtpAddress, recip.Name);
                    SetSenderSubject(smtpAddress, mail.Subject);
                }
                catch (Exception ex)
                {
                }
            }
        }
            public Boolean AddReceipents(Outlook.MailItem mailItem, String ToAddress, String CC, String BCC)
            {
                Outlook.Recipients receipents    = null;
                Outlook.Recipient  receipentsTo  = null;
                Outlook.Recipient  receipentsCC  = null;
                Outlook.Recipient  receipentsBCC = null;

                Boolean SuccessFlag = false;

                try
                {
                    //This is for Adding Recepients in Mail.
                    receipents = mailItem.Recipients;

                    //This is for adding To Address
                    if (!string.IsNullOrWhiteSpace(ToAddress))
                    {
                        string[] arrToAddress = ToAddress.Split(new char[] { ',', ';' });
                        foreach (string Addr in arrToAddress)
                        {
                            try
                            {
                                if (!string.IsNullOrWhiteSpace(Addr) && Addr.IndexOf('@') != -1)
                                {
                                    receipentsTo      = receipents.Add(Addr.Trim());
                                    receipentsTo.Type = (int)Outlook.OlMailRecipientType.olTo;
                                }
                                else
                                {
                                    throw new Exception("\"To\" Address is not correct : " + Addr);
                                }
                            }
                            catch (Exception e)
                            {
                                throw new System.Exception("Error in \"To\" Address");
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Value for the required variable \"TO\" was not supplied");
                    }

                    //This is for adding CC Address
                    if (!string.IsNullOrWhiteSpace(CC))
                    {
                        string[] arrCC = CC.Split(new char[] { ',', ';' });
                        foreach (string Addr in arrCC)
                        {
                            try
                            {
                                if (!string.IsNullOrWhiteSpace(Addr) && Addr.IndexOf('@') != -1)
                                {
                                    receipentsCC      = receipents.Add(Addr.Trim());
                                    receipentsCC.Type = (int)Outlook.OlMailRecipientType.olCC;
                                }
                                else
                                {
                                    throw new Exception("\"CC\" Address is not correct : " + Addr);
                                }
                            }
                            catch (Exception e)
                            {
                                throw new System.Exception("Error in \"CC\" Address");
                            }
                        }
                    }


                    //This is for adding BCC Address
                    if (!string.IsNullOrWhiteSpace(BCC))
                    {
                        string[] arrBCC = BCC.Split(new char[] { ',', ';' });
                        foreach (string Addr in arrBCC)
                        {
                            if (!string.IsNullOrWhiteSpace(Addr) && Addr.IndexOf('@') != -1)
                            {
                                receipentsBCC      = receipents.Add(Addr.Trim());
                                receipentsBCC.Type = (int)Outlook.OlMailRecipientType.olBCC;
                            }
                            else
                            {
                                throw new Exception("\"CC\" Address is not correct " + ToAddress);
                            }
                        }
                    }

                    //Resolving all address
                    SuccessFlag = receipents.ResolveAll();
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    receipents    = null;
                    receipentsTo  = null;
                    receipentsCC  = null;
                    receipentsBCC = null;
                }
                return(SuccessFlag);
            }