/// <summary>
        /// Sends the auto reply.
        /// </summary>
        /// <param name="IncidentId">The incident id.</param>
        /// <param name="From">From.</param>
        /// <param name="To">To.</param>
        internal static void SendAutoReply(int IncidentId, string From, string To)
        {
            IncidentBox incidentBox = IncidentBox.Load(Incident.GetIncidentBox(IncidentId));

            if (!incidentBox.Document.EMailRouterBlock.SendAutoReply)
            {
                return;
            }

            Alerts2.Message msg = Alerts2.GetMessage(incidentBox.Document.GeneralBlock.AutoReplyEMailSubjectTemplate,
                                                     incidentBox.Document.GeneralBlock.AutoReplyEMailBodyTemplate,
                                                     SystemEventTypes.Issue_Created,
                                                     IncidentId, null,
                                                     -1,
                                                     Security.CurrentUser.UserID);

            string subject = msg.Subject, body = msg.Body;

            MemoryStream emlMessage = new MemoryStream();

            byte[] tmpBuffer = null;

            #region Fill Pop3 Message Stream
            // Create Pop3 Message Headers
            StringBuilder sbHeaders = new StringBuilder();

            sbHeaders.AppendFormat("Date: {0}", GetEMailCreationDate()).Append("\r\n");
            sbHeaders.Append("From: [email protected]").Append("\r\n");
            sbHeaders.Append("To: [email protected]").Append("\r\n");
            sbHeaders.AppendFormat("Subject: {0}", Rfc822HeaderCollection.Encode2AsciiString(subject)).Append("\r\n");
            sbHeaders.Append("MIME-Version: 1.0").Append("\r\n");
            sbHeaders.Append("Content-Type: multipart/mixed; boundary=\"----------7E143249668A83E\"").Append("\r\n");
            sbHeaders.Append("\r\n");

            tmpBuffer = Encoding.ASCII.GetBytes(sbHeaders.ToString());
            emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length);

            // Create Pop3 Message Entry
            StringBuilder sbMessage = new StringBuilder();

            sbMessage.Append("------------7E143249668A83E").Append("\r\n");

            // IF MESSAGE IS PLAIN TEXT
            //sbMessage.Append("Content-Type: text/plain; charset=utf-8").Append("\r\n");

            // IF MESSAGE IS HTML TEXT
            sbMessage.Append("Content-Type: text/html; charset=utf-8").Append("\r\n");

            sbMessage.Append("Content-Transfer-Encoding: base64").Append("\r\n");
            sbMessage.Append("\r\n");

            // OZ Fixed 500 5.3.3 Line too long (in reply to end of DATA command)
            sbMessage.Append(Convert.ToBase64String(Encoding.UTF8.GetBytes(body), Base64FormattingOptions.InsertLineBreaks)).Append("\r\n");

            tmpBuffer = Encoding.ASCII.GetBytes(sbMessage.ToString());
            emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length);

            // Add Final Line
            tmpBuffer = Encoding.ASCII.GetBytes("------------7E143249668A83E--\r\n\r\n");
            emlMessage.Write(tmpBuffer, 0, tmpBuffer.Length);

            #endregion

            Pop3Message InMsg = new Pop3Message(emlMessage);

            OutputMessageCreator outputAutoReply = new OutputMessageCreator(InMsg, IncidentId, From, From);
            outputAutoReply.AddRecipient(To);

            int emailBoxId = EMail.EMailRouterOutputMessage.FindEMailRouterPublicId(IncidentId);

            foreach (OutputMessage outputMsg in outputAutoReply.Create())
            {
                try
                {
                    SmtpClientUtility.SendMessage(OutgoingEmailServiceType.HelpDeskEmailBox, emailBoxId, outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(ex);
                    Log.WriteError(ex.ToString());
                }
            }
        }
Beispiel #2
0
        private static void SendReminder(DateTypes dateType, ObjectTypes objecType, int?objectId, Guid?objectUid, List <int> users)
        {
            // TODO: Step 0. Check Security (Not implemented yet)

            // Step 1. Calculate variables
            foreach (int userId in users)
            {
                int reminderType = 0;

                using (IDataReader reader = User.GetUserPreferences(userId))
                {
                    if (reader.Read())
                    {
                        reminderType = (int)reader["ReminderType"];
                    }
                }

                if (reminderType != 0)
                {
                    // Step 2. Get Message Template
                    ReminderTemplate tmpl = Reminder.GetMessageTemplate(dateType, User.GetUserLanguage(userId));

                    // Step 2.1. Calculate Variables
                    ArrayList vars = new ArrayList();

                    Alerts2.GetObjectVariables(objecType, objectId, objectUid, false, Reminder.GetVariables(dateType), vars);

                    // Step 3. Replace variables and GetMessage
                    Alerts2.Message msg = Reminder.GetMessage(tmpl, objectId, objectUid, objecType, userId, (VariableInfo[])vars.ToArray(typeof(VariableInfo)));

                    // Step 4. Save to log
                    using (DbTransaction tran = DbTransaction.Begin())
                    {
                        int logId = DbAlert2.MessageLogAdd(msg.Subject, msg.Body);
                        DBSystemEvents.RecipientUpdateSend(userId, (reminderType & 1) != 0, PortalConfig.UseIM && (reminderType & 2) != 0, logId);                         // IsNotifiedByEmail, IsNotifiedByIBN

                        tran.Commit();
                    }

                    #region -- Send via Email --
                    // Step 5. Send via Email
                    try
                    {
                        if ((reminderType & 1) != 0)                        //IsNotifiedByEmail
                        {
                            string body = "<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\" /></head><body>" + msg.Body + "</body></html>";

                            using (DbTransaction tran = DbTransaction.Begin())
                            {
                                Alerts2.SendMessage(DeliveryType.Email, Reminder.GetAddress(DeliveryType.Email, userId), body, msg.Subject);

                                DBSystemEvents.RecipientUpdateSent(userId, (int)DeliveryType.Email, true);

                                tran.Commit();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteError(ex.ToString());
                    }
                    #endregion

                    #region -- Send via IM --
                    // Step 6. Send via IM
                    try
                    {
                        if ((reminderType & 2) != 0)                        //uInfo != null && uInfo.IsNotifiedByIBN)
                        {
                            using (DbTransaction tran = DbTransaction.Begin())
                            {
                                Alerts2.SendMessage(DeliveryType.IBN, Reminder.GetAddress(DeliveryType.IBN, userId), msg.Body, msg.Subject);

                                DBSystemEvents.RecipientUpdateSent(userId, (int)DeliveryType.IBN, true);

                                tran.Commit();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteError(ex.ToString());
                    }
                    #endregion
                }
            }
        }