Пример #1
0
        public static void SendMessage(string[] To, string Subject, string Body, Mediachase.IBN.Business.ControlSystem.DirectoryInfo Attachments, string Mode, NameValueCollection Params)
        {
            // Cleanup Temporary files
            DbEMailTempFile.CleanUp();

            #region Validate Arguments
            if (To == null)
            {
                throw new ArgumentNullException("To");
            }

            if (Subject == null)
            {
                throw new ArgumentNullException("Subject");
            }

            if (Body == null)
            {
                throw new ArgumentNullException("Body");
            }

            //if (To.Length == 0)
            //    throw new ArgumentOutOfRangeException("To", "Email recipient list is empty.");

            if (Mode == null)
            {
                Mode = string.Empty;
            }

            if (Params == null)
            {
                Params = new NameValueCollection();
            }
            #endregion

            string FromEmail = string.Empty;

            switch (Mode)
            {
            case EMailClient.IssueMode:
            case EMailClient.SmtpTestMode:
                FromEmail = Alerts2.AlertSenderEmail;
                break;

            default:
                FromEmail = Security.CurrentUser.Email;
                break;
            }

            string FullFromEmail = string.Format("\"{0} {1}\" <{2}>",
                                                 Security.CurrentUser.LastName,
                                                 Security.CurrentUser.FirstName,
                                                 FromEmail);

            using (DbTransaction tran = DbTransaction.Begin())
            {
                EMailMessageLogSetting EmailLogSettings = EMailMessageLogSetting.Current;
                if (EmailLogSettings.IsActive)
                {
                    EMailMessageLog.CleanUp(EmailLogSettings.Period);
                }
                else
                {
                    EmailLogSettings = null;
                }

                Mode = Mode.ToLower();

                #region Pre-format incoming arguments
                switch (Mode)
                {
                case EMailClient.IssueMode:
                    if (Params["IssueId"] == null)
                    {
                        throw new ArgumentNullException("Params[\"IssueId\"]");
                    }

                    int IssueId = int.Parse(Params["IssueId"]);

                    // TODO: Validate Subject & Ticket
                    if (TicketUidUtil.LoadFromString(Subject) == string.Empty)
                    {
                        IncidentBox incidentBox = IncidentBox.Load(Incident.GetIncidentBox(IssueId));

                        string IncidentTicket = Incident.GetIdentifier(IssueId);

                        if (incidentBox.Document.GeneralBlock.AllowOutgoingEmailFormat)
                        {
                            StringBuilder sb = new StringBuilder(incidentBox.Document.GeneralBlock.OutgoingEmailFormatSubject, 4096);

                            sb.Replace("[=Title=]", Subject);
                            sb.Replace("[=Ticket=]", IncidentTicket);
                            //sb.Replace("[=Text=]", Body);
                            sb.Replace("[=FirstName=]", Security.CurrentUser.FirstName);
                            sb.Replace("[=LastName=]", Security.CurrentUser.LastName);

                            Subject = sb.ToString();
                        }
                        else
                        {
                            Subject = string.Format("RE: [{0}] {1}",
                                                    IncidentTicket,
                                                    Subject);
                        }
                    }
                    break;

                default:
                    break;
                }

                #endregion

                Pop3Message msg = Create(FullFromEmail, To, Subject, Body, Attachments);

                switch (Mode)
                {
                case EMailClient.IssueMode:
                    #region Issue
                    int IssueId = int.Parse(Params["IssueId"]);

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

                    bool AllowEMailRouting = true;

                    EMailRouterIncidentBoxBlock settings = IncidentBoxDocument.Load(incidentBox.IncidentBoxId).EMailRouterBlock;
                    if (!settings.AllowEMailRouting)
                    {
                        AllowEMailRouting = false;
                    }

                    EMailRouterPop3Box internalPop3Box = EMailRouterPop3Box.ListInternal();
                    if (internalPop3Box == null)
                    {
                        AllowEMailRouting = false;
                    }

                    // Register Email Message
                    // OZ: [2007--05-25] Fix Problem Object reference not set to an instance of an object If (internalPop3Box == NULL)
                    int EMailMessageId = EMailMessage.Create(internalPop3Box != null?
                                                             internalPop3Box.EMailRouterPop3BoxId : EMailRouterOutputMessage.FindEMailRouterPublicId(IssueId),
                                                             msg);

                    // Register Forume Node
                    int ThreadNodeId = EMailMessage.AddToIncidentMessage(true, IssueId, EMailMessageId, msg);

                    // Send Message

                    if (AllowEMailRouting)
                    {
                        ArrayList excludedUsers = EMailRouterOutputMessage.Send(IssueId, internalPop3Box, msg, To);
                        SystemEvents.AddSystemEvents(SystemEventTypes.Issue_Updated_Forum_MessageAdded, IssueId, -1, excludedUsers);
                    }
                    else
                    {
                        FromEmail     = EMailRouterOutputMessage.FindEMailRouterPublicEmail(IssueId);
                        FullFromEmail = string.Format("\"{0} {1}\" <{2}>",
                                                      Security.CurrentUser.LastName,
                                                      Security.CurrentUser.FirstName,
                                                      FromEmail);

                        // Create OutputMessageCreator
                        OutputMessageCreator issueOutput = new OutputMessageCreator(msg,
                                                                                    -1,
                                                                                    FromEmail,
                                                                                    FullFromEmail);

                        // Fill Recipent
                        foreach (string ToItem in To)
                        {
                            issueOutput.AddRecipient(ToItem);
                        }

                        foreach (EMailIssueExternalRecipient exRecipient in EMailIssueExternalRecipient.List(IssueId))
                        {
                            issueOutput.AddRecipient(exRecipient.EMail);
                        }

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

                        //Send Smtp Message
                        foreach (OutputMessage outputMsg in issueOutput.Create())
                        {
                            SmtpClientUtility.SendMessage(OutgoingEmailServiceType.HelpDeskEmailBox, emailBoxId, outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data);
                        }

                        ArrayList excludedUsers = new ArrayList();

                        foreach (string ToItem in To)
                        {
                            int emailUserId = DBUser.GetUserByEmail(ToItem, false);
                            if (emailUserId > 0)
                            {
                                excludedUsers.Add(emailUserId);
                            }
                        }

                        SystemEvents.AddSystemEvents(SystemEventTypes.Issue_Updated_Forum_MessageAdded, IssueId, -1, excludedUsers);
                    }
                    #endregion
                    break;

                case EMailClient.SmtpTestMode:
                    throw new NotImplementedException();
                //OutputMessageCreator smtpTestOutput = new OutputMessageCreator(msg,
                //    -1,
                //    FromEmail,
                //    FullFromEmail);

                //// Fill Recipent
                //foreach (string ToItem in To)
                //{
                //    smtpTestOutput.AddRecipient(ToItem);
                //}

                ////Send Smtp Message
                //foreach (OutputMessage outputMsg in smtpTestOutput.Create())
                //{
                //    //SmtpClientUtility.DirectSendMessage(outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data);
                //    //SmtpBox.SendTestEmail(
                //}
                //break;
                default:
                    #region Default
                    // Create OutputMessageCreator
                    OutputMessageCreator defaultOutput = new OutputMessageCreator(msg,
                                                                                  -1,
                                                                                  FromEmail,
                                                                                  FullFromEmail);

                    // Fill Recipent
                    foreach (string ToItem in To)
                    {
                        defaultOutput.AddRecipient(ToItem);
                    }

                    //Send Smtp Message
                    foreach (OutputMessage outputMsg in defaultOutput.Create())
                    {
                        SmtpClientUtility.SendMessage(OutgoingEmailServiceType.SendFile, null, outputMsg.MailFrom, outputMsg.RcptTo, outputMsg.Subject, outputMsg.Data);
                    }

                    #endregion
                    break;
                }

                if (Attachments != null)
                {
                    FileStorage.InnerDeleteFolder(Attachments.Id);
                }

                tran.Commit();
            }
        }
Пример #2
0
        protected void btnSubmit_Click(object sender, System.EventArgs e)
        {
            bool withResponse = false;

            if (this.IsPostBack)
            {
                try
                {
                    string sUserLight = "userlight";

                    // check user's name and password here
                    UserLight currentUser = Security.GetUser(Login.Value, Password.Value);
                    if (currentUser == null)
                    {
                        throw new HttpException(405, "Your login or password is invalid.");
                    }

                    // Security Addon [3/2/2004]
                    UserLight retUser = null;
                    if (HttpContext.Current.Items.Contains(sUserLight))
                    {
                        retUser = (UserLight)HttpContext.Current.Items[sUserLight];
                        HttpContext.Current.Items.Remove(sUserLight);
                    }
                    HttpContext.Current.Items.Add(sUserLight, currentUser);
                    // End Security Addon [3/2/2004]

                    // New Folder System Addon [12/27/2005]
                    string ContainerName = "FileLibrary";
                    string ContainerKey  = String.Empty;
                    int    objectId      = Int32.Parse(ObjectId.Value);
                    int    objectTypeId  = Int32.Parse(ObjectTypeId.Value);
                    int    folderId      = 0;

                    switch ((ObjectTypes)objectTypeId)
                    {
                    case ObjectTypes.Project:
                        ContainerKey = "ProjectId_" + objectId.ToString();
                        break;

                    case ObjectTypes.Issue:
                        ContainerKey = string.Empty;
                        break;

                    case ObjectTypes.Task:
                        ContainerKey = "TaskId_" + objectId.ToString();
                        break;

                    case ObjectTypes.CalendarEntry:
                        ContainerKey = "EventId_" + objectId.ToString();
                        break;

                    case ObjectTypes.Folder:
                        ContainerKey = "Workspace";
                        if (objectId != 0)
                        {
                            folderId     = objectId;
                            ContainerKey = Mediachase.IBN.Business.ControlSystem.DirectoryInfo.GetContainerKey(folderId);
                        }
                        break;

                    case ObjectTypes.Document:
                        ContainerKey = "DocumentId_" + objectId.ToString();
                        break;

                    case ObjectTypes.ToDo:
                        ContainerKey = "ToDoId_" + objectId.ToString();
                        break;
                    }
                    if (ContainerKey != String.Empty)
                    {
                        Mediachase.IBN.Business.ControlSystem.BaseIbnContainer bic = Mediachase.IBN.Business.ControlSystem.BaseIbnContainer.Create(ContainerName, ContainerKey);
                        Mediachase.IBN.Business.ControlSystem.FileStorage      fs  = (Mediachase.IBN.Business.ControlSystem.FileStorage)bic.LoadControl("FileStorage");

                        if (folderId == 0)
                        {
                            folderId = fs.Root.Id;
                        }

                        string fileName = Path.GetFileName(PublishedFile.PostedFile.FileName);
                        if (FileName.Value != string.Empty)
                        {
                            fileName = FileName.Value;
                        }

                        /*int	index = 0;
                         * while (fs.FileExist(fileName, folderId))
                         * {
                         *      fileName = Path.GetFileNameWithoutExtension(PublishedFile.PostedFile.FileName) + (index++).ToString();
                         *      fileName += Path.GetExtension(PublishedFile.PostedFile.FileName);
                         * }*/
                        fs.SaveFile(folderId, fileName, PublishedFile.PostedFile.InputStream);
                    }
                    else if ((ObjectTypes)objectTypeId == ObjectTypes.List)
                    {
                        string xml = SaveNParseExcel(PublishedFile.PostedFile.FileName, PublishedFile.PostedFile.InputStream);

                        Response.BinaryWrite(System.Text.Encoding.UTF8.GetBytes(xml));
                        withResponse = true;
                    }
                    else if ((ObjectTypes)objectTypeId == ObjectTypes.Issue)
                    {
                        string fileName = Path.GetFileName(PublishedFile.PostedFile.FileName);
                        if (FileName.Value != string.Empty)
                        {
                            fileName = FileName.Value;
                        }

                        // OZ: 2008-08-19 Add Process Eml Attachments
                        if (Path.GetExtension(fileName).ToLower() == ".eml")
                        {
                            // Calculate email box
                            int emailBoxId = EMailRouterOutputMessage.FindEMailRouterPublicId(objectId);

                            // Save Email to email storage
                            int eMailMessageId = EMailMessage.CreateFromStream(emailBoxId, PublishedFile.PostedFile.InputStream);

                            EMailMessage.AddToIncidentMessage(false, objectId, eMailMessageId);

                            try
                            {
                                ArrayList excludeUsers = EMailRouterOutputMessage.Send(objectId, eMailMessageId);
                                SystemEvents.AddSystemEvents(SystemEventTypes.Issue_Updated_Forum_MessageAdded, objectId, -1, excludeUsers);
                            }
                            catch (Exception ex)
                            {
                                System.Diagnostics.Trace.WriteLine(ex);
                                //Log.WriteError(ex.ToString());
                            }
                        }
                        // Process Default files
                        else
                        {
                            BaseIbnContainer destContainer = BaseIbnContainer.Create("FileLibrary", string.Format("IncidentId_{0}", objectId));
                            ForumStorage     forumStorage  = (ForumStorage)destContainer.LoadControl("ForumStorage");

                            ForumThreadNodeInfo info = forumStorage.CreateForumThreadNode(string.Empty, Security.CurrentUser.UserID, (int)ForumStorage.NodeContentType.TextWithFiles);

                            BaseIbnContainer forumContainer = BaseIbnContainer.Create("FileLibrary", string.Format("ForumNodeId_{0}", info.Id));
                            FileStorage      fs             = (FileStorage)forumContainer.LoadControl("FileStorage");


                            fs.SaveFile(fileName, PublishedFile.PostedFile.InputStream);

                            ForumThreadNodeSettingCollection settings1 = new ForumThreadNodeSettingCollection(info.Id);
                            settings1.Add(ForumThreadNodeSetting.Internal, "1");
                        }
                    }
                    // End New Folder System Addon [12/27/2005]

                    // Security Addon [3/2/2004]
                    HttpContext.Current.Items.Remove(sUserLight);
                    HttpContext.Current.Items.Add(sUserLight, retUser);
                    // End Security Addon [3/2/2004]
                }
                catch (Exception ex)
                {
                    throw new HttpException(405, "Internal Exception", ex);
                }
                if (!withResponse)
                {
                    this.Response.Write("Published Completed");
                }
                this.Response.End();
            }
        }