■ POP3 프로토콜을 사용해 메일을 가져오는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
using System; using System.Collections.Generic; using OpenPop.Mime; using OpenPop.Pop3; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { string hostName = "pop.naver.com"; int port = 995; string userName = "testid"; string password = "1234"; using(Pop3Client client = new Pop3Client()) { client.Connect(hostName, port, true); client.Authenticate(userName, password, AuthenticationMethod.UsernameAndPassword); int messageCount = client.GetMessageCount(); for(int i = 1; i <= messageCount; i++) { Message message = client.GetMessage(i); string fromAddress = message.Headers.From.Address; string subject = message.Headers.Subject; string messageBody; MessagePart plainTextVersionMessagePart = message.FindFirstPlainTextVersion(); if(plainTextVersionMessagePart == null) { MessagePart htmlVersionMessagePart = message.FindFirstHtmlVersion(); messageBody = htmlVersionMessagePart.GetBodyAsText(); } else { messageBody = plainTextVersionMessagePart.GetBodyAsText(); } List<MessagePart> attachmentMessagePartList = message.FindAllAttachments(); Console.WriteLine($"메시지 ID : {message.Headers.MessageId }"); Console.WriteLine($"발송자 주소 : {fromAddress }"); Console.WriteLine($"제목 : {subject }"); //Console.WriteLine($"본문 : {messageBody }"); Console.WriteLine($"첨부 파일 수 : {attachmentMessagePartList.Count}"); //foreach(MessagePart attachmentMessagePart in attachmentMessagePartList) //{ // Console.WriteLine("첨부 파일 : " + attachmentMessagePart.FileName); // // FileInfo targetFileInfo = new FileInfo($"D:\\temp\\{attachmentMessagePart.FileName}"); // // attachmentMessagePart.Save(targetFileInfo); //} Console.WriteLine(); } Console.WriteLine("메일 건수 : " + messageCount); } } #endregion } } |