Example #1
0
        public void PopTest()
        {

            using (PopClient popClient = new PopClient("pop.lsong.org"))
            {
                popClient.Connect();
                popClient.Authenticate("*****@*****.**", "lsong940");

                int messageCount = popClient.GetMessageCount();
                for (int i = messageCount; i > 0; i--)
                {
                    MessageHeader messageHeaders = popClient.GetMessageHeaders(i);
                }
            }
        }
Example #2
0
        public void MailTest()
        {

            using (SmtpClient smtpClient = new SmtpClient("smtp.163.com"))
            {
                smtpClient.Connected += (x, y) => _logger.Debug("mail", "Connected:{0}", y);
                smtpClient.Authenticated += (x, y) => _logger.Debug("mail", "Authenticated:{0}", y);
                smtpClient.StartedMessageTransfer += (x, y) => _logger.Debug("mail", "StartedMessageTransfer:{0}", y);
                smtpClient.EndedMessageTransfer += (x, y) => _logger.Debug("mail", "EndedMessageTransfer:{0}", y);
                smtpClient.Disconnected += (x, y) => _logger.Debug("mail", "Disconnected:{0}", y);

                smtpClient.Connect();

                smtpClient.UserName = "******";
                smtpClient.Password = "******";

                smtpClient.Authenticate("*****@*****.**", "*****@*****.**");

                MailAddress from = new MailAddress("Lsong", "*****@*****.**");
                MailAddress to = new MailAddress("*****@*****.**");
                MailAddress cc = new MailAddress("Test<*****@*****.**>");

                MailMessage mailMessage = new MailMessage(from, to);
                mailMessage.AddRecipient(cc, AddressType.Cc);
                mailMessage.AddRecipient("*****@*****.**", AddressType.Bcc);

                mailMessage.Charset = "UTF-8";
                mailMessage.Priority = MailPriority.High;
                mailMessage.Notification = true;

                mailMessage.AddCustomHeader("X-CustomHeader", "Value");
                mailMessage.AddCustomHeader("X-CompanyName", "Value");

                //string testCid = mailMessage.AddImage("C:\\test.bmp");

                //mailMessage.AddAttachment("C:\\test.zip");

                mailMessage.Subject = "This's a test Mail.";
                mailMessage.Body = "hello everybody .";
                mailMessage.HtmlBody =
                    string.Format("<html><body>hello everybody .<br /><img src='cid:{0}' /></body></html>", "");

                smtpClient.SendMail(mailMessage);
            }

            using (PopClient popClient = new PopClient("pop.163.com"))
            {
                popClient.UserName = "";
                popClient.Password = "";
                popClient.Connect("pop.163.com", 110, false);
                popClient.Authenticate("*****@*****.**", "*****@*****.**");

                int messageCount = popClient.GetMessageCount();
                for (int i = messageCount; i > 1; i--)
                {
                    //try
                    //{
                    MessageHeader messageHeader = popClient.GetMessageHeaders(i);
                    MailAddress sender = messageHeader.Sender;
                    MailAddress from = messageHeader.From;
                    List<MailAddress> to = messageHeader.To;
                    string subject = messageHeader.Subject;

                    _logger.Debug("mail", subject);
                    if (sender != null)
                    {
                        _logger.Info("mail", "Sender:{0}", sender);
                    }
                    if (from != null)
                    {
                        _logger.Info("mail", "From:{0}", from);
                    }
                    if (to != null)
                    {
                        foreach (MailAddress mailAddress in to)
                        {
                            _logger.Info("mail", "TO:{0}", mailAddress);
                        }
                    }
                    Message message = popClient.GetMessage(i);
                    MessagePart textBody = message.FindFirstPlainTextVersion();
                    MessagePart htmlBody = message.FindFirstHtmlVersion();


                    if (textBody != null)
                    {
                        string text = textBody.GetBodyAsText();
                        System.Console.WriteLine(text);
                    }else if (htmlBody != null)
                    {
                        string html = htmlBody.GetBodyAsText();
                        System.Console.WriteLine(html);
                    }
                    System.Console.ReadKey();
                    //}
                    //catch (Exception exception)
                    //{
                    //    _logger.Error("mail", exception);
                    //}
                }
            }

        }