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;
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", "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;
for (int i = count; i >= 1; i -= 1)
{
try
{
Application.DoEvents();
string body;
Message message = pop3Client.GetMessage(i);
filenames = "";
MessageHeader messageheader = message.Headers;
mailFrom = messageheader.ReturnPath.ToString();
mailFromName = messageheader.From.DisplayName.ToString();
messageId = messageheader.MessageId;
subject = messageheader.Subject.ToString();
MessagePart plainTextPart = message.FindFirstPlainTextVersion();
if (plainTextPart != null)
{
body = plainTextPart.GetBodyAsText();
subject = plainTextPart.ContentId;
}
else
{
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>>";
}
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];
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);
}
}
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)
{
}
catch (PopServerLockedException)
{
}
catch (LoginDelayException)
{
}
catch (Exception e)
{
}
finally
{
}
if (nooffiles1 != 0)
{
if (pop3Client.Connected)
pop3Client.Disconnect();
pop3Client.Connect("pop.gmail.com", 995, true);
pop3Client.Authenticate("recent:" + username + "@gmail.com", password);
}
}
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)
{
}
}
}