Example #1
0
        public static void Run()
        {
            // ExStart:SendingBulkEmails
            // Create SmtpClient as client and specify server, port, user name and password
            SmtpClient client = new SmtpClient("mail.server.com", 25, "Username", "Password");

            // Create instances of MailMessage class and Specify To, From, Subject and Message
            MailMessage message1 = new MailMessage("*****@*****.**", "*****@*****.**", "Subject1", "message1, how are you?");
            MailMessage message2 = new MailMessage("*****@*****.**", "*****@*****.**", "Subject2", "message2, how are you?");
            MailMessage message3 = new MailMessage("*****@*****.**", "*****@*****.**", "Subject3", "message3, how are you?");

            // Create an instance of MailMessageCollection class
            MailMessageCollection manyMsg = new MailMessageCollection();

            manyMsg.Add(message1);
            manyMsg.Add(message2);
            manyMsg.Add(message3);

            // Use client.BulkSend function to complete the bulk send task
            try
            {
                // Send Message using BulkSend method
                client.Send(manyMsg);
                Console.WriteLine("Message sent");
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            // ExEnd:SendingBulkEmails
        }
        public static void Run()
        {
            // ExStart:SendingBulkEmails
            // Create SmtpClient as client and specify server, port, user name and password
            SmtpClient client = new SmtpClient("mail.server.com", 25, "Username", "Password");

            // Create instances of MailMessage class and Specify To, From, Subject and Message
            MailMessage message1 = new MailMessage("*****@*****.**", "*****@*****.**", "Subject1", "message1, how are you?");
            MailMessage message2 = new MailMessage("*****@*****.**", "*****@*****.**", "Subject2", "message2, how are you?");
            MailMessage message3 = new MailMessage("*****@*****.**", "*****@*****.**", "Subject3", "message3, how are you?");

            // Create an instance of MailMessageCollection class
            MailMessageCollection manyMsg = new MailMessageCollection();
            manyMsg.Add(message1);
            manyMsg.Add(message2);
            manyMsg.Add(message3);

            // Use client.BulkSend function to complete the bulk send task
            try
            {
                // Send Message using BulkSend method
                client.Send(manyMsg);                
                Console.WriteLine("Message sent");
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
            // ExEnd:SendingBulkEmails
        }
Example #3
0
        public static void TestReceiveEmail()
        {
            try
            {
                Imap imp = new Imap();
                imp.Connect("imap.126.com");
                imp.Login("*****@*****.**", "Aa00000000");
                imp.SelectFolder("Inbox");
                MailMessageCollection msgs = imp.DownloadMessageHeaders(Imap.AllMessages, false);
                // POP3 version: msgs = pop.DownloadMessageHeaders();
                imp.Disconnect();

                foreach (MailMessage msg in msgs)
                {
                    Console.WriteLine("From: " + msg.From.ToString());
                    Console.WriteLine("To: " + msg.To.ToString());
                    Console.WriteLine("Subject: " + msg.Subject);
                    Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Example #4
0
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load message collection.
        MailMessageCollection messages = MailMessageCollection.Load("Collection.mbox");

        // Display message information.
        foreach (MailMessage message in messages)
        {
            Console.WriteLine($"From: {message.From}");
            Console.WriteLine($"Subject: {message.Subject}");
            Console.WriteLine($"Body: {message.BodyText}");
            Console.WriteLine();
        }

        // Create new message.
        MailMessage newMessage = new MailMessage("*****@*****.**", "*****@*****.**")
        {
            Subject  = "Test email message with a text body",
            BodyText = "This is a test message with a text body."
        };

        // Add message to collection.
        messages.Add(newMessage);

        // Save message collection.
        messages.Save("Modified Collection.mbox");
    }
Example #5
0
        private void GetNewMessagesFromServerAndSaveToDb(DbStorage dbStorage, Folder fld, int[] Indexes, out bool errorOccured, ref ArrayList pop3Uids)
        {
            _folderName = Utils.GetLocalizedFolderNameByType(fld);
            _msgNumber  = 1;
            _msgsCount  = Indexes.Length;
            MailMessageCollection mailMessageCollection = new MailMessageCollection();

//			const int maxMsgsPersession = Constants.DownloadChunk;
            try
            {
                foreach (int index in Indexes)
                {
//                    int downloadMsgsCount = ((_msgsCount) > maxMsgsPersession) ? maxMsgsPersession : _msgsCount;
                    if ((_msgsCount) > 0)
                    {
                        if ((fld.SyncType == FolderSyncType.NewEntireMessages) ||
                            (fld.SyncType == FolderSyncType.AllEntireMessages))
                        {
                            mailMessageCollection.Add(_pop3Obj.DownloadEntireMessage(index));
//							mailMessageCollection = _pop3Obj.DownloadEntireMessages(newMsgStartIndex + 1, downloadMsgsCount);
                        }
                        if ((fld.SyncType == FolderSyncType.NewHeadersOnly) ||
                            (fld.SyncType == FolderSyncType.AllHeadersOnly))
                        {
                            mailMessageCollection.Add(_pop3Obj.DownloadMessageHeader(index));
                            //							mailMessageCollection = _pop3Obj.DownloadMessageHeaders(newMsgStartIndex + 1, downloadMsgsCount);
                        }
                        if (!_updatedFolders.ContainsKey(fld.ID))
                        {
                            _updatedFolders.Add(fld.ID, fld.FullPath);
                        }
                    }
                }
                WebMailMessageCollection coll = new WebMailMessageCollection(_account, mailMessageCollection, true, fld);
                ApplyXSpam(coll);
                ApplyFilters(coll, dbStorage, fld, ref pop3Uids);
                if (_account.MailMode == MailMode.DeleteMessagesFromServer)
                {
                    if ((fld.SyncType == FolderSyncType.AllHeadersOnly) ||
                        (fld.SyncType == FolderSyncType.NewHeadersOnly))
                    {
                        Log.WriteLine("GetNewMessagesFromServerAndSaveToDb", "Incorrect Settings: " + _account.MailMode.ToString() + " + " + fld.SyncType.ToString());
                    }
                    else
                    {
                        DeleteMessages(coll.ToUidsCollection(true), null);
                    }
                }
                errorOccured = false;
            }
            catch (Exception ex)
            {
                errorOccured = true;
                Log.WriteException(ex);
                throw new WebMailException(ex);
            }
            _folderName = "";
            _msgNumber  = 0;
            _msgsCount  = 0;
        }
        private bool FetchMaessages(Imap imap)
        {
            UidCollection uids = (UidCollection)imap.Search(true, "UNSEEN", null);

            Log.Info("Unseen email count: {0}", uids.Count);

            bool handledMail = false;

            if (uids.Count > 0)
            {
                Log.Info("Fetching unseen messages...");

                MailMessageCollection msgs = imap.DownloadEntireMessages(uids.ToString(), true);

                foreach (MailMessage msg in msgs)
                {
                    Log.Info("Recent message index: {0} Subject: {1}", msg.IndexOnServer, msg.Subject);

                    if (msg.Subject.Contains(this.reqMailSubject))
                    {
                        Log.Info("Pacnet confirmation message has been received with subject: {0}", msg.Subject);
                        handledMail = true;
                        DB.ExecuteNonQuery(
                            "SetPacnetTopUpConfirmationRequestConfirmed",
                            CommandSpecies.StoredProcedure,
                            new QueryParameter("DateSent", this.dateSentReq),
                            new QueryParameter("DateConfirmed", TimeZoneInfo.ConvertTimeToUtc(msg.DateReceived))
                            );
                    }             // if has matching subject
                }                 // foreach email

                Log.Info("Fetching unseen messages complete.");
            }             // if
            return(handledMail);
        }
Example #7
0
        /// <summary>
        /// Callback for BeginIdle. It'll be called after stopping idle and will download new messages
        /// </summary>
        private void IdleCallback(IAsyncResult result)
        {
            _imap.EndIdle();

            // If not exiting, i.e. just stopping idle and we should try to download new messages
            // Exiting means the application is being terminated and we shouldn't try downloading new messages
            if (!_exiting)
            {
                TimerStop();

                // Search for UNSEEN (i.e. new) messages
                var uids = (UidCollection)_imap.Search(true, "UNSEEN", null);

                if (uids.Count > 0)
                {
                    // Download all the messages found by Search method
                    MailMessageCollection msgs = _imap.DownloadMessageHeaders(uids.ToString(), true);

                    // Iterate througn the messages collection and display info about them
                    foreach (MailMessage msg in msgs)
                    {
                        HandleMessage(msg);
                    }
                }

                // Messages have been downloaded and idling starts again
                TimerStart();
                _imap.BeginIdle(new AsyncCallback(IdleCallback), null);
            }
            else
            {
                // If exiting is in progress, disconnect after stopping idle
                _imap.Disconnect();
            }
        }
 public WebMailMessageCollection(Account acct, MailMessageCollection messageCollection, bool isUidStr, Folder fld)
 {
     foreach (MailMessage msg in messageCollection)
     {
         WebMailMessage webMsg = new WebMailMessage(acct);
         webMsg.Init(msg, isUidStr, fld);
         Add(webMsg);
     }
 }
Example #9
0
        }         // imp_MessageStatus

        /// <summary>
        /// Callback for BeginIdle. It'll be called after stopping idle and will download new messages
        /// </summary>
        private void IdleCallback(IAsyncResult result)
        {
            try {
                imp.EndIdle();

                // If not exiting, i.e. just stopping idle and we should try to download new messages
                // Exiting means the application is being terminated and we shouldn't try downloading new messages
                if (!exiting)
                {
                    TimerStop();
                    Info("Stopped idling, will download messages");

                    UidCollection uids = FillUids();

                    if (uids.Count != 0 && LastId != uids[uids.Count - 1])
                    {
                        LastId = uids[uids.Count - 1];
                        Info("Last received mail:{0}", LastId);
                        MailMessageCollection msgs = imp.DownloadEntireMessages(uids.ToString(), true);
                        HandleMessages(msgs);

                        if (imp.IsIdle)
                        {
                            TimerStop();
                            imp.StopIdle();
                        }                 // if
                    }                     // if

                    // Messages have been downloaded and idling starts again
                    TimerStart();
                    imp.BeginIdle(IdleCallback, null);
                    Info("Started idling again");
                }
                else
                {
                    // If exiting is in progress, disconnect after stopping idle
                    imp.Disconnect();
                }                 // if
            }
            catch (Exception ex) {
                Error("Error occured while in IdleCallback:{0}", ex);
                Info("Trying to reconnect to mailbox");
                SafeDispose();
                ConnectToMailboxLoop();
            }     // try
        }         // IdleCallback
Example #10
0
        public void TestMailbee()
        {
            _cfg = new Conf(_log);
            _cfg.Init();

            try
            {
                Global.LicenseKey = _cfg.MailBeeLicenseKey;
            }
            catch (MailBeeLicenseException e)
            {
                _log.Error("License key is invalid: {0}", e);
                //Mailer.Mailer.SendMail(_cfg.TestAddress, _cfg.TestPassword, "EzAutoresonder Error", e.ToString(), "*****@*****.**");
            }             // try

            _imap = new Imap {
                SslMode = MailBee.Security.SslStartupMode.OnConnect
            };

            // Connect to IMAP server
            _imap.Connect(_cfg.Server, _cfg.Port);
            _log.Info("Connected to the server");

            // Log into IMAP account
            _imap.Login("", "");           //todo enter a login
            _log.Info("Logged into the server");

            // Select Inbox folder
            _imap.ExamineFolder("Inbox");
            var uids = (UidCollection)_imap.Search(true, "UNSEEN", null);

            if (uids.Count > 0)
            {
                // Download all the messages found by Search method
                MailMessageCollection msgs = _imap.DownloadMessageHeaders(uids.ToString(), true);

                // Iterate througn the messages collection and display info about them
                foreach (MailMessage msg in msgs)
                {
                    Console.WriteLine("Message #" + msg.IndexOnServer.ToString(CultureInfo.InvariantCulture) +
                                      " has subject: " + msg.Subject + "  from: " + msg.From.Email + " received on: " +
                                      msg.DateReceived);
                }
            }
        }
Example #11
0
        private bool GetAllMessages(Pop3 pop)
        {
            try
            {
                MailMessageCollection messages = pop.DownloadEntireMessages();

                foreach (MailMessage message in messages)
                {
                    if (AddToList(message))
                    {
                        pop.DeleteMessage(message.IndexOnServer);
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                _lastError = ex.Message;
            }
            return(false);
        }
Example #12
0
 protected abstract void HandleMessages(MailMessageCollection msgs);
Example #13
0
        }         // Init

        public void Run()
        {
            DateTime oStartTime = DateTime.UtcNow;

            for ( ; ;)
            {
                DateTime oNow = DateTime.UtcNow;

                if (oNow.Subtract(oStartTime).CompareTo(m_oTotalWaitingTime) > 0)
                {
                    Error("Email did not arrive, not waiting for it any more.");
                    break;
                }                 // if

                try
                {
                    var imap = new Imap
                    {
                        // Enable SSL/TLS if necessary
                        SslMode = MailBee.Security.SslStartupMode.OnConnect
                    };

                    // Connect to IMAP server
                    imap.Connect(m_oConf.Server, m_oConf.Port);
                    Info("Connected to the server");

                    // Log into IMAP account
                    imap.Login(m_oConf.LoginAddress, m_oConf.LoginPassword);
                    Info("Logged into the server");

                    // Select Inbox folder
                    imap.SelectFolder("Inbox");

                    UidCollection uids = (UidCollection)imap.Search(true, "UNSEEN", null);

                    Debug("Unseen email count: {0}", uids.Count);

                    if (uids.Count > 0)
                    {
                        Debug("Fetching unseen messages...");

                        MailMessageCollection msgs = imap.DownloadEntireMessages(uids.ToString(), true);

                        bool handledMail = false;

                        foreach (MailMessage msg in msgs)
                        {
                            Info("Recent message index: {0} Subject: {1}", msg.IndexOnServer, msg.Subject);

                            if (msg.HasAttachments)
                            {
                                foreach (Attachment attachment in msg.Attachments)
                                {
                                    if (Consts.AttachmentContentTypes.Contains(attachment.ContentType))
                                    {
                                        Info("Has pdf attachment {0}", attachment.Filename);
                                        byte[]          data            = attachment.GetData();
                                        ParsePacNetText parsePacNetText = new ParsePacNetText(m_oConf.LoginAddress, m_oConf.LoginPassword);
                                        parsePacNetText.ParsePdf(data);
                                        handledMail = true;
                                    }             // if appropriate attachment type
                                }                 // foreach attachment
                            }                     // if has attachment
                        }                         // foreach email

                        if (handledMail)
                        {
                            PacNetBalance.SavePacNetBalanceToDb();
                        }

                        Debug("Fetching unseen messages complete.");

                        if (handledMail)
                        {
                            break;
                        }
                    }                     // if

                    imap.Disconnect();
                }
                catch (PacNetBalanceException pex)
                {
                    Error("PacNetBalanceException: {0}", pex);
                    Mailer.Mailer.SendMail(m_oConf.LoginAddress, m_oConf.LoginPassword, "PacNet Balance Report Error", pex.ToString(),
                                           "*****@*****.**");
                }
                catch (MailBeeStreamException mex)
                {
                    Error("MailBeeStreamException: {0}", mex);
                }
                catch (Exception e) {
                    Error("Some generic Exception: {0}", e);
                }                 // try

                Debug("Sleeping...");
                Thread.Sleep(m_oConf.MailboxReconnectionIntervalSeconds * 1000);
            }     // for
        }         // Run