public static void DeleteMessages(ImapClient client, IMailFolder mailbox, IList <UniqueId> uids)
 {
     if (client.Capabilities.HasFlag(ImapCapabilities.UidPlus))
     {
         mailbox.Expunge(uids);
     }
     else
     {
         mailbox.AddFlags(uids, MessageFlags.Deleted, true);
         mailbox.Expunge();
     }
 }
Beispiel #2
0
        /// <summary>
        /// 将邮件保存到草稿箱 返回邮件的唯一标识
        /// 调用前先调用配置方法CfgIMAP(),调用制做邮件方法
        /// </summary>
        public int SaveDrafts(int uniqueId = -1)
        {
            try
            {
                using (ImapClient client = ConnectIMAP())
                {
                    // 打开草稿箱,添加邮件
                    IMailFolder folder = client.GetFolder(SpecialFolder.Drafts);
                    folder.Open(FolderAccess.ReadWrite);

                    // 如果保存的是已经有的草稿邮件,则删除它再保存新的草稿.(没找到保存已有草稿的办法)
                    if (uniqueId > -1)
                    {
                        List <UniqueId> uidls = new List <UniqueId>();
                        uidls.Add(new UniqueId((uint)uniqueId));
                        folder.SetFlags(uidls, MessageFlags.Seen | MessageFlags.Deleted, true);
                        folder.Expunge(uidls);
                    }

                    UniqueId?uid = folder.Append(this.message, MessageFlags.Seen | MessageFlags.Draft);
                    //
                    folder.Close();
                    client.Disconnect(true);
                    return(uid.HasValue ? (int)uid.Value.Id : -1);
                }
            }
            catch (Exception e)
            {
                ErrMsg = $"邮件保存草稿时异常:{e.ToString()} [{e.Message}]";
                return(-1);
            }
        }
Beispiel #3
0
        public static void RemoveFolder(IMailFolder folder, Configuration config)
        {
            using var kernel = new FakeItEasyMockingKernel();
            kernel.Rebind <ILogger>().ToConstant(Log.Logger);
            kernel.Rebind <Configuration>().ToConstant(config);
            kernel.Rebind <ImapStore>().ToSelf().InSingletonScope();
            kernel.Rebind <ImapConnectionFactory>().ToSelf().InSingletonScope();
            var imapFac = kernel.Get <ImapConnectionFactory>();

            using var connection = imapFac.GetImapConnectionAsync().Result;

            var subFolders = folder.GetSubfolders();

            foreach (var subFolder in subFolders)
            {
                RemoveFolder(subFolder, config);
            }

            folder.Open(FolderAccess.ReadWrite);
            var allMessages = folder.Search(SearchQuery.All);

            folder.SetFlags(allMessages, MessageFlags.Deleted, true);
            folder.Expunge();
            folder.Close();
            try
            {
                folder.Delete();
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while deleting folder: " + e.ToString());
            }
            connection.Disconnect(true);
        }
Beispiel #4
0
        public void MessageShown(int uid)
        {
            var mess = msg.ElementAt(msg.Count - uid);
            var i    = unSorted.IndexOf(mess);

            if (!seen.Any(m => m.MessageId == mess.MessageId))
            {
                seen.Add(mess);
                notseen.Remove(notseen.Single(foo => foo.MessageId == mess.MessageId));
                inbox.AddFlags(i, MessageFlags.Seen, true);
                inbox.Expunge();
                mailList.MarkAsRead(uid - 1);
            }
        }
Beispiel #5
0
        private static void DeleteMails(IMailFolder inbox, ImapClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }

            for (int i = 0; i < inbox.Count; i++)
            {
                inbox.AddFlags(i, MessageFlags.Deleted, true);
            }

            inbox.Expunge();
            client.Disconnect(true);
            Console.WriteLine("Your mail have been deleted");
        }
Beispiel #6
0
        public HttpStatusCode RemoveEmail(UniqueId id, string folder)
        {
            if (!IsAuthenticated())
            {
                return(HttpStatusCode.ExpectationFailed);
            }
            IMailFolder mailFolder = GetFolder(folder);

            if (mailFolder != null)
            {
                mailFolder.Open(FolderAccess.ReadWrite);
                mailFolder.AddFlags(id, MessageFlags.Deleted, true);
                mailFolder.Expunge();
                mailFolder.Close();
                return(HttpStatusCode.OK);
            }
            else
            {
                return(HttpStatusCode.InternalServerError);
            }
        }
Beispiel #7
0
        private UniqueId GetFirstExistingLockMessage(IMailFolder folder, CancellationToken cancellationToken = default)
        {
            folder.Open(FolderAccess.ReadOnly);
            var lockMessages        = folder.Search(SearchQuery.SubjectContains(LockMessageSubject), cancellationToken);
            var orderedLockMessages = lockMessages?.OrderBy(m => m.Id);

            if (orderedLockMessages.Count() > 1)
            {
                var idToRemove = orderedLockMessages.Last();
                try
                {
                    folder.Open(FolderAccess.ReadWrite);
                    folder.SetFlags(idToRemove, MessageFlags.Deleted, true, cancellationToken);
                    folder.Expunge();
                }
                catch
                {
                    logger.Debug("Failed to remove duplicate lock message with ID {0}; note that this might be ok if another client already removed the duplicate", idToRemove);
                }
            }

            return(orderedLockMessages.First());
        }
        private void checkForMails(object sender, ElapsedEventArgs f)
        {
            if (!m_requester)
            {
                return;
            }

            try
            {
                if (m_debug)
                {
                    m_log.Info("[" + Name + "] checkForMails");
                }

                using (var client = new ImapClient())
                {
                    client.CheckCertificateRevocation = false;
                    client.Timeout = 10000;
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                    if (IMAP_SERVER_SSL == true)
                    {
                        if (m_debug)
                        {
                            m_log.Info("[" + Name + "] Connect SSL");
                        }

                        client.Connect(IMAP_SERVER_HOSTNAME, IMAP_SERVER_PORT, SecureSocketOptions.Auto);
                    }
                    else if (IMAP_SERVER_TLS == true)
                    {
                        if (m_debug)
                        {
                            m_log.Info("[" + Name + "] Connect TLS");
                        }

                        client.Connect(IMAP_SERVER_HOSTNAME, IMAP_SERVER_PORT, SecureSocketOptions.StartTlsWhenAvailable);
                    }
                    else
                    {
                        if (m_debug)
                        {
                            m_log.Info("[" + Name + "] Connect None");
                        }

                        client.Connect(IMAP_SERVER_HOSTNAME, IMAP_SERVER_PORT, SecureSocketOptions.None);
                    }

                    if (IMAP_SERVER_LOGIN != String.Empty && IMAP_SERVER_PASSWORD != String.Empty)
                    {
                        if (m_debug)
                        {
                            m_log.Info("[" + Name + "] Login with " + IMAP_SERVER_LOGIN + ";" + IMAP_SERVER_PASSWORD);
                        }

                        client.Authenticate(IMAP_SERVER_LOGIN, IMAP_SERVER_PASSWORD);
                    }

                    IMailFolder IMAPInbox = client.Inbox;
                    IMAPInbox.Open(FolderAccess.ReadWrite);

                    if (m_debug)
                    {
                        m_log.Info("[" + Name + "] Found " + IMAPInbox.Count + " messages.");
                    }

                    for (int i = 0; i < IMAPInbox.Count; i++)
                    {
                        MimeMessage message = IMAPInbox.GetMessage(i);
                        foreach (MailboxAddress adress in message.To.Mailboxes)
                        {
                            try
                            {
                                if (m_debug)
                                {
                                    m_log.Info("[" + Name + "] Message To: " + adress.Address);
                                    m_log.Info("[" + Name + "] Objekt ID: " + adress.Address.Split('@')[0]);
                                }

                                String UUIDString = adress.Address.Split('@')[0].Trim();

                                if (isUUID(UUIDString))
                                {
                                    UUID            objID       = UUID.Parse(UUIDString);
                                    SceneObjectPart sceneObject = m_scene.GetSceneObjectPart(objID);

                                    if (sceneObject != null)
                                    {
                                        m_messages.Add(new InternalMail(message, objID));
                                        IMAPInbox.SetFlags(i, MessageFlags.Deleted, true);

                                        if (m_debug)
                                        {
                                            m_log.Info("[" + Name + "] Get Message for objekt " + sceneObject.Name + " (" + sceneObject.UUID + ")");
                                        }
                                    }
                                }
                                else
                                {
                                    IMAPInbox.SetFlags(i, MessageFlags.Deleted, true);
                                }
                            }catch (Exception _innerEroor)
                            {
                                m_log.Error("[" + Name + "] " + _innerEroor.Message);
                                IMAPInbox.SetFlags(i, MessageFlags.Deleted, true);
                            }
                        }
                    }

                    IMAPInbox.Expunge();
                    client.Disconnect(true);
                }
            }catch (Exception _error)
            {
                m_log.Error("[" + Name + "] " + _error.Message);
            }
        }
Beispiel #9
0
        private void MailCheck()
        {
            DBConnect     conn          = new DBConnect();
            SqlConnection sqlConnection = new SqlConnection(@"Data Source=" + conn.host + ";Initial Catalog=" + conn.db +
                                                            ";" + "User ID=" + conn.user + ";Password="******"SELECT * FROM Mails WHERE Send_from = '{message.From.ToString().Replace("'","")}' " +
                                                 $"and Subject = '{message.Subject.ToString().Replace("'", "")}' and Send_to = '{message.To.ToString().Replace("'", "")}'";

                        bool kostyl = false;
                        using (DbDataReader reader = sqlCommand.ExecuteReader())
                        {
                            if (!reader.HasRows)
                            {
                                kostyl = true;
                            }
                        }

                        if (kostyl)
                        {
                            SqlCommand command = sqlConnection.CreateCommand();
                            command.CommandText = "INSERT INTO Mails (Send_from, Subject, Date_send, Send_to, Importance, isRead) " +
                                                  $"VALUES ('{message.From.ToString().Replace("'","")}', '{message.Subject.ToString().Replace("'", "")}', " +
                                                  $"'{message.Date.ToString().Replace("'", "")}', '{message.To.ToString().Replace("'", "")}', 'Важное', 0)";

                            command.ExecuteNonQuery();
                            inbox.AddFlags(item.UniqueId, MessageFlags.Deleted, true);
                            inbox.Expunge();
                        }
                    }

                    inbox.Close();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error(ex.ToString());
            }
            finally
            {
                sqlConnection.Close();
                sqlConnection.Dispose();
            }
        }
Beispiel #10
0
 public void ClearDeletedMessages()
 {
     _currentFolder.Expunge();
 }
Beispiel #11
0
 public static void DeleteEmails(ImapClient client, IList <UniqueId> emails, IMailFolder folder)
 {
     folder.AddFlags(emails, MessageFlags.Deleted, false);
     folder.Expunge();
 }