Exemplo n.º 1
0
        private bool SendEmail()
        {
            OutlookApplication outlookApplication = new OutlookApplication();

            try
            {
                outlookApplication.SendEmail(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem,
                                             Utils.GetConfig(AppConstants.EMAIL_TO, ""),
                                             Utils.GetConfig(AppConstants.EMAIL_CC, ""),
                                             Utils.GetConfig(AppConstants.EMAIL_SUBJECT, "") + $" @ ~{DateTime.Now.ToString("yyyy / MM / dd")}",
                                             webBrowserShow.DocumentText,
                                             Microsoft.Office.Interop.Outlook.OlImportance.olImportanceNormal);
            }
            catch (Exception e)
            {
                _Log.Error("Send email error:" + e.Message);
                return(false);
            }
            finally
            {
                if (outlookApplication.IsNew)
                {
                    outlookApplication.Dispose();
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates the instance.
        /// </summary>
        /// <param name="oApp">The o app.</param>
        /// <returns></returns>
        public static SyncApplication CreateInstance(OutlookApplication outlookApplication)
        {
            SyncApplication retVal = _instance;

            if (retVal == null)
            {
                UserProfileSetting settings = UserProfileSetting.LoadActiveProfile();

                if (settings != null)
                {
                    retVal = _instance = new SyncApplication(outlookApplication, settings);
                }
            }
            return(retVal);
        }
Exemplo n.º 3
0
 private static void CreateOutlookNamespace()
 {
     //Try to create new Outlook namespace 5 times, because mostly it fails the first time, if not yet running
     for (int i = 0; i < 5; i++)
     {
         try
         {
             _outlookNamespace = OutlookApplication.GetNamespace("MAPI");
             break;  //Exit the for loop, if getting outlook namespace was successful
         }
         catch (COMException ex)
         {
             if (ex.ErrorCode == unchecked ((int)0x80029c4a))
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
             }
             if (i == 4)
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and running.", ex);
             }
             else
             {
                 Logger.Log("Try: " + i, EventType.Debug);
                 Thread.Sleep(1000 * 10 * (i + 1));
             }
         }
         catch (InvalidCastException ex)
         {
             Logger.Log(ex, EventType.Debug);
             throw new NotSupportedException(OutlookRegistryUtils.GetPossibleErrorDiagnosis(), ex);
         }
         catch (Exception ex)
         {
             if (i == 4)
             {
                 Logger.Log(ex, EventType.Debug);
                 throw new NotSupportedException("Could not connect to 'Microsoft Outlook'. Make sure Outlook 2003 or above version is installed and running.", ex);
             }
             else
             {
                 Logger.Log("Try: " + i, EventType.Debug);
                 Thread.Sleep(1000 * 10 * (i + 1));
             }
         }
     }
 }
Exemplo n.º 4
0
        public bool SendEmail(OutlookApplication application, EmailWrapper emailData)
        {
            bool success = application.CreateEmail(out OutlookEmail email);

            if (success)
            {
                email.SetTo(emailData.Receiver);
                email.SetSubject(emailData.Subject);
                email.SetHTMLBody(emailData.HTMLBody);
                email.SendEmail();
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 5
0
        //this method must call in Addin thread
        public static UIController CreateInstance(AddinModule addinModule)
        {
            if (addinModule == null)
            {
                throw new ArgumentNullException("addinModule");
            }

            if (_instance != null)
            {
                return(_instance);
            }

            //Set LogFilePath
            DebugAssistant.LogFilePath = ApplicationConfig.LogPathFile;

            FormSyncOptions            syncOptionsForm     = new FormSyncOptions();
            FormSyncItem               syncForm            = new FormSyncItem();
            FormSyncConflictResolution syncConflictResForm = new FormSyncConflictResolution();
            OutlookListener            listener            = new OutlookListener(addinModule);
            IntPtr             handle             = listener.Handle;
            OutlookApplication outlookApplication = OutlookApplication.CreateInstance(listener);
            SyncApplication    syncApp            = SyncApplication.CreateInstance(outlookApplication);

            if (syncApp == null)
            {
                throw new NullReferenceException("syncApp");
            }

            UIController retVal = new UIController(addinModule, listener, syncApp, syncForm,
                                                   syncOptionsForm, syncConflictResForm);

            if (syncConflictResForm != null)
            {
                //Force create control
                handle = syncConflictResForm.Handle;
            }

            if (syncForm != null)
            {
                //Subscribe events SyncApp(Model)
                retVal.HookEvents(syncApp);
                //Sunscribe events syncForm(View)
                retVal.HookEvents(syncForm);
                //Force create control
                handle = syncForm.Handle;

                //TODO: Нужно прочитать историю последней синхронизации и установить статус прошлой синхронизации
                syncForm.AddSyncMenuItem(Outlook.OlItemType.olAppointmentItem);
                syncForm.AddSyncMenuItem(Outlook.OlItemType.olContactItem);
                syncForm.AddSyncMenuItem(Outlook.OlItemType.olTaskItem);
                syncForm.AddSyncMenuItem(Outlook.OlItemType.olNoteItem);
                //Appointment
                SyncItemInfo           status          = new SyncItemInfo();
                syncAppointmentSetting apppointSetting = syncApp.CurrentSettings.CurrentSyncAppointentSetting;
                if (apppointSetting != null)
                {
                    status.Status = (eSyncStatus)apppointSetting.lastSyncStatus;
                    if (status.Status != eSyncStatus.Unknow)
                    {
                        status.LastSyncDate = new DateTime(TimeSpan.TicksPerSecond * apppointSetting.lastSyncDate);
                    }

                    if (status.Status == eSyncStatus.Ok || status.Status == eSyncStatus.Unknow)
                    {
                        status.Status = eSyncStatus.Ready;
                    }
                }

                syncForm.ThrSetSyncItemStatus(Outlook.OlItemType.olAppointmentItem, status);
                //Contact
                //Task
                status        = new SyncItemInfo();
                status.Status = eSyncStatus.Ready;
                syncForm.ThrSetSyncItemStatus(Outlook.OlItemType.olTaskItem, status);
                //Note
                status        = new SyncItemInfo();
                status.Status = eSyncStatus.Unknow;
                syncForm.ThrSetSyncItemStatus(Outlook.OlItemType.olContactItem, status);

                syncForm.ThrSetSyncItemStatus(Outlook.OlItemType.olNoteItem, status);


                retVal._syncItemForm = syncForm;
            }

            if (syncOptionsForm != null)
            {
                //Force create control
                handle = syncOptionsForm.Handle;
                //Sunscribe events syncOptionsForm(View)
                retVal.HookEvents(syncOptionsForm);
            }

            _instance = retVal;

            return(retVal);
        }
Exemplo n.º 6
0
        private void SendEmails()
        {
            App app = Application.Current as App;

            if (app.SqlManager.IsConnected)
            {
                // Getting the entries where prealert is 0 / false
                ISqlResultSet result = app.SqlManager.ExecuteQuerryFromFile("Sql/GetPreAlertFalse.sql");
                if (result.GetRowCount() == 0)
                {
                    Console.WriteLine("No entries with PreAlert = false found!");
                    return;
                }

                string           json              = File.ReadAllText(App.TRANSFORM_DIR + "PurchaseOrder.json");
                TableTransformer tableTransformer  = TableTransformer.FromJson(json);
                ISqlResultSet    transformedResult = tableTransformer.TransformSqlResultSet(result);

                json             = File.ReadAllText(App.TRANSFORM_DIR + "Items.json");
                tableTransformer = TableTransformer.FromJson(json);

                HashSet <int> ids = new HashSet <int>();
                for (int i = 0; i < result.GetRowCount(); i++)
                {
                    int?nid = result.GetValue(0, i).AsInt();
                    if (nid.HasValue)
                    {
                        ids.Add(nid.Value);
                    }
                }

                List <EmailWrapper> emails = new List <EmailWrapper>();

                for (int i = 0; i < result.GetRowCount(); i++)
                {
                    int           idcol                 = result.ColumnIndexOf("ID");
                    int           colid                 = result.GetValue(idcol, i).AsInt().Value;
                    int           inboundcol            = result.ColumnIndexOf("INBOUNDID");
                    int           inboundid             = result.GetValue(inboundcol, i).AsInt().Value;
                    ISqlResultSet itemResult            = app.SqlManager.ExectuteParameterizedQuerryFromFile("Sql/ItemLevel.sql", new string[] { inboundid.ToString() });
                    ISqlResultSet transformedItemResult = tableTransformer.TransformSqlResultSet(itemResult);

                    EmailDataContext context = new EmailDataContext
                    {
                        ShipmentTable = SimpleHTMLTable.FromSqlResult(transformedResult, i, 1),
                        ItemTable     = SimpleHTMLTable.FromSqlResult(transformedItemResult)
                    };

                    string     htmlSource = File.ReadAllText("Html/EmailTemplate.html");
                    HTMLParser parser     = new HTMLParser(htmlSource)
                    {
                        DataContext = context
                    };
                    string parsedSource = parser.Parse();

                    EmailWrapper emailData = new EmailWrapper
                    {
                        ID         = colid,
                        ShouldSend = true,
                        Receiver   = app.Settings.EmailAdress,
                        Subject    = ("PREALERT - OrderID=" + inboundid),
                        HTMLBody   = parsedSource
                    };

                    emails.Add(emailData);
                }

                if (app.Settings.ShowEmailsBeforeSending)
                {
                    EmailPreviewWindow emailPreviewWindow = new EmailPreviewWindow(emails);
                    emailPreviewWindow.ShowDialog();
                }

                HashSet <int> sendIds = new HashSet <int>();

                OutlookApplication application = OutlookApplication.CreateApplication();
                foreach (EmailWrapper data in emails)
                {
                    if (data.ShouldSend)
                    {
                        //Console.WriteLine("EmailData: ID:{0}, TO:{1}, SUBJECT:{2}, SHOULD_SEND:{3}", data.ID, data.Receiver, data.Subject, data.ShouldSend);
                        bool sent = SendEmail(application, data);
                        if (sent)
                        {
                            sendIds.Add(data.ID);
                        }
                    }
                }

                if (sendIds.Count > 0)
                {
                    // updating the prealert to 1 / true
                    StringBuilder conditionBuilder = new StringBuilder();

                    int j = 0;
                    foreach (int id in sendIds)
                    {
                        conditionBuilder.Append("ID = " + id);
                        j++;
                        if (j < sendIds.Count)
                        {
                            conditionBuilder.Append(" OR ");
                        }
                    }

                    string updateQuerry = "UPDATE TRITPurchaseOrder SET PREALERT = 1 WHERE " + conditionBuilder.ToString();
                    int    rowsAffected = app.SqlManager.ExecuteWithoutResult(updateQuerry);
                    Console.WriteLine("Rows affected: {0}", rowsAffected);
                }
            }
        }
Exemplo n.º 7
0
 public static AppointmentSyncProvider CreateInstance(syncAppointmentSetting settings, OutlookApplication outlookApplication)
 {
     return(new AppointmentSyncProvider(settings, outlookApplication));
 }
Exemplo n.º 8
0
 private AppointmentSyncProvider(syncAppointmentSetting settings, OutlookApplication outlookApplication)
 {
     _outlookApplication = outlookApplication;
     _activeSyncSetting  = settings;
 }
Exemplo n.º 9
0
 private SyncApplication(OutlookApplication outlookApplication, UserProfileSetting settings)
 {
     _outlookApplication = outlookApplication;
     _settings           = settings;
     _workManager        = new ThreadedWorkManager <Outlook.OlItemType>(this.DoSync);
 }