请为我的Windows应用程序帮助我下载gmail附件 [英] Please healp me for my windows application to download gmail attachments

查看:90
本文介绍了请为我的Windows应用程序帮助我下载gmail附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我创建了一个Windows应用程序,该应用程序以gmail下载所有附件.在此应用程序中,用户成功登录后,项目将自动下载邮件中的附件.

如果我们不想下载,我们可以退出应用程序.

在应用程序下载附件之间,我想显示消息.例如,当安装了新软件时,将在启动按钮附近显示一条消息:已安装软件"(仅限第一次).

同样,当附件下载时,我想显示一条类似的消息.

我的代码是

Hi, I have created a windows application which downloads all attachments in gmail. In this application, once the user has logged in successfully, the project automatically downloads attachments in the mail.

If we don''t want to download we can just exit from the application.

In between when the application downloads the attachments, I wants to show message. For example, when a new software installed there willll be a message displayed: ''there is a software installed'' near to start button (the first time only).

Likewise I want to show a message like that when the time of attachments downloads.

My code is

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using OpenPop.Mime;
using OpenPop.Mime.Header;
using OpenPop.Pop3;
using OpenPop.Pop3.Exceptions;
using OpenPop.Common.Logging;
using MySql.Data.MySqlClient;
using System.Configuration;
using Message = OpenPop.Mime.Message;

namespace GmailAttachments
{
    public partial class Form1 : Form
    {
        private Pop3Client pop3Client;
        private Dictionary<int,> messages;
                
        //MySqlConnection myConn = new MySqlConnection(cnString);
        public Form1()
        {

            InitializeComponent();
            pop3Client = new Pop3Client();
            messages = new Dictionary<int,>();
            this.MaximumSize = this.MinimumSize = this.Size;
        }
        private void SetFormPosition()
        {
            this.StartPosition = FormStartPosition.Manual;
            this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
            this.Top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            ReceiveMails();
            timer1.Enabled = true;
        }

