Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
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);
        }
    }
   
}
Posted
Updated 8-May-12 22:54pm
v2
Comments
Md. Rashim Uddin 9-May-12 4:45am    
So What the Problem is? Can you explain a little more explanation of the problem you are facing??
Sandeep Mewara 10-May-12 1:38am    
And the issue is?

1 solution

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.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900