Notify C# Client, when SMTP Server receive a new Email

前端 未结 3 615
星月不相逢
星月不相逢 2020-12-08 20:55

I want to get all emails in my ASP.NET application that have a certain CC-recipient. To use this for future emails I didn\'t want to polling all the time to get them. But I

相关标签:
3条回答
  • 2020-12-08 21:29

    You can try this:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using EAGetMail; //add EAGetMail namespace
    
    namespace receiveemail
    {
       class Program
       {
           static void Main(string[] args)
           {
                // Create a folder named "inbox" under current directory
                // to save the email retrie enter code here ved.
                string curpath = Directory.GetCurrentDirectory();
                string mailbox = String.Format("{0}\\inbox", curpath);
    
                // If the folder is not existed, create it.
                if (!Directory.Exists(mailbox))
                {
                    Directory.CreateDirectory(mailbox);
                }
    
                // Gmail IMAP4 server is "imap.gmail.com"
                MailServer oServer = new MailServer("imap.gmail.com",
                    "gmailid@gmail.com", "yourpassword", ServerProtocol.Imap4 );
                MailClient oClient = new MailClient("TryIt");
    
                // Set SSL connection,
                oServer.SSLConnection = true;
    
                // Set 993 IMAP4 port
                oServer.Port = 993;
    
                try
                {
                    oClient.Connect(oServer);
                    MailInfo[] infos = oClient.GetMailInfos();
    
                    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);
    
                        // Download email from GMail IMAP4 server
                        Mail oMail = oClient.GetMail(info);
    
                        Console.WriteLine("From: {0}", oMail.From.ToString());
                        Console.WriteLine("Subject: {0}\r\n", oMail.Subject);
    
                        // Generate an email file name based on date time.
                        System.DateTime d = System.DateTime.Now;
                        System.Globalization.CultureInfo cur = new
                            System.Globalization.CultureInfo("en-US");
                        string sdate = d.ToString("yyyyMMddHHmmss", cur);
                        string fileName = String.Format("{0}\\{1}{2}{3}.eml",
                            mailbox, sdate, d.Millisecond.ToString("d3"), i);
    
                        // Save email to local disk
                        oMail.SaveAs(fileName, true);
    
                        // Mark email as deleted in GMail account.
                        oClient.Delete(info);
                    }
    
                    // Quit and purge emails marked as deleted from Gmail IMAP4 server.
                    oClient.Quit();
                }
                catch (Exception ep)
                {
                    Console.WriteLine(ep.Message);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 21:53

    You could send a copy of your emails(i.e. using /etc/aliases file in PostFix) to a MAIL SERVER YOU CAN HANDLE. Once there, you can implement a MAIL PROCESSOR that do whatever you want anytime a mail that MEET CERTAIN CONDITIONS arrives.

    Hope that helps,

    0 讨论(0)
  • 2020-12-08 21:54

    You are approaching this from the wrong angle.

    SMTP does not support receiving mail (never mind PUSH mail). POP3 is what you can use for retrieving mail, but it does not have support for PUSH either (so you would have to pull for mail).

    The IMAP4 IDLE extension is what most refer to as PUSH mail - so you will need to find a library for C# that supports IMAP4 IDLE. I found some information that will get you going in the right direction (no reason to duplicate it here):

    • Using C# .Net Libraries to Check for IMAP Messages
    • Accessing IMAP in C#

    Keep in mind when choosing a solution that it needs to support IDLE. I really like the look of MailSystem.Net as it fulfills your requirements.

    Remember that your mail server also needs to have IMAP4 and IMAP4 IDLE enabled. Some mail servers don't support it, so you might be clean out of luck (and will have to use POP3 pulling).

    0 讨论(0)
提交回复
热议问题