        MySqlConnection mConnection;
        MySqlDataAdapter mDa;
        MySqlCommand mCmd;
        public void connectionopen()
        {
            string connStr = "server=******;user=*****;database=*******;port=3306;password=*******;";
            mConnection = new MySqlConnection(connStr);
            if (mConnection.State == ConnectionState.Open)
            {
                mConnection.Close();
            }
            mConnection.Open();
        }
        public int ExecuteSPNonQuery(string Query)
        {
            int result;
            connectionopen();
            mCmd = new MySqlCommand(Query, mConnection);

            try
            {
                result = mCmd.ExecuteNonQuery();
            }
            catch (MySqlException mye)
            {
                throw mye;
            }
            finally
            {
                mConnection.Close();
                mConnection.Dispose();
            }
            return result;

        }
        private void ReceiveMails()
        {
            int nooffiles = 0;
            string username = txtUserName.Text;
            string password = txtPwd.Text;
            string mailFrom = "";
            string mailFromName = "";
            string mailFromId = "";
            string messageId = "";
            string subject = "";
            string filenames = "";
            int nooffiles1 = 0;
            int nooffiles2 = 0;
            try
            {
                if (username == "" || password == "")
                {
                    //MessageBox.Show("Please enter the user credentials");

                    MessageBox.Show("Please enter the user credentials", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
                    this.WindowState = FormWindowState.Maximized;
                    return;
                }
                if (pop3Client.Connected)
                    pop3Client.Disconnect();
                pop3Client.Connect("pop.gmail.com", 995, true);
                pop3Client.Authenticate("recent:"+username+"@gmail.com", password);
                int count = pop3Client.GetMessageCount();
                messages.Clear();
                int success = 0;
                int fail = 0;
                
                //if (count < 1)
                //{
                //    MessageBox.Show("There is no Mail");
                //}
                for (int i = count; i >= 1; i -= 1)
                {
                    try
                    {
                        Application.DoEvents();
                        string body;
                        Message message = pop3Client.GetMessage(i);
                        filenames = "";
                        //MessageHeader messageheader = new MessageHeader();
                        MessageHeader messageheader = message.Headers;
                        mailFrom = messageheader.ReturnPath.ToString();
                        mailFromName = messageheader.From.DisplayName.ToString();
                        //string temp = mailFrom.Replace('>',  " ");
                        //string[] mailItems = temp.Split('<');
                        //mailFromName = mailItems[1];
                        //mailFromId = mailItems[2];
                        messageId = messageheader.MessageId;                        
                        subject = messageheader.Subject.ToString();
                        MessagePart plainTextPart = message.FindFirstPlainTextVersion();                        
                        if (plainTextPart != null)
                        {
                            // The message had a text/plain version - show that one                            
                            body = plainTextPart.GetBodyAsText();                            
                            subject = plainTextPart.ContentId;                            
                        }
                        else
                        {
                            // Try to find a body to show in some of the other text versions
                            List<messagepart> textVersions = message.FindAllTextVersions();
                            if (textVersions.Count >= 1)
                                body = textVersions[0].GetBodyAsText();
                            else
                                body = "<<OpenPop>> Cannot find a text version body in this message to show <<OpenPop>>";
                        }
                        
                        // Build up the attachment list
                        List<messagepart> attachments = message.FindAllAttachments();
                        
                        if (attachments.Count > 0)
                        {
                            nooffiles2 = 0;
                            System.IO.Directory.CreateDirectory("C:\\string\\Mail\\" + messageId);
                            foreach (MessagePart attachment in attachments)
                            {
                                string ext = attachment.FileName.Split('.')[1];
                                //nooffiles1 = nooffiles1 + 1;
                                filenames =filenames+","+attachment.FileName;
                                FileInfo file = new FileInfo("C:\\string\\Mail\\" + messageId + "\\" + attachment.FileName);
                                nooffiles1 = nooffiles1 + 1;
                                nooffiles2 = nooffiles2 + 1;
                                attachment.Save(file);
                                
                            }                            
                        }
                        string query = "insert into mailattachmentstracking(messageid,mailfromname,mailfromid,downloadedby,subject,filenames,nooffilesinmail,downtime) values('" + messageId + "','" + mailFromName + "','" + mailFrom + "','" + username + "','" + subject + "','" + filenames + "','" + nooffiles2+ "',now())";
                        int res = ExecuteSPNonQuery(query);                        
                        messages.Add(i, message);
                        success++;
                    }                     
                    catch (Exception e)
                    {
                        fail++;
                    }
                    pop3Client.DeleteMessage(i);
                    //if (nooffiles1 == 0)
                    //{
                    //    MessageBox.Show("There is no attachment in the mail");
                    //}
                }                
                
            }
            catch (InvalidLoginException)
            {
                timer1.Enabled = false;
                MessageBox.Show(this, "The server did not accept the user credentials!", "POP3 Server Authentication");
                this.WindowState = FormWindowState.Maximized;
                return;
            }
            catch (PopServerNotFoundException)
            {
                //MessageBox.Show(this, "The server could not be found", "POP3 Retrieval");
            }
            catch (PopServerLockedException)
            {
                //MessageBox.Show(this, "The mailbox is locked. It might be in use or under maintenance. Are you connected elsewhere?", "POP3 Account Locked");
            }
            catch (LoginDelayException)
            {
                //MessageBox.Show(this, "Login not allowed. Server enforces delay between logins. Have you connected recently?", "POP3 Account Login Delay");
            }
            catch (Exception e)
            {
                //MessageBox.Show(this, "Error occurred retrieving mail. " + e.Message, "POP3 Retrieval");
            }
            finally
            {

            }
            if (nooffiles1 != 0)
            {
                //MessageBox.Show(nooffiles1 + " Files Downloaded");
                
                if (pop3Client.Connected)
                    pop3Client.Disconnect();
                pop3Client.Connect("pop.gmail.com", 995, true);
                pop3Client.Authenticate("recent:" + username + "@gmail.com", password);
                //List<string> messageids = pop3Client.GetMessageUids();
            }
        }
        
        private void btnExit_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            Application.Exit();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtPwd.Text = "";
            txtUserName.Text = "";
            timer1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            button1_Click(sender, e);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            //this.MaximumSize = new System.Drawing.Size(50, 20);
            //this.MinimumSize = new System.Drawing.Size(50, 20);
        }
    }
   
}

推荐答案

为下载开始时创建一个事件,并在该事件下显示消息.您可以将相同的概念应用于安装,仅在完成时触发一个事件,我会让它亲自更新状态标签.如果您在表单中使用的标签会发生变化,我想人们会更喜欢将其贴死.您可以创建一个标签更新子,该子播放一个ping来提醒用户.
Create an event for when the download starts and display the message under the event. You can apply the same concept to the installation just trigger an event on completion, i would have it update a status label personally. If you use a label in your form that changes i think people would prefer it to being mbox''d to death. You could create a label update sub that plays a ping to alert the user.


这篇关于请为我的Windows应用程序帮助我下载gmail附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