Example #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            // Create a POP3 client
            Pop3Client client = new Pop3Client();

            // Basic settings (required)
            client.Host     = "pop3.youdomain.com";
            client.Username = "******";
            client.Password = "******";

            // ExStart:SSLEnabledServer
            // Set implicit security mode
            client.SecurityOptions = SecurityOptions.SSLImplicit;
            // ExEnd:SSLEnabledServer

            try
            {
                // Retrieve first message in MailMessage format directly
                Aspose.Email.MailMessage msg;
                msg             = client.FetchMessage(1);
                txtFrom.Text    = msg.From.ToString();
                txtSubject.Text = msg.Subject.ToString();
                txtBody.Text    = msg.HtmlBody.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static void Run()
        {
            // ExStart:ParseMessageAndSave
            // The path to the File directory.
            string dataDir = RunExamples.GetDataDir_POP3();

            // Create an instance of the Pop3Client class
            Pop3Client client = new Pop3Client();

            // Specify host, username and password, Port and SecurityOptions for your client
            client.Host = "pop.gmail.com";
            client.Username = "******";
            client.Password = "******";
            client.Port = 995;
            client.SecurityOptions = SecurityOptions.Auto;

            try
            {
                // Fetch the message by its sequence number and Save the message using its subject as the file name
                MailMessage msg = client.FetchMessage(1);
                msg.Save(dataDir + "first-message_out.eml", SaveOptions.DefaultEml);
                client.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(Environment.NewLine + ex.Message);
            }
            finally
            {
                client.Dispose();
            }
            // ExEnd:ParseMessageAndSave
            Console.WriteLine(Environment.NewLine + "Downloaded email using POP3. Message saved at " + dataDir + "first-message_out.eml");
        }
Example #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            // ExStart:AsposeEmailPop3
            // Create a POP3 client
            Pop3Client client = new Pop3Client();

            // Basic settings (required)
            client.Host     = "pop3.youdomain.com";
            client.Username = "******";
            client.Password = "******";

            try
            {
                // Retrieve first message in MailMessage format directly
                Aspose.Email.MailMessage msg;
                msg             = client.FetchMessage(1);
                txtFrom.Text    = msg.From.ToString();
                txtSubject.Text = msg.Subject.ToString();
                txtBody.Text    = msg.HtmlBody.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            // ExEnd:AsposeEmailPop3
        }
        public static void Run()
        {
            // ExStart:ParseMessageAndSave
            // The path to the File directory.
            string dataDir = RunExamples.GetDataDir_POP3();

            // Create an instance of the Pop3Client class
            Pop3Client client = new Pop3Client();

            // Specify host, username and password, Port and SecurityOptions for your client
            client.Host            = "pop.gmail.com";
            client.Username        = "******";
            client.Password        = "******";
            client.Port            = 995;
            client.SecurityOptions = SecurityOptions.Auto;

            try
            {
                // Fetch the message by its sequence number and Save the message using its subject as the file name
                MailMessage msg = client.FetchMessage(1);
                msg.Save(dataDir + "first-message_out.eml", SaveOptions.DefaultEml);
                client.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(Environment.NewLine + ex.Message);
            }
            finally
            {
                client.Dispose();
            }
            // ExEnd:ParseMessageAndSave
            Console.WriteLine(Environment.NewLine + "Downloaded email using POP3. Message saved at " + dataDir + "first-message_out.eml");
        }
    protected void gvMessages_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DisplayMessage")
        {
            pnlMessage.Visible = true;
            string msgSequenceNumber = e.CommandArgument.ToString();

            try
            {
                // initialize pop3 client
                Pop3Client client = new Pop3Client();
                client.Host = txtHost.Text;
                client.Port = int.Parse(txtPort.Text);
                client.Username = txtUsername.Text;
                client.Password = Session["Password"].ToString();

                // SSL settings
                if (chSSL.Checked == true)
                {
                    client.EnableSsl = true;
                    client.SecurityMode = Pop3SslSecurityMode.Implicit;
                }

                // connect to pop3 server and login
                client.Connect(true);
                lblMessage.ForeColor = Color.Green;
                lblMessage.Text = "Successfully connected to POP3 Mail server.<br><hr>";

                // get the message
                MailMessage msg = client.FetchMessage(int.Parse(msgSequenceNumber));
                // sender name and address
                litFrom.Text = msg.From[0].DisplayName + " <" + msg.From[0].Address + ">";
                // to addresses
                litTo.Text = "";
                foreach (MailAddress address in msg.To)
                {
                    litTo.Text += address.DisplayName + " <" + address.Address + "> , ";
                }
                // cc addresses
                litCc.Text = "";
                foreach (MailAddress address in msg.CC)
                {
                    litCc.Text += address.DisplayName + " <" + address.Address + "> , ";
                }
                // subject
                litSubject.Text = msg.Subject;
                litBody.Text = msg.HtmlBody;

                client.Disconnect();
            }
            catch (Exception ex)
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "Error: " + ex.Message;
            }
        }
    }
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_POP3();
            string dstEmail = dataDir + "message.msg";

            //Create an instance of the Pop3Client class
            Pop3Client client = new Pop3Client();

            //Specify host, username and password for your client
            client.Host = "pop.gmail.com";

            // Set username
            client.Username = "******";

            // Set password
            client.Password = "******";

            // Set the port to 995. This is the SSL port of POP3 server
            client.Port = 995;

            // Enable SSL
            client.SecurityOptions = SecurityOptions.Auto;

            try
            {
                int messageCount = client.GetMessageCount();
                // Create an instance of the MailMessage class
                MailMessage msg;

                for (int i = 1; i <= messageCount; i++)
                {
                    //Retrieve the message in a MailMessage class
                    msg = client.FetchMessage(i);

                    Console.WriteLine("From:" + msg.From.ToString());
                    Console.WriteLine("Subject:" + msg.Subject);
                    Console.WriteLine(msg.HtmlBody);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                client.Disconnect();
            } 

            Console.WriteLine(Environment.NewLine + "Retrieved email messages using POP3 ");
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "json";
            Pop3Client client = new Pop3Client();

            client.Host         = "pop.126.com";
            client.Username     = "******";
            client.Password     = "******";
            client.Port         = 110;
            client.SecurityMode = Pop3SslSecurityMode.Implicit;
            string result = string.Empty;

            try
            {
                int              messageCount = client.GetMessageCount();
                MailMessage      msg;
                string           mailSender = string.Empty;
                List <EmailItem> items      = new List <EmailItem>();
                EmailItem        emailItem  = new EmailItem();
                for (int i = messageCount; i >= messageCount - 2; i--)
                {
                    msg = client.FetchMessage(i);
                    if (!string.IsNullOrEmpty(msg.From.DisplayName))
                    {
                        mailSender = msg.From.DisplayName;
                    }
                    else
                    {
                        mailSender = msg.From.Address.Substring(0, msg.From.Address.LastIndexOf('@'));
                    }

                    items.Add(new EmailItem
                    {
                        Subject  = msg.Subject,
                        SendDate = msg.Date,
                        Sender   = mailSender
                    });
                    emailItem.Items = items;
                }
                result = JsonConvert.SerializeObject(emailItem, Formatting.Indented);
            }
            catch (Exception ex)
            {
                result = string.Empty;
            }
            finally
            {
                client.Disconnect();
            }
            context.Response.Write(result);
        }
    private void DownloadFile(string msgSequenceNumber, string format)
    {
        try
        {
            // initialize pop3 client
            Pop3Client client = new Pop3Client();
            client.Host = txtHost.Text;
            client.Port = int.Parse(txtPort.Text);
            client.Username = txtUsername.Text;
            client.Password = Session["Password"].ToString();

            // SSL settings
            if (chSSL.Checked == true)
            {
                client.EnableSsl = true;
                client.SecurityMode = Pop3SslSecurityMode.Implicit;
            }

            // connect to pop3 server and login
            client.Connect(true);
            lblMessage.ForeColor = Color.Green;
            lblMessage.Text = "Successfully connected to POP3 Mail server.<br><hr>";

            // get the message
            MemoryStream stream = new MemoryStream();
            MailMessage msg = client.FetchMessage(int.Parse(msgSequenceNumber));
            if (format == "eml")
                msg.Save(stream, MessageFormat.Eml);
            else
                msg.Save(stream, MessageFormat.Msg);
            stream.Position = 0;
            byte[] msgBytes = new byte[stream.Length];
            stream.Read(msgBytes, 0, (int)stream.Length);

            client.Disconnect();

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("Content-Length", msgBytes.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment; filename=Message." + format);
            Response.ContentType = "application/octet-stream";
            Response.BinaryWrite(msgBytes);
            Response.End();

        }
        catch (Exception ex)
        {
            lblMessage.ForeColor = Color.Red;
            lblMessage.Text = "Error: " + ex.Message;
        }
    }
Example #9
0
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir  = RunExamples.GetDataDir_POP3();
            string dstEmail = dataDir + "message.msg";

            //Create an instance of the Pop3Client class
            Pop3Client client = new Pop3Client();

            //Specify host, username and password for your client
            client.Host = "pop.gmail.com";

            // Set username
            client.Username = "******";

            // Set password
            client.Password = "******";

            // Set the port to 995. This is the SSL port of POP3 server
            client.Port = 995;

            // Enable SSL
            client.SecurityOptions = SecurityOptions.Auto;

            try
            {
                int messageCount = client.GetMessageCount();
                // Create an instance of the MailMessage class
                MailMessage msg;

                for (int i = 1; i <= messageCount; i++)
                {
                    //Retrieve the message in a MailMessage class
                    msg = client.FetchMessage(i);

                    Console.WriteLine("From:" + msg.From.ToString());
                    Console.WriteLine("Subject:" + msg.Subject);
                    Console.WriteLine(msg.HtmlBody);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                client.Disconnect();
            }

            Console.WriteLine(Environment.NewLine + "Retrieved email messages using POP3 ");
        }
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_POP3();
            string dstEmail = dataDir + "first-message.eml";

            //Create an instance of the Pop3Client class
            Pop3Client client = new Pop3Client();

            //Specify host, username and password for your client
            client.Host = "pop.gmail.com";

            // Set username
            client.Username = "******";

            // Set password
            client.Password = "******";

            // Set the port to 995. This is the SSL port of POP3 server
            client.Port = 995;

            // Enable SSL
            client.SecurityOptions = SecurityOptions.Auto;

            try
            {
                //Fetch the message by its sequence number
                MailMessage msg = client.FetchMessage(1);

                //Save the message using its subject as the file name
                msg.Save(dstEmail, Aspose.Email.Mail.SaveOptions.DefaultEml);
                client.Disconnect();

            }
            catch (Exception ex)
            {
                Console.WriteLine(Environment.NewLine + ex.Message);
            }
            finally
            {
                client.Disconnect();
            } 

            Console.WriteLine(Environment.NewLine + "Downloaded email using POP3. Message saved at " + dstEmail);
        }
Example #11
0
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir  = RunExamples.GetDataDir_POP3();
            string dstEmail = dataDir + "first-message.eml";

            //Create an instance of the Pop3Client class
            Pop3Client client = new Pop3Client();

            //Specify host, username and password for your client
            client.Host = "pop.gmail.com";

            // Set username
            client.Username = "******";

            // Set password
            client.Password = "******";

            // Set the port to 995. This is the SSL port of POP3 server
            client.Port = 995;

            // Enable SSL
            client.SecurityOptions = SecurityOptions.Auto;

            try
            {
                //Fetch the message by its sequence number
                MailMessage msg = client.FetchMessage(1);

                //Save the message using its subject as the file name
                msg.Save(dstEmail, Aspose.Email.Mail.SaveOptions.DefaultEml);
                client.Disconnect();
            }
            catch (Exception ex)
            {
                Console.WriteLine(Environment.NewLine + ex.Message);
            }
            finally
            {
                client.Disconnect();
            }

            Console.WriteLine(Environment.NewLine + "Downloaded email using POP3. Message saved at " + dstEmail);
        }
Example #12
0
        private void readInbox(Pop3Client client)
        {
            Dictionary <string, clsFileList> convertFiles = new Dictionary <string, clsFileList>();


            // get number of messages in the mailbox
            int messageCount = client.GetMessageCount();

            // iterate through the messages and retrieve one by one
            for (int i = 1; i <= messageCount; i++)
            {
                // create object of type MailMessage
                MailMessage msg;

                msg = client.FetchMessage(i);

                foreach (Attachment attachedFile in msg.Attachments)
                {
                    extractAttachment(convertFiles, attachedFile);
                }
            }
        }
        public static void Run()
        {
            try
            {
                // Create a POP3 client
                Pop3Client client;
                client = new Pop3Client();

                // Basic settings (required)
                client.Host            = "pop.gmail.com";
                client.Username        = "******";
                client.Password        = "******";
                client.Port            = 995;
                client.SecurityOptions = SecurityOptions.SSLImplicit;

                // ExStart:RetrieveGmailMessages
                // Get the number of messages in the mailbox
                int messageCount = client.GetMessageCount();

                // Iterate through the messages and retrieve one by one
                for (int i = 1; i <= messageCount; i++)
                {
                    // Create an object of type MailMessage and Retrieve the message in MailMessage format directly
                    Aspose.Email.MailMessage msg;
                    msg = client.FetchMessage(i);
                    Console.WriteLine("From:" + msg.From.ToString());
                    Console.WriteLine("Subject:" + msg.Subject);
                    Console.WriteLine(msg.HtmlBody);
                    msg.Save(i + "Getmessage_out.eml");
                }
                // ExEnd:RetrieveGmailMessages
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public static void Run()
        {
            //ExStart:RetrievingEmailMessages
            // Create an instance of the Pop3Client class
            Pop3Client client = new Pop3Client();

            // Specify host, username, password, Port and SecurityOptions for your client
            client.Host = "pop.gmail.com";
            client.Username = "******";
            client.Password = "******";
            client.Port = 995;
            client.SecurityOptions = SecurityOptions.Auto;
            try
            {
                int messageCount = client.GetMessageCount();
                // Create an instance of the MailMessage class and Retrieve message
                MailMessage message;
                for (int i = 1; i <= messageCount; i++)
                {
                    message = client.FetchMessage(i);
                    Console.WriteLine("From:" + message.From);
                    Console.WriteLine("Subject:" + message.Subject);
                    Console.WriteLine(message.HtmlBody);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                client.Dispose();
            }
            //ExEnd:RetrievingEmailMessages
            Console.WriteLine(Environment.NewLine + "Retrieved email messages using POP3 ");
        }
        public static void Run()
        {
            //ExStart:RetrievingEmailMessages
            // Create an instance of the Pop3Client class
            Pop3Client client = new Pop3Client();

            // Specify host, username, password, Port and SecurityOptions for your client
            client.Host            = "pop.gmail.com";
            client.Username        = "******";
            client.Password        = "******";
            client.Port            = 995;
            client.SecurityOptions = SecurityOptions.Auto;
            try
            {
                int messageCount = client.GetMessageCount();
                // Create an instance of the MailMessage class and Retrieve message
                MailMessage message;
                for (int i = 1; i <= messageCount; i++)
                {
                    message = client.FetchMessage(i);
                    Console.WriteLine("From:" + message.From);
                    Console.WriteLine("Subject:" + message.Subject);
                    Console.WriteLine(message.HtmlBody);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                client.Dispose();
            }
            //ExEnd:RetrievingEmailMessages
            Console.WriteLine(Environment.NewLine + "Retrieved email messages using POP3 ");
        }
Example #16
0
        public static void 查看气象台163pop3()
        {
            var util = new XmlConfig(Environment.CurrentDirectory + @"\config\智能网格设置.xml");
            // Connect and log in to POP3
            const string host     = "pop.163.com";
            const int    port     = 995;
            var          username = util.Read("emailConfig", "emailFrom", "address");
            var          password = util.Read("emailConfig", "emailFrom", "password");
            var          yjjl     = util.Read("emailConfig", "空气邮件查收记录");
            var          client   = new Pop3Client(host, port, username, password);

            try
            {
                var jlLists   = new List <string>();
                var dateLists = new List <DateTime>();
                var szls      = yjjl.Split(',');
                if (szls.Length > 0)
                {
                    foreach (var item in szls)
                    {
                        if (item.Trim() != "")
                        {
                            try
                            {
                                var strDate = item.Trim();
                                jlLists.Add(strDate);
                                dateLists.Add(Convert.ToDateTime(strDate.Substring(0, 4) + "-" +
                                                                 strDate.Substring(4, 2) + "-" +
                                                                 strDate.Substring(6, 2)));
                            }
                            catch
                            {
                            }
                        }
                    }
                }

                var builder1 = new MailQueryBuilder();
                builder1.From.Contains("yewu_zzy", true);
                builder1.InternalDate.Before(DateTime.Now.AddDays(1));
                builder1.InternalDate.Since(DateTime.Now.AddDays(-7));
                var query1          = builder1.GetQuery();
                var messageInfoCol1 = client.ListMessages(query1);
                var changeBS        = false;
                for (var i = 0; i < messageInfoCol1.Count; i++)
                {
                    if (!jlLists.Exists(y => messageInfoCol1[i].Subject.Contains(y)))
                    {
                        changeBS = true;
                        var msg = client.FetchMessage(messageInfoCol1[i].UniqueId);
                        try
                        {
                            var regex = new Regex("\\d{10}");
                            var m     = regex.Match(messageInfoCol1[i].Subject);
                            if (m.Success)
                            {
                                var strDate    = m.Groups[0].Value;
                                var dateTimels = Convert.ToDateTime(strDate.Substring(0, 4) + "-" +
                                                                    strDate.Substring(4, 2) + "-" +
                                                                    strDate.Substring(6, 2));
                                var dataDir = util.Read("路径", "Path空气质量中长期预报") +
                                              $@"{dateTimels:yyyy}\{dateTimels:MM}\{dateTimels:dd}\\";
                                if (!File.Exists(dataDir))
                                {
                                    Directory.CreateDirectory(dataDir);
                                }

                                foreach (var item in msg.Attachments)
                                {
                                    item.Save(dataDir + item.Name);
                                }
                                dateLists.Add(dateTimels);
                            }
                        }
                        catch
                        {
                        }
                    }
                }

                try
                {
                    if (changeBS)
                    {
                        if (dateLists.Count > 0)
                        {
                            var saveDate = new List <DateTime>();
                            foreach (var item in dateLists)
                            {
                                if (item.CompareTo(DateTime.Now.Date.AddDays(-8)) >= 0)
                                {
                                    saveDate.Add(item);
                                }
                            }

                            var saveStr = "";
                            foreach (var item in saveDate)
                            {
                                saveStr += $"{item:yyyyMMdd},";
                            }
                            if (saveStr.Length > 0)
                            {
                                saveStr = saveStr.Substring(0, saveStr.Length - 1);
                            }
                            util.Write(saveStr, "emailConfig", "空气邮件查收记录");
                        }
                    }
                }
                catch
                {
                }

                client.Dispose();
            }
            catch (Exception ex)
            {
            }
        }