コード例 #1
0
        public void TestAuthenticationExceptions()
        {
            var commands = new List <Pop3ReplayCommand> ();

            commands.Add(new Pop3ReplayCommand("", "comcast.greeting.txt"));
            commands.Add(new Pop3ReplayCommand("CAPA\r\n", "comcast.capa1.txt"));
            commands.Add(new Pop3ReplayCommand("USER username\r\n", "comcast.ok.txt"));
            commands.Add(new Pop3ReplayCommand("PASS password\r\n", "comcast.err.txt"));
            commands.Add(new Pop3ReplayCommand("QUIT\r\n", "comcast.quit.txt"));

            using (var client = new Pop3Client()) {
                try {
                    client.ReplayConnect("localhost", new Pop3ReplayStream(commands, false), CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Connect: {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "Client failed to connect.");

                Assert.AreEqual(ComcastCapa1, client.Capabilities);
                Assert.AreEqual(0, client.AuthenticationMechanisms.Count);
                Assert.AreEqual(31, client.ExpirePolicy);

                try {
                    var credentials = new NetworkCredential("username", "password");
                    client.Authenticate(credentials, CancellationToken.None);
                    Assert.Fail("Expected AuthenticationException");
                } catch (AuthenticationException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Authenticate: {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "AuthenticationException should not cause a disconnect.");

                try {
                    var sizes = client.GetMessageSizes(CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Count: {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    var sizes = client.GetMessageSizes(CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessageSizes: {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    var size = client.GetMessageSize("uid", CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessageSize(uid): {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    var size = client.GetMessageSize(0, CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessageSize(int): {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    var uids = client.GetMessageUids(CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessageUids: {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    var uid = client.GetMessageUid(0, CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessageUid: {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    var message = client.GetMessage("uid", CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessage(uid): {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    var message = client.GetMessage(0, CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in GetMessage(int): {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    client.DeleteMessage("uid", CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in DeleteMessage(uid): {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    client.DeleteMessage(0, CancellationToken.None);
                    Assert.Fail("Expected ServiceNotAuthenticatedException");
                } catch (ServiceNotAuthenticatedException) {
                    // we expect this exception...
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in DeleteMessage(int): {0}", ex);
                }

                Assert.IsTrue(client.IsConnected, "ServiceNotAuthenticatedException should not cause a disconnect.");

                try {
                    client.Disconnect(true, CancellationToken.None);
                } catch (Exception ex) {
                    Assert.Fail("Did not expect an exception in Disconnect: {0}", ex);
                }

                Assert.IsFalse(client.IsConnected, "Failed to disconnect");
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: hontsev/EmailGetter
        /// <summary>
        /// 将邮件列表内容存入excel
        /// </summary>
        private void workOutputExcel()
        {
            Invoke(updateState, (object)SystemState.working);
            Invoke(printstr, (object)("开始输出邮件数据到excel:" + excelFileName));
            try
            {
                string excelPath = excelFileName.Substring(0, excelFileName.LastIndexOf("\\")) + "\\";
                string attPath   = excelPath + @"邮件附件\";
                if (!Directory.Exists(attPath))
                {
                    Directory.CreateDirectory(attPath);
                }
                Invoke(printstr, (object)("附件内容输出到:" + attPath));
                IWorkbook  wb     = new HSSFWorkbook();
                ICellStyle style1 = wb.CreateCellStyle();
                IFont      font1  = wb.CreateFont();
                font1.Underline = FontUnderlineType.None;
                font1.Color     = HSSFColor.Black.Index;
                style1.SetFont(font1);
                ICellStyle style2 = wb.CreateCellStyle();
                IFont      font2  = wb.CreateFont();
                font2.Underline = FontUnderlineType.Single;
                font2.Color     = HSSFColor.Blue.Index;
                style2.SetFont(font2);
                string[] strHead     = new string[] { "收信日期", "标题", "发信人", "收信人", "抄送", "正文", "附件" };
                int[]    columnWidth = new int[] { 21, 40, 15, 15, 15, 30, 30 };
                ISheet   sheet1      = wb.CreateSheet();
                var      headRow     = sheet1.CreateRow(0);
                for (int i = 0; i < columnWidth.Length; i++)
                {
                    var tcell = headRow.CreateCell(i);
                    tcell.SetCellValue(strHead[i]);
                    tcell.CellStyle = style1;
                    sheet1.SetColumnWidth(i, columnWidth[i] * 256);
                }
                OpenPop.Mime.Message message;
                for (int i = 0; i < emails.Count; i++)
                {
                    //向excel文件中添加项
                    var   contentRow = sheet1.CreateRow(i + 1);
                    ICell tcell;
                    tcell = contentRow.CreateCell(0);
                    tcell.SetCellValue(emails[i].Date);
                    tcell.CellStyle = style1;
                    tcell           = contentRow.CreateCell(1);
                    tcell.SetCellValue(emails[i].Subject);
                    tcell.CellStyle = style1;
                    tcell           = contentRow.CreateCell(2);
                    tcell.SetCellValue(emails[i].From);
                    tcell.CellStyle = style1;
                    tcell           = contentRow.CreateCell(3);
                    tcell.SetCellValue(emails[i].To);
                    tcell.CellStyle = style1;
                    tcell           = contentRow.CreateCell(4);
                    tcell.SetCellValue(emails[i].Cc);
                    tcell.CellStyle = style1;
                    string tmp = emails[i].Content;
                    if (tmp.Length > 1000)
                    {
                        tmp = tmp.Substring(0, 1000) + "****内容未完****";
                    }
                    tcell = contentRow.CreateCell(5);
                    tcell.SetCellValue(tmp);
                    tcell.CellStyle = style1;

                    //存储附件
                    if (emailClient.GetMessageSize(emails[i].no) > 10 * 1024 * 1024)
                    {
                        //邮件大于10m不接收附件
                        tcell = contentRow.CreateCell(6);
                        tcell.SetCellValue("****附件过大,请去邮箱自行查看****");
                        tcell.CellStyle = style1;
                        Invoke(printstr, (object)(String.Format("保存第 {0} 封邮件 ,附件过大无法下载。", i + 1)));
                    }
                    else
                    {
                        message = emailClient.GetMessage(emails[i].no);
                        List <MessagePart> attachments = message.FindAllAttachments();
                        int attno = 0;
                        foreach (MessagePart attachment in attachments)
                        {
                            attno++;
                            string   filename = attPath + "[" + emails[i].no + attachments.Count + "] - " + attachment.FileName;
                            FileInfo fi       = new FileInfo(filename);
                            attachment.Save(fi);
                            HSSFHyperlink link = new HSSFHyperlink(HyperlinkType.Url);
                            link.Address = filename;
                            //link.Address = ToUnicode(filename);
                            ICell cell = contentRow.CreateCell(5 + attno, CellType.Blank);
                            cell.SetCellValue(attachment.FileName);
                            cell.Hyperlink = link;
                            cell.CellStyle = style2;
                        }
                        if (attno == 0)
                        {
                            //无附件
                            tcell = contentRow.CreateCell(6);
                            tcell.SetCellValue("无");
                            tcell.CellStyle = style1;
                        }
                        Invoke(printstr, (object)(String.Format("保存第 {0} 封邮件 , {1} 个附件", i + 1, attachments.Count)));
                    }
                }
                FileStream file = new FileStream(excelFileName, FileMode.Create, FileAccess.ReadWrite);
                wb.Write(file);
                file.Close();
                file.Dispose();
                Invoke(printstr, (object)("excel文件输出完毕。请查看:" + excelFileName));
                Invoke(updateState, (object)SystemState.endwork);
            }
            catch (Exception e)
            {
                Invoke(printstr, (object)("出错,错误码:" + e.Message));
                Invoke(updateState, (object)SystemState.endwork);
            }
        }
コード例 #3
0
 public void TestGetMessageSize()
 {
     Connect();
     Assert.Throws(typeof(InvalidUseException), delegate { Client.GetMessageSize(RandomMessageNumber); });
 }
コード例 #4
0
 public void TestGetMessageSizeDoesNotThrow()
 {
     Authenticate("+OK 5 0");             // Message 5 has size is 0
     Assert.DoesNotThrow(delegate { Client.GetMessageSize(RandomMessageNumber); });
 }