C# 메일 수신과 삭제(OpenPoP.NET)

C# 메일 수신과 삭제(OpenPoP.NET)

메일 수신과 삭제 기능의 호출 부분

using (Pop3Client pop3Client = GetPop3Client())
{
    var pop3 = new Pop3Mail();
    List<MailInfo> receivedMailList = pop3.ReceiveMails(pop3Client);
    pop3.DeleteMessageByMessageId(client, 1); // MessageId
}

Pop3Client생성

public static Pop3Client GetPop3Client()
{
    string host = ConfigurationManager.AppSettings["pop.host"];
    int port = Convert.ToInt32(ConfigurationManager.AppSettings["pop.port"]);
    string username = ConfigurationManager.AppSettings["pop.username"];
    string password = ConfigurationManager.AppSettings["pop.password"];

    var client = new Pop3Client();
    client.Connect(host, port, true);
    client.Authenticate(username, password);

    return client;
}

Web.config or App.config

<appSettings>
  <add key="pop.host" value="host" />
  <add key="pop.port" value="port" />
  <add key="pop.username" value="username" />
  <add key="pop.password" value="password" />
</appSettings>

메일 수신 & 삭제용 메소드

using OpenPop.Mime;
using OpenPop.Pop3;
using System;
using System.Collections.Generic;
using System.Configuration;
using Message = OpenPop.Mime.Message;

public class Pop3Mail {

public List<MailInfo> ReceiveMails(Pop3Client client)
{   
    var receivedMailList = new List<MailInfo>();
    int messageCount = client.GetMessageCount();
    string subject = string.Empty;
    string body = string.Empty;

    for (int i = messageCount; i >= 1; i -= 1)
    {
        Message message = client.GetMessage(i);
        MessagePart plainTextPart = message.FindFirstPlainTextVersion();
        subject = message.Headers.Subject;
        body = plainTextPart.GetBodyAsText();

        var mailInfo = new MailInfo();
        mailInfo.CurrentSubject = subject;
        mailInfo.CurrentBody = body;
        mailInfo.CurrentMessageID = message.Headers.MessageId;
        receivedMailList.Add(mailInfo);
    }
    return receivedMailList;
}

public void DeleteMessageByMessageId(Pop3Client client, string messageId)
{
    try
    {
        int messageCount = client.GetMessageCount();

        for (int messageItem = messageCount; messageItem > 0; messageItem--)
        {
            if (client.GetMessageHeaders(messageItem).MessageId == messageId)
            {
                client.DeleteMessage(messageItem);
            }
        }
    }
    catch (Exception e)
    {
        // 메일이 이미 삭제된 경우엔 에러 처리에서 제외한다
        if (e.Message.IndexOf("ERR Message is deleted") == -1)
        {
            throw new Exception("메일 삭제 도중에 문제가 발생했습니다.");
        }
    }
}
}

메일 수신 내용 격납용 클래스

public class MailInfo
{
    public string CurrentMessageID { get; set; }
    public string CurrentSubject { get; set; }
    public string CurrentBody { get; set; }
    public string PreviousMessageID { get; set; }
    public string PreviousSubject { get; set; }
    public string PreviousBody { get; set; }
}

참고 사이트

Open Pop.NET

Pie's Tech Note

생계형 개발자의 메모장

comments powered by Disqus

    rss facebook twitter github youtube mail spotify instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora