コード例 #1
0
        protected override void Execute(CodeActivityContext context)
        {
            string      username        = Email.Get(context);          //发送端账号
            string      password        = Password.Get(context);       //发送端密码(这个客户端重置后的密码)
            string      server          = Server.Get(context);         //邮件服务器
            Int32       port            = Port.Get(context);           //端口号
            string      mailFolderTo    = MailFolderTo.Get(context);   //目标文件夹
            string      mailFolderFrom  = MailFolderFrom.Get(context); //源文件夹
            MimeMessage mailMoveMessage = MailMoveMessage.Get(context);

            ImapClient  client = new ImapClient();
            SearchQuery query;

            try
            {
                client.Connect(server, port, SecureConnection);
                client.Authenticate(username, password);

                if (EnableSSL)
                {
                    client.SslProtocols = System.Security.Authentication.SslProtocols.Ssl3;
                }

                query = SearchQuery.All;
                List <IMailFolder> mailFolderList = client.GetFolders(client.PersonalNamespaces[0]).ToList();
                IMailFolder        fromFolder     = client.GetFolder(mailFolderFrom);
                IMailFolder        toFolder       = client.GetFolder(mailFolderTo);
                fromFolder.Open(FolderAccess.ReadWrite);
                IList <UniqueId>   uidss  = fromFolder.Search(query);
                List <MailMessage> emails = new List <MailMessage>();
                for (int i = uidss.Count - 1; i >= 0; i--)
                {
                    MimeMessage message = fromFolder.GetMessage(new UniqueId(uidss[i].Id));
                    if (message.Date == mailMoveMessage.Date &&
                        message.MessageId == mailMoveMessage.MessageId &&
                        message.Subject == mailMoveMessage.Subject)
                    {
                        fromFolder.MoveTo(new UniqueId(uidss[i].Id), toFolder);
                        break;
                    }
                }
                client.Disconnect(true);
            }
            catch (Exception e)
            {
                client.Disconnect(true);
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, "IMAP移动邮件失败", e.Message);
            }
        }
コード例 #2
0
ファイル: Provider.cs プロジェクト: Matioz/JanuszMail
        public HttpStatusCode MoveEmailToFolder(UniqueId id, string folderSrc, string folderDst)
        {
            if (!IsAuthenticated())
            {
                return(HttpStatusCode.ExpectationFailed);
            }
            IMailFolder mailFolder = GetFolder(folderSrc);

            if (mailFolder != null)
            {
                mailFolder.Open(FolderAccess.ReadWrite);
                mailFolder.MoveTo(id, GetFolder(folderDst));
                return(HttpStatusCode.OK);
            }
            else
            {
                return(HttpStatusCode.InternalServerError);
            }
        }
コード例 #3
0
        public void StarEmail(MailInfo mail)
        {
            using (ImapClient client = new ImapClient())
            {
                client.Connect(mailServer, port, ssl);
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.Authenticate(UserData.Email, UserData.Password);

                IMailFolder folder = client.GetFolder(mail.ParentFolder.FullName);
                folder.Open(FolderAccess.ReadWrite);

                var folders = client.GetFolders(new FolderNamespace('/', "[Gmail]"));
                foreach (var item in folders)
                {
                    if (item.FullName.Equals(@"[Gmail]/Csillagozott") || item.FullName.Equals(@"[Gmail]/Starred"))
                    {
                        folder.MoveTo(mail.Uid, client.GetFolder(item.FullName));
                    }
                }

                folder.Close();
                client.Disconnect(true);
            }
        }
コード例 #4
0
        /// <summary>
        /// Move the email with the given unique identifier to the destination mailbox
        /// </summary>
        /// <param name="uid">The unique identifier for the email</param>
        /// <param name="destinationMailbox">The destination mailbox</param>
        /// <example>
        /// <code source = "../EmailUnitTests/EmailUnitWithDriver.cs" region="MoveMessage" lang="C#" />
        /// </example>
        public virtual void MoveMailMessage(string uid, string destinationMailbox)
        {
            IMailFolder folder = this.GetCurrentFolder();

            folder.MoveTo(new UniqueId(uint.Parse(uid)), this.EmailConnection.GetFolder(destinationMailbox));
        }
コード例 #5
0
        private void ProcessRules(IMailFolder inbox) {
            inbox.Open(FolderAccess.ReadWrite);
            inbox.Status(StatusItems.Unread);

            if (inbox.Unread > 0) {
                var unreadMessageUids = inbox.Search(SearchQuery.NotSeen);
                var toMove = new Dictionary<string, List<UniqueId>>();
                var markAsRead = new List<UniqueId>();

                // process unread messages
                foreach (var unreadMessageUid in unreadMessageUids) {
                    var message = inbox.GetMessage(unreadMessageUid);

                    var matchingRule = GetMatchingRule(message);
                    if (matchingRule != null) {
                        if (!toMove.ContainsKey(matchingRule.Destination)) {
                            toMove.Add(matchingRule.Destination, new List<UniqueId>());
                        }

                        toMove[matchingRule.Destination].Add(unreadMessageUid);

                        if (matchingRule.MarkAsRead) {
                            markAsRead.Add(unreadMessageUid);
                        }
                    }
                }

                // mark as read
                if (markAsRead.Any()) {
                    inbox.AddFlags(markAsRead, MessageFlags.Seen, true);
                }

                // move to destination
                if (toMove.Any()) {
                    foreach (var destination in toMove.Keys) {
                        inbox.MoveTo(toMove[destination], inbox.GetSubfolder(destination));
                    }
                }
            }
        }
コード例 #6
0
        public override void RunCommand(object sender)
        {
            var         engine                 = (AutomationEngineInstance)sender;
            MimeMessage vMimeMessage           = (MimeMessage)v_IMAPMimeMessage.ConvertUserVariableToObject(engine);
            string      vIMAPHost              = v_IMAPHost.ConvertUserVariableToString(engine);
            string      vIMAPPort              = v_IMAPPort.ConvertUserVariableToString(engine);
            string      vIMAPUserName          = v_IMAPUserName.ConvertUserVariableToString(engine);
            string      vIMAPPassword          = v_IMAPPassword.ConvertUserVariableToString(engine);
            var         vIMAPDestinationFolder = v_IMAPDestinationFolder.ConvertUserVariableToString(engine);

            using (var client = new ImapClient())
            {
                client.ServerCertificateValidationCallback = (sndr, certificate, chain, sslPolicyErrors) => true;
                client.SslProtocols = SslProtocols.None;

                using (var cancel = new CancellationTokenSource())
                {
                    try
                    {
                        client.Connect(v_IMAPHost, int.Parse(v_IMAPPort), true, cancel.Token); //SSL
                    }
                    catch (Exception)
                    {
                        client.Connect(v_IMAPHost, int.Parse(v_IMAPPort)); //TLS
                    }

                    client.AuthenticationMechanisms.Remove("XOAUTH2");
                    client.Authenticate(vIMAPUserName, vIMAPPassword, cancel.Token);

                    var      splitId   = vMimeMessage.MessageId.Split('#').ToList();
                    UniqueId messageId = UniqueId.Parse(splitId.Last());
                    splitId.RemoveAt(splitId.Count - 1);
                    string messageFolder = string.Join("", splitId);

                    IMailFolder toplevel               = client.GetFolder(client.PersonalNamespaces[0]);
                    IMailFolder foundSourceFolder      = GetIMAPEmailsCommand.FindFolder(toplevel, messageFolder);
                    IMailFolder foundDestinationFolder = GetIMAPEmailsCommand.FindFolder(toplevel, vIMAPDestinationFolder);

                    if (foundSourceFolder != null)
                    {
                        foundSourceFolder.Open(FolderAccess.ReadWrite, cancel.Token);
                    }
                    else
                    {
                        throw new Exception("Source Folder not found");
                    }

                    if (foundDestinationFolder == null)
                    {
                        throw new Exception("Destination Folder not found");
                    }

                    var messageSummary = foundSourceFolder.Fetch(new[] { messageId }, MessageSummaryItems.Flags);

                    if (v_IMAPOperationType == "Move MimeMessage")
                    {
                        if (v_IMAPMoveCopyUnreadOnly == "Yes")
                        {
                            if (!messageSummary[0].Flags.Value.HasFlag(MessageFlags.Seen))
                            {
                                foundSourceFolder.MoveTo(messageId, foundDestinationFolder, cancel.Token);
                            }
                        }
                        else
                        {
                            foundSourceFolder.MoveTo(messageId, foundDestinationFolder, cancel.Token);
                        }
                    }
                    else if (v_IMAPOperationType == "Copy MimeMessage")
                    {
                        if (v_IMAPMoveCopyUnreadOnly == "Yes")
                        {
                            if (!messageSummary[0].Flags.Value.HasFlag(MessageFlags.Seen))
                            {
                                foundSourceFolder.CopyTo(messageId, foundDestinationFolder, cancel.Token);
                            }
                        }
                        else
                        {
                            foundSourceFolder.CopyTo(messageId, foundDestinationFolder, cancel.Token);
                        }
                    }

                    client.Disconnect(true, cancel.Token);
                    client.ServerCertificateValidationCallback = null;
                }
            }
        }