Пример #1
0
        public void Init()
        {
            _Id = 1;
            _server = "127.0.0.1";
            _username = "";
            _password = "";
            _bodyTemplate = "{0} {1} {2}";
            _inlineAttachedPictures  = false;
            _reportingUserName = "******";
            _deleteAllMessages= false;

            _entry = new Entry();
            _entry.AttachmentFileNames.Clear();

            List<Project> p = Project.GetAllProjects();
            List<IssueType> issT = IssueType.GetIssueTypesByProjectId(p[0].Id);

            // _entry.ProjectMailbox = new ProjectMailbox("*****@*****.**", p[0].Id, "Admin", "", issT[0].Id);
        }
Пример #2
0
        /// <summary>
        /// ReadMail reads the pop3 mailbox
        /// </summary>
        public void ReadMail()
        {
            Pop3 pop3 = new Pop3();

            try
            {
                try
                {
                    pop3.User = Pop3Username;
                    pop3.Password = Pop3Password;

                    pop3.Connect(Pop3Server);

                    if (pop3.HasTimeStamp == true)
                        pop3.Login();//APOPLogin();
                    else
                        pop3.Login();

                    pop3.GetAccountStat();

                    int j = 1;

                    bool messageWasProcessed = false;

                    // Read each message to find out the recipient. This is for the project.
                    for (; j <= pop3.MessageCount; j++)
                    {
                        SimpleMailMessage message = SimpleMailMessage.Parse(pop3.GetMessage(j));

                        string messageFrom = "";

                        if (message.From.Count > 0)
                        {
                            MailBox from = message.From[0];
                            messageFrom = from.Address;
                        }

                        // E-Mail addresses look usually like this:
                        // My Name <*****@*****.**> or simply
                        // [email protected]. This block handles
                        // both variants.

                        // Need to check the TO, CC and BCC message fields for the bugnet email address
                        List<MailBox[]> recipients = new List<MailBox[]>();
                        recipients.Add((MailBox[])message.To.ToArray());
                        recipients.Add((MailBox[])message.Cc.ToArray());
                        recipients.Add((MailBox[])message.Bcc.ToArray());

                        foreach (MailBox[] toAr in recipients)
                        {
                            foreach (MailBox to in toAr)
                            {
                                string mailbox = to.Address;

                                ProjectMailbox pmbox = ProjectMailbox.GetProjectByMailbox(mailbox);

                                // Only if the mailbox appears in project's mailbox aliases
                                // we accept the message
                                if (pmbox != null)
                                {
                                    string binariesBaseUri;
                                    string binariesPath;

                                    string uploadPath = Project.GetProjectById(pmbox.ProjectId).UploadPath.TrimEnd('/');

                                    //string appPath = HostSetting.GetHostSetting("DefaultUrl");
                                    string appPath = HttpContext.Current.Request.PhysicalApplicationPath;
                                    if (!System.IO.Directory.Exists(appPath))
                                        throw new Exception("Upload path does not exist.");

                                    if (uploadPath.StartsWith("~"))
                                    {
                                        binariesPath = uploadPath.Replace("~", appPath.TrimEnd('\\'));

                                        if (!System.IO.Directory.Exists(appPath))
                                            throw new Exception("Upload path does not exist.");

                                        binariesBaseUri = uploadPath.Replace("~\\", Globals.DefaultUrl).Replace("\\", "/");
                                    }
                                    else
                                    {
                                        binariesPath = string.Concat(appPath, "Uploads\\");

                                        if (!System.IO.Directory.Exists(appPath))
                                            throw new Exception("Upload path does not exist.");

                                        binariesBaseUri = string.Concat(Globals.DefaultUrl, "Uploads/");
                                    }

                                    Entry entry = new Entry();

                                    entry.Title = message.Subject.Trim();
                                    entry.From = messageFrom;
                                    entry.ProjectMailbox = pmbox;

                                    entry.Date = message.Date;

                                    // plain text?
                                    if (message.HtmlDataString.Length == 0)
                                    {
                                        entry.Content.Append(message.TextDataString);
                                    }
                                    else
                                    {
                                        // Strip the <body> out of the message (using code from below)
                                        Regex bodyExtractor = new Regex("<body.*?>(?<content>.*)</body>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                                        Match match = bodyExtractor.Match(message.HtmlDataString);
                                        if (match != null && match.Success && match.Groups["content"] != null)
                                        {
                                            entry.Content.Append(match.Groups["content"].Value);
                                        }
                                        else
                                        {
                                            entry.Content.Append(message.HtmlDataString);
                                        }
                                    }

                                    Hashtable embeddedFiles = new Hashtable();
                                    ArrayList attachedFiles = new ArrayList();

                                    foreach (MailAttachment attachment in message.Attachments)
                                    {
                                        if (attachment.Data != null && attachment.FileName != null && attachment.FileName.Length > 0)
                                        {
                                            string fn = StoreAttachment(attachment, binariesPath);

                                            entry.MailAttachments.Add(attachment);
                                            entry.AttachmentFileNames.Add(fn);

                                            attachedFiles.Add(fn);
                                        }
                                    }

                                    // TODO Should use XSLT templates BGN-1591

                                    if (entry.MailAttachments.Count > 0)
                                    {
                                        entry.Content.Append("<p>");

                                        for (int i = 0; i < entry.MailAttachments.Count; i++)
                                        {
                                            MailAttachment attachment = (MailAttachment)entry.MailAttachments[i];

                                            string absoluteUri = string.Concat(binariesBaseUri, entry.AttachmentFileNames[i]);
                                            if (Pop3InlineAttachedPictures && attachment.ContentType.MimeType == MimeType.Image)
                                            {
                                                entry.Content.Append(String.Format("<br><img src=\"{0}\" />", absoluteUri));
                                            }
                                            else
                                            {
                                                entry.Content.Append(String.Format("<br><a href=\"{0}\" />{1}</a>", absoluteUri, attachment.FileName));
                                            }
                                        }

                                        entry.Content.Append("</p>");
                                    }

                                    messageWasProcessed = true;

                                    SaveEntry(entry);
                                }
                                else
                                {
                                    if (Log.IsWarnEnabled)
                                        Log.WarnFormat("Project Mailbox Not Found: {0}", message.To.ToString());
                                }
                            }
                        }
                        // [email protected] (01-MAR-04)
                        if (Pop3DeleteAllMessages || messageWasProcessed)
                            pop3.DeleteMessage(j);
                    }
                }
                catch (Exception ex)
                {
                    throw ProcessException(ex);
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
            finally
            {
                try
                {
                    pop3.Close();
                }
                catch
                {
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Saves the entry.
        /// </summary>
        /// <param name="entry">The entry.</param>
        public void SaveEntry(Entry entry)
        {
            try
            {
                string body = string.Format(this.BodyTemplate, entry.Content.ToString().Trim(), entry.From, entry.Date.ToString());
                int curPID = entry.ProjectMailbox.ProjectId;

                Issue MailIssue = Issue.GetDefaultIssueByProjectId(curPID, entry.Title.Trim(), body.Trim(), entry.ProjectMailbox.AssignToName, ReportingUserName);

                if (MailIssue.Save())
                {
                    //If there is an attached file present then add it to the database
                    //and copy it to the directory specified in the web.config file
                    for (int i = 0; i < entry.AttachmentFileNames.Count; i++)
                    {
                        MailAttachment attMail = entry.MailAttachments[i] as MailAttachment;
                        IssueAttachment att = new IssueAttachment(0,
                            MailIssue.Id,
                            ReportingUserName,
                            ReportingUserName,
                            DateTime.Now,
                            Path.GetFileName(entry.AttachmentFileNames[i].ToString()),
                            attMail.ContentType.ToString(),
                            attMail.Data,
                            attMail.Data.Length, "Attached via email"
                            );
                        att.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }