Example #1
0
        /// <summary>
        /// Sends the test email.
        /// </summary>
        /// <param name="smtpBoxId">The SMTP box id.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="htmlBody">The HTML body.</param>
        /// <param name="completeCheckPageUrl">The complete check page URL.</param>
        public static void SendTestEmail(int smtpBoxId, string subject, string htmlBody, string completeCheckPageUrl)
        {
            if (subject == null)
            {
                throw new ArgumentNullException("subject");
            }
            if (htmlBody == null)
            {
                throw new ArgumentNullException("htmlBody");
            }
            if (completeCheckPageUrl == null)
            {
                throw new ArgumentNullException("completeCheckPageUrl");
            }

            SmtpBoxRow row = new SmtpBoxRow(smtpBoxId);

            // Reset Check params
            row.Checked  = false;
            row.CheckUid = Guid.NewGuid();

            row.Update();

            // Create Complete Back Link
            string backLink = completeCheckPageUrl + "?uid=" + row.CheckUid.ToString();

            SmtpClient client = SmtpClientUtility.CreateSmtpClient(new SmtpBox(row));

            string to = Security.CurrentUser.Email;

            // Create Mail Message
            MailMessage tesMsg = new MailMessage();

            MailAddress currentUser = new MailAddress(to, Security.CurrentUser.DisplayName);

            tesMsg.To.Add(currentUser);
            tesMsg.From    = currentUser;
            tesMsg.Subject = subject;

            tesMsg.IsBodyHtml = true;
            tesMsg.Body       = htmlBody + "<br/><a href='" + backLink + "'>" + backLink + "</a>";

            client.Send(tesMsg);

            if (!string.IsNullOrEmpty(tesMsg.ErrorMessage))
            {
                throw new SmtpException(tesMsg.ErrorMessage);
            }
        }
        public static ArrayList Send(int IncidentId, EMailRouterPop3Box pop3Box, Pop3Message InMsg, string[] RecipientEmails)
        {
            ArrayList retVal = new ArrayList();

            IncidentBox incidentBox = IncidentBox.Load(Incident.GetIncidentBox(IncidentId));

            EMailRouterIncidentBoxBlock settings = IncidentBoxDocument.Load(incidentBox.IncidentBoxId).EMailRouterBlock;

            if (pop3Box.IsInternal)
            {
                #region Internal -> Extrenal
                // Internal -> Internal Info

                // 2009-06-16 OZ: Add Chech send emails to internal users
                if (settings.AllowEMailRouting)
                {
                    // 2007-02-12 OZ: "FN LN" <*****@*****.**>
                    string FromEmail = string.Format("\"{0} {1}\" <{2}>",
                                                     Security.CurrentUser.LastName,
                                                     Security.CurrentUser.FirstName,
                                                     pop3Box.EMailAddress);

                    OutputMessageCreator output2Iternal = new OutputMessageCreator(InMsg, IncidentId, pop3Box.EMailAddress, FromEmail);

                    // Exclude a message sender
                    //output2Iternal.AddIgnoreRecipient(EMailMessage.GetSenderEmail(InMsg));

                    // Load InternalUser
                    foreach (int UserId in GetInternalUsersByIncidentId(IncidentId))
                    {
                        output2Iternal.AddRecipient(UserId);
                        retVal.Add(UserId);
                    }

                    // Load InformationRecipientList
                    //				foreach(int infoRecipient in IncidentBoxDocument.Load(incidentBox.IncidentBoxId).EMailRouterBlock.InformationRecipientList)
                    //				{
                    //					output2Iternal.AddRecipient(infoRecipient);
                    //				}

                    foreach (OutputMessage outputMsg in output2Iternal.Create())
                    {
                        //try
                        //{
                        SmtpClientUtility.SendMessage(OutgoingEmailServiceType.HelpDeskEmailBox, pop3Box.EMailRouterPop3BoxId, outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data);
                        //}
                        //catch(Exception ex)
                        //{
                        //    System.Diagnostics.Trace.WriteLine(ex);
                        //    Log.WriteError(ex.ToString());
                        //}
                    }
                }

                // Internal -> Extrenal
                string publicEmail = FindEMailRouterPublicEmail(IncidentId);

                if (publicEmail != string.Empty)
                {
                    //EMailRouterPop3Box externalPop3Box = EMailRouterPop3Box.Load(realEMailBoxId);

                    OutputMessageCreator output2External = new OutputMessageCreator(InMsg, IncidentId, publicEmail, publicEmail);

                    // Load External Senders
//					foreach(string exRecipient in EMailMessage.GetExternalSendersByIncidentId(IncidentId))
//					{
//						output2External.AddRecipient(exRecipient);
//					}

                    // Load External Senders
                    foreach (EMailIssueExternalRecipient exRecipient in EMailIssueExternalRecipient.List(IncidentId))
                    {
                        output2External.AddRecipient(exRecipient.EMail);
                    }

                    // 2007-03-12 RecipientEmails Addon
                    if (RecipientEmails != null)
                    {
                        foreach (string exItem in RecipientEmails)
                        {
                            int emailUserId = DBUser.GetUserByEmail(exItem, false);
                            if (emailUserId > 0)
                            {
                                if (!retVal.Contains(emailUserId))
                                {
                                    output2External.AddRecipient(exItem);
                                    retVal.Add(emailUserId);
                                }
                            }
                            else
                            {
                                output2External.AddRecipient(exItem);
                            }
                        }
                    }
                    //

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

                    foreach (OutputMessage outputMsg in output2External.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());
                        //}
                    }
                }
                #endregion
            }
            else
            {
                if (!settings.AllowEMailRouting)
                {
                    return(retVal);
                }

                EMailRouterPop3Box internalPop3Box = EMailRouterPop3Box.ListInternal();
                if (internalPop3Box == null)
                {
                    return(retVal);
                }

                #region                 // External -> Internal
                // External -> Internal

                string SenderName = EMailMessage.GetSenderName(InMsg);
                string FromEmail  = SenderName == string.Empty?internalPop3Box.EMailAddress:
                                    string.Format("\"{0}\" <{1}>",
                                                  SenderName,
                                                  internalPop3Box.EMailAddress);

                OutputMessageCreator output = new OutputMessageCreator(InMsg, IncidentId, internalPop3Box.EMailAddress, FromEmail);

                string Subject = (InMsg.Subject == null?string.Empty:InMsg.Subject);

                if (Subject == string.Empty)
                {
                    // OZ: Maybe:  Set Default Inicdent Title if subject is empty
                    //Subject = Incident.GetIncidentTitle(IncidentId);
                }

                if (TicketUidUtil.LoadFromString(Subject) == string.Empty)
                {
                    output.Subject = string.Format("[{0}] {1}", TicketUidUtil.Create(incidentBox.IdentifierMask, IncidentId), Subject);
                }

                foreach (int UserId in GetInternalUsersByIncidentId(IncidentId))
                {
                    output.AddRecipient(UserId);
                    retVal.Add(UserId);
                }

//				IncidentBoxDocument incidentDoc = IncidentBoxDocument.Load(incidentBox.IncidentBoxId);
//
//				foreach(int infoRecipient in incidentDoc.EMailRouterBlock.InformationRecipientList)
//				{
//					output.AddRecipient(infoRecipient);
//				}

                foreach (OutputMessage outputMsg in output.Create())
                {
                    //try
                    //{
                    SmtpClientUtility.SendMessage(OutgoingEmailServiceType.HelpDeskEmailBox, internalPop3Box.EMailRouterPop3BoxId, outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data);
                    //}
                    //catch(Exception ex)
                    //{
                    //    System.Diagnostics.Trace.WriteLine(ex);
                    //    Log.WriteError(ex.ToString());
                    //}
                }
                #endregion
            }

            return(retVal);
        }
        /// <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());
                }
            }
        }