Пример #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 int InsertEmail(EmailItem item)
        {
            SqlTransaction transaction = null;

            using (SqlConnection connection = new SqlConnection(Config.ConnectionString))
            {
                try
                {
                    connection.Open();
                    transaction = connection.BeginTransaction();
                    string sql = "INSERT INTO dbo.Email (message_id,subject,content,date,cc,path)"
                                 + "  VALUES(@message_id,@subject,@content,@date,@cc,@path);SELECT @@IDENTITY";
                    SqlCommand cmd = new SqlCommand(sql, connection);
                    cmd.Transaction = transaction;
                    cmd.Parameters.AddWithValue("@message_id", item.MessageID);
                    cmd.Parameters.AddWithValue("@date", item.Date);

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

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

                    if (item.Cc.Count > 0)
                    {
                        cmd.Parameters.AddWithValue("@cc", string.Join(",", item.Cc));
                        item.Cc.Clear();
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@cc", DBNull.Value);
                    }

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

                    int id = Convert.ToInt32(cmd.ExecuteScalar());
                    transaction.Commit();
                    return(id);
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    return(0);
                }
            }
        }