示例#1
0
        public static void GetDataFromMail()
        {
            string dataDir    = GetDataDir_Emails();
            string dataDirAtt = GetDataDir_Attachments();

            using (Imap imap = new Imap())
            {
                EmailItem item = new EmailItem();
                try
                {
                    imap.Connect("imap.gmail.com", 993, true);
                    imap.UseBestLogin(Username, Password);
                    imap.SelectInbox();
                    List <long> uids = imap.Search(Flag.Unseen);

                    int count = 0;
                    if (uids.Count > 0)
                    {
                        foreach (long uid in uids)
                        {
                            IMail email = new MailBuilder().CreateFromEml(imap.GetMessageByUID(uid));
                            var   eml   = imap.GetMessageByUID(uid);

                            item.Subject   = email.Subject;
                            item.MessageID = email.MessageID;
                            item.Date      = Convert.ToDateTime(email.Date);
                            item.Content   = email.GetBodyAsHtml();
                            item.Path      = dataDir + email.MessageID + ".eml";
                            string filename = string.Format(dataDir + item.MessageID + ".eml", uid);
                            File.WriteAllBytes(filename, eml);
                            foreach (MailAddress cc in email.Cc)
                            {
                                foreach (MailBox mailbox in cc.GetMailboxes())
                                {
                                    item.Cc.Add(mailbox.Address);
                                }
                            }
                            int emailID = Email.InsertEmail(item);
                            if (emailID > 0)
                            {
                                foreach (MimeData SaveAttachment in email.Attachments)
                                {
                                    AttachmentItem AttachmentItem = new AttachmentItem();
                                    Guid           g = Guid.NewGuid();
                                    while (Email.GUIDIsExist(g.ToString()))
                                    {
                                        g = Guid.NewGuid();
                                    }
                                    AttachmentItem.GUID           = Convert.ToString(g);
                                    AttachmentItem.EmailID        = emailID;
                                    AttachmentItem.AttachmentName = SaveAttachment.FileName;
                                    AttachmentItem.ContentType    = Convert.ToString(SaveAttachment.ContentType).ToLower();
                                    AttachmentItem.Size           = Convert.ToInt64(SaveAttachment.Size);
                                    AttachmentItem.Path           = dataDirAtt + AttachmentItem.GUID + "-" + SaveAttachment.SafeFileName;


                                    if (Email.InsertAttachment(AttachmentItem))
                                    {
                                        SaveAttachment.Save(dataDirAtt + AttachmentItem.GUID + "-" + SaveAttachment.SafeFileName);
                                    }
                                    else
                                    {
                                        Console.WriteLine("Fail to download attachment.");
                                    }
                                }
                                count++;
                                Console.WriteLine(" - Inserted email " + item.Subject + " - " + item.Date);
                                imap.MarkMessageSeenByUID(uid);
                                imap.GmailLabelMessageByUID(uid, FolderFlag.XStarred.Name);
                            }
                        }

                        Console.WriteLine("--Download " + count + " emails completed--");
                        imap.Close();
                    }
                }
                catch
                {
                    Console.WriteLine("Internet problem occured.Please check your internet connection.");
                    Thread.Sleep(5000);
                    GetDataFromMail();
                }
            }
        }
示例#2
0
        public static bool InsertAttachment(AttachmentItem item)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(Config.ConnectionString))
                {
                    connection.Open();
                    string sql = "INSERT INTO dbo.Attachment(email_id,attachment_name,content_type,size,temp_name,path)"
                                 + "VALUES(@email_id,@attachment_name,@content_type,@size,@temp_name,@path)";
                    SqlCommand cmd = new SqlCommand(sql, connection);

                    cmd.Parameters.AddWithValue("@email_id", item.EmailID);

                    if (String.IsNullOrEmpty(item.GUID))
                    {
                        cmd.Parameters.AddWithValue("@temp_name", DBNull.Value);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@temp_name", item.GUID);
                    }


                    if (String.IsNullOrEmpty(item.AttachmentName))
                    {
                        cmd.Parameters.AddWithValue("@attachment_name", DBNull.Value);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@attachment_name", item.AttachmentName);
                    }


                    if (String.IsNullOrEmpty(item.ContentType))
                    {
                        cmd.Parameters.AddWithValue("@content_type", DBNull.Value);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@content_type", item.ContentType);
                    }


                    if (String.IsNullOrEmpty(Convert.ToString(item.Size)))
                    {
                        cmd.Parameters.AddWithValue("@size", DBNull.Value);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@size", item.Size);
                    }
                    if (String.IsNullOrEmpty(item.Path))
                    {
                        cmd.Parameters.AddWithValue("@path", DBNull.Value);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@path", item.Path);
                    }
                    cmd.ExecuteNonQuery();
                    return(true);
                }
            }
            catch
            {
                return(false);
            }
        }