C# 读取指定邮箱的邮件,并自动创建工单

设计思路

1. 写一个邮件服务配置界面,主要包括邮件地址(Email),协议(Protocol),邮件服务器的地址(Email Server),密码(Password),端口(Port),是否使用SSL等。

2. 写windows service程序,定时(通常每小时执行一次)执行一段程序。这段程序首先读取邮件服务配置,然后通过EAGetMail(第三方类库)连接上邮件服务器,读取邮箱中未读邮件(对于非pop3协议) ,根据邮件内容创建工单,最后标记邮件已读或者删除邮件。

通过EAGetMail(第三方类库)读取邮件的示例代码

using System;
using System.Globalization;
using System.IO;
using EAGetMail; //add EAGetMail namespace

namespace receiveemail
{
    class Program
    {
        // Generate an unqiue email file name based on date time
        static string _generateFileName(int sequence)
        {
            DateTime currentDateTime = DateTime.Now;
            return string.Format("{0}-{1:000}-{2:000}.eml",
                currentDateTime.ToString("yyyyMMddHHmmss", new CultureInfo("en-US")),
                currentDateTime.Millisecond,
                sequence);
        }

        static void Main(string[] args)
        {
            try
            {
                // Create a folder named "inbox" under current directory
                // to save the email retrieved.
                string localInbox = string.Format("{0}\\inbox", Directory.GetCurrentDirectory());
                // If the folder is not existed, create it.
                if (!Directory.Exists(localInbox))
                {
                    Directory.CreateDirectory(localInbox);
                }

                MailServer oServer = new MailServer("imap.emailarchitect.net",
                            "test@emailarchitect.net",
                            "testpassword",
                            ServerProtocol.Imap4);

                // Enable SSL/TLS connection, most modern email server require SSL/TLS by default
                oServer.SSLConnection = true;
                oServer.Port = 993;

                // if your server doesn't support SSL/TLS, please use the following codes
                // oServer.SSLConnection = false;
                // oServer.Port = 143;

                MailClient oClient = new MailClient("TryIt");
                oClient.Connect(oServer);

                MailInfo[] infos = oClient.GetMailInfos();
                Console.WriteLine("Total {0} email(s)\r\n", infos.Length);
                for (int i = 0; i < infos.Length; i++)
                {
                    MailInfo info = infos[i];
                    Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}",
                        info.Index, info.Size, info.UIDL);

                    // Receive email from IMAP4 server
                    Mail oMail = oClient.GetMail(info);

                    Console.WriteLine("From: {0}", oMail.From.ToString());
                    Console.WriteLine("Subject: {0}\r\n", oMail.Subject);

                    // Generate an unqiue email file name based on date time.
                    string fileName = _generateFileName(i + 1);
                    string fullPath = string.Format("{0}\\{1}", localInbox, fileName);

                    // Save email to local disk
                    oMail.SaveAs(fullPath, true);

                    // Mark email as deleted from IMAP4 server.
                    oClient.Delete(info);
                }

                // Quit and expunge emails marked as deleted from IMAP4 server.
                oClient.Quit();
                Console.WriteLine("Completed!");
            }
            catch (Exception ep)
            {
                Console.WriteLine(ep.Message);
            }
        }
    }
}

读取邮件常用端口

邮件协议 常用端口 SSL端口
Imap4
143
993
Pop3
110
995
posted @ 2021-07-12 14:15  SilverFox8588  阅读(669)  评论(0编辑  收藏  举报