예제 #1
0
        /// <summary>
        /// Example showing:
        ///  - how to fetch all messages from a POP3 server
        /// </summary>
        /// <param name="hostname">Hostname of the server. For example: pop3.live.com</param>
        /// <param name="port">Host port to connect to. Normally: 110 for plain POP3, 995 for SSL POP3</param>
        /// <param name="useSsl">Whether or not to use SSL to connect to server</param>
        /// <param name="username">Username of the user on the server</param>
        /// <param name="password">Password of the user on the server</param>
        /// <returns>All Messages on the POP3 server</returns>
        public void ProcessNewMessages(string hostname, int port, bool useSsl, string username, string password)
        {
            // The client disconnects from the server when being disposed
            using (Pop3Client client = new Pop3Client())
            {
                // Connect to the server
                client.Connect(hostname, port, useSsl);

                // Authenticate ourselves towards the server
                client.Authenticate(username, password);

                // Get the number of messages in the inbox
                int messageCount = client.GetMessageCount();

                // We don't want to download all messages
                // List<Message> allMessages = new List<Message>(messageCount);
                List<string> uids = client.GetMessageUids();

                // Messages are numbered in the interval: [1, messageCount]
                // Ergo: message numbers are 1-based.
                for (int i = 1; i <= messageCount; i++)
                {
                    String uid = client.GetMessageUid(i);
                    if (seenUids.Contains(uid))
                    {
                        continue;
                    }

                    Message m = client.GetMessage(i);
                    if (m.MessagePart != null)
                    {
                        if (m.MessagePart.MessageParts != null)
                        {
                            for (int j = 0; j < m.MessagePart.MessageParts.Count; j++)
                            {
                                //if (m.MessagePart.MessageParts[j].Body != null)
                                //{
                                //    System.Console.Write(ASCIIEncoding.ASCII.GetString(m.MessagePart.MessageParts[j].Body));
                                //}

                                if (((m.MessagePart.MessageParts[j].IsAttachment) &&
                                    (m.MessagePart.MessageParts[j].FileName == "invite.ics")) ||
                                    ((m.MessagePart.MessageParts[j].ContentType != null) &&
                                     (m.MessagePart.MessageParts[j].ContentType.MediaType.StartsWith("text/calendar"))))
                                {
                                    //System.Console.Write(ASCIIEncoding.ASCII.GetString(m.MessagePart.MessageParts[j].Body));
                                    List<String> lines = ParseICalendar(m.MessagePart.MessageParts[j].Body);

                                    // Then find the relevant lines.
                                    // Make sure this is a VCALENDAR attachment.
                                    if ((lines != null) && lines[0].StartsWith("BEGIN:VCALENDAR"))
                                    {
                                        Meeting meeting = new Meeting();
                                        ExtractAttributes(lines, meeting);
                                        String json = meeting.ToJson();
                                        System.Console.WriteLine("Processing Message: " + uid);
                                        System.Console.WriteLine("JSON: " + json);
                                        seenUids.Add(uid);
                                        //PostJsonToWebServer(json);
                                    }
                                }
                            }
                        }
                    }
                    //System.Console.Write(ASCIIEncoding.ASCII.GetString(m.MessagePart.MessageParts[0].Body));
                    //allMessages.Add(m);
                }
            }
        }
		public void TestGetMessageUid()
		{
			const string welcomeMessage = "+OK";
			const string okUsername = "******";
			const string okPassword = "******";
			const string messageUidResponse = "+OK 2 psycho"; // Message 2 has UID psycho
			const string serverResponses = welcomeMessage + "\r\n" + okUsername + "\r\n" + okPassword + "\r\n" + messageUidResponse + "\r\n";
			
			Stream inputStream = new MemoryStream(Encoding.ASCII.GetBytes(serverResponses));
			Stream outputStream = new MemoryStream();

			Pop3Client client = new Pop3Client();
			client.Connect(new CombinedStream(inputStream, outputStream));
			client.Authenticate("test", "test");

			const string expectedOutput = "psycho";

			// Delete all the messages
			string output = client.GetMessageUid(2);

			// We now expect that the client has given us the correct UID
			Assert.AreEqual(expectedOutput, output);
		}
        private string pop3recievecontent(string gmailuid)
        {
            Pop3Client pop3Client = null;
            pop3Client = new Pop3Client();
            pop3Client.Connect("pop.gmail.com", 995, true);
            pop3Client.Authenticate("*****@*****.**", "chp@2015", AuthenticationMethod.TryBoth);
            int count = pop3Client.GetMessageCount();
            this.Emails = new List<Email1>();
            int counter = 0;
            for (int j = count; j >= 1; j--)
            {
                Message message = pop3Client.GetMessage(j);
                string hh1 = pop3Client.GetMessageUid(j);

                if (gmailuid == hh1)
                {
                    Email1 email1 = new Email1()
                    {
                        MessageNumber = j,
                        Subject = message.Headers.Subject,
                        DateSent = message.Headers.DateSent,

                        From = string.Format("<a href = 'mailto:{1}'>{0}</a>", message.Headers.From.DisplayName, message.Headers.From.Address),
                    };

                    MessagePart body = message.FindFirstHtmlVersion();

                    if (body != null)
                    {
                        email1.Body = body.GetBodyAsText();
                        return email1.Body;
                    }
                    else
                    {
                        body = message.FindFirstPlainTextVersion();

                        if (body != null)
                        {
                            email1.Body = body.GetBodyAsText();
                            return email1.Body;
                        }
                    }

                    this.Emails.Add(email1);
                }
                counter++;
            }
            return "";
        }