예제 #1
0
 private void m_pTabMail_MessagesToolbar_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
 {
     this.Cursor = Cursors.WaitCursor;
     try{
         if (string.Equals(e.ClickedItem.Name, "save"))
         {
             SaveFileDialog dlg = new SaveFileDialog();
             dlg.FileName = "message.eml";
             if (dlg.ShowDialog() == DialogResult.OK)
             {
                 this.Cursor = Cursors.WaitCursor;
                 POP3_ClientMessage message = (POP3_ClientMessage)m_pTabMail_Messages.SelectedItems[0].Tag;
                 File.WriteAllBytes(dlg.FileName, message.MessageToByte());
                 this.Cursor = Cursors.Default;
             }
         }
         else if (string.Equals(e.ClickedItem.Name, "delete"))
         {
             if (MessageBox.Show(this, "Do you want to delete selected message ?", "Confirm Delete:", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
             {
                 POP3_ClientMessage message = (POP3_ClientMessage)m_pTabMail_Messages.SelectedItems[0].Tag;
                 message.MarkForDeletion();
                 m_pTabMail_Messages.SelectedItems[0].Remove();
             }
         }
     }
     catch (Exception x) {
         MessageBox.Show(this, "Error: " + x.Message, "Error:", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     this.Cursor = Cursors.Default;
 }
예제 #2
0
        private void LumiSoftMail()
        {
            string sendmail, HtmlBody = "", date;

            //网络连接判断
            if (!IsNetConnect())
            {
                return;
            }

            POP3_Client popMail = new POP3_Client();

            try
            {
                popMail.Timeout = HtmlTextPath.EMAIL_TIME_OUT;

                if (string.IsNullOrEmpty(MAIL_USER_NAME) || string.IsNullOrEmpty(MAIL_USER_PWD) || string.IsNullOrEmpty(MAIL_POP) || string.IsNullOrEmpty(MAIL_SENDER) ||
                    string.IsNullOrEmpty(PRT_COUNT) || string.IsNullOrEmpty(COMPANY_NAME))
                {
                    QueryUser();
                }

                try
                {
                    popMail.Connect(MAIL_POP, HtmlTextPath.EMAIL_PORT, false);
                }
                catch (Exception ex)
                {
                    Console.Out.WriteLine("ex:" + ex);
                    SetRichTextValue(DateTime.Now.ToString("o") + @"######Internet connection failed######");
                    return;
                    //throw;
                }

                //登录邮箱地址
                popMail.Login(MAIL_USER_NAME, MAIL_USER_PWD);
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine("ex:" + ex);
                //richTextBox1.Text += System.Environment.NewLine + "ex:" + ex;
                //Console.Out.WriteLine("Can not connect email server" + DateTime.Now.ToString("o"));
                //richTextBox1.Text += System.Environment.NewLine + "Can not connect email server:" + DateTime.Now.ToString("o");
                //SetRichTextValue("MAIL_USER_NAME:" + MAIL_USER_NAME + "MAIL_USER_PWD:" + MAIL_USER_PWD + "MAIL_POP:" + MAIL_POP + "MAIL_SENDER:" + MAIL_SENDER + "PRT_COUNT:" + PRT_COUNT + "COMPANY_NAME:" + COMPANY_NAME);
                SetRichTextValue(DateTime.Now.ToString("o") + @"######Can not connect email server######");
                return;
            }

            Console.Out.WriteLine(DateTime.Now.ToString("o"));
            SetRichTextValue(DateTime.Now.ToString("o"));
            POP3_ClientMessageCollection messagesCollection = popMail.Messages;

            POP3_ClientMessage message = null;

            //存放需删除的邮件
            List <POP3_ClientMessage> lstMessage = new List <POP3_ClientMessage>();

            string strMessage = "";

            if (0 < messagesCollection.Count)
            {
                //for (int i = messagesCollection.Count - 1; i >= 0; i--)
                for (var index = 0; index < popMail.Messages.Count; index++)
                {
                    POP3_ClientMessage mail = popMail.Messages[index];

                    try
                    {
                        message = mail;
                    }
                    catch (Exception)
                    {
                        popMail.Timeout = HtmlTextPath.EMAIL_TIME_OUT;
                        popMail.Connect(MAIL_POP, HtmlTextPath.EMAIL_PORT, true);
                        popMail.Login(MAIL_USER_NAME, MAIL_USER_PWD);
                    }

                    Mail_Message mailMessage = null;

                    try
                    {
                        if (message != null)
                        {
                            byte[] messBytes = message.MessageToByte();
                            mailMessage = Mail_Message.ParseFromByte(messBytes);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    catch (Exception)
                    {
                        SetRichTextValue(DateTime.Now.ToString("o") + @"ERR GET MSG TO BYTE");
                        continue;
                        //Console.WriteLine(@"ERR GET MSG TO BYTE:" + DateTime.Now.ToString("o"));
                        //throw;
                    }


                    sendmail = mailMessage.From[0].Address;
                    HtmlBody = mailMessage.BodyHtmlText;

                    if (!sendmail.Equals(MAIL_SENDER))
                    {
                        message.MarkForDeletion();
                        SetRichTextValue(DateTime.Now.ToString("o") + @"######===" + sendmail + "===Message discarded#####");
                        continue;
                    }

                    //PrtOrder(HtmlBody.Replace("脳", "×").Replace("拢", "£"));
                    //PrtOrderWithTemplate(HtmlBody);
                    if (VERSION.Equals("2"))
                    {
                        HtmlBody = HtmlBody.Replace("<body style=\"", "<body style=\"font-family:Arial; ");
                    }
                    else if (VERSION.Equals("3"))
                    {
                        HtmlBody = HtmlBody.Replace("<body", "<body style=\"font-family:Arial; \"");
                    }

                    //读取Html失败时,跳出循环
                    if (!GetPrtInfo(HtmlBody))
                    {
                        message.MarkForDeletion();
                        SetRichTextValue(DateTime.Now.ToString("o") + @"######===" + orderId + "===OLD Message discarded#####");
                        continue;
                    }

                    ////DataGridView中的订单号不能重复
                    //for (int j = 0; j < dgvOrder.RowCount; j++)
                    //{
                    //    if (dgvOrder.Rows[j].Cells[0].Value != null)
                    //    {
                    //        if (!string.IsNullOrEmpty(dgvOrder.Rows[j].Cells[0].Value.ToString()))
                    //        {
                    //            if (dgvOrder.Rows[j].Cells[0].Value.ToString().Equals(orderId))
                    //            {
                    //                message.MarkForDeletion();
                    //                SetRichTextValue(DateTime.Now.ToString("o") + @"######===" + orderId + "===DGV Message discarded#####");
                    //                continue;
                    //            }
                    //        }
                    //    }
                    //}

                    //存在订单时不打印
                    //if (SqlHelper.QueryId(@"SELECT mailID FROM Mail_ID WHERE orderID='" + orderId + "'"))
                    //{
                    //    SetRichTextValue(DateTime.Now.ToString("o") + @"######Duplicate order ID######");
                    //    continue;
                    //}

                    if (VERSION.Equals("2"))
                    {
                        HtmlBody = HtmlBody.Replace("h1", "h4").Replace("<p>", "").Replace("</p>", "<br />")
                                   .Replace("<p style=\"width:94%;\">", "").Replace("<strong>", "").Replace("</strong>", "");
                        //HtmlBody = HtmlBody.Replace("h1", "h5");
                        HtmlBody = HtmlBody.Replace("<h4>", "").Replace("</h4>", "").Replace("<b>", "")
                                   .Replace("</b>", "")
                                   .Replace("border-top:hidden;", "").Replace("style=\"border-top:hidden;\"", "");
                    }
                    else if (VERSION.Equals("3"))
                    {
                        //中文字体更大一号字体
                        HtmlBody = HtmlBody.Replace("<span style=\"font-size:18px;\">",
                                                    "<span style=\"font-size:24px;\">");
                        HtmlBody = HtmlBody.Replace("h1", "h4").Replace("<p>", "").Replace("</p>", "<br />")
                                   .Replace("<p style=\"width:94%;\">", "").Replace("<strong>", "").Replace("</strong>", "");
                        //HtmlBody = HtmlBody.Replace("h1", "h5");
                        HtmlBody = HtmlBody.Replace("<h4>", "").Replace("</h4>", "").Replace("<b>", "")
                                   .Replace("</b>", "")
                                   .Replace("border-top:hidden;", "").Replace("style=\"border-top:hidden;\"", "");
                    }


                    //打印完成后插入数据
                    if (!SqlHelper.InsertId(
                            @"INSERT INTO Mail_ID(mailID, orderID, orderType, orderTime, orderHtmlBody) VALUES('"
                            + mail.UID + "', '"
                            + orderId + "', '"
                            + orderType + "', '"
                            + orderDate + "', '')"))
                    {
                        MessageBox.Show(@"WRITE Data Error!", @"ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        //int index = this.dgvOrder.Rows.Add();
                        this.dgvOrder.Rows.Insert(0, dgvOrder.Rows);
                        dgvOrder.Rows[0].Cells[0].Value = orderId;
                        dgvOrder.Rows[0].Cells[1].Value = orderDate;
                        dgvOrder.Rows[0].Cells[2].Value = orderType;
                        dgvOrder.Rows[0].Cells[3].Value = HtmlBody;

                        dgvOrder.Refresh();
                    }

                    //播放语音提示
                    if (File.Exists(Environment.CurrentDirectory + wavNewMail))
                    {
                        SoundPlayer player = new SoundPlayer(Environment.CurrentDirectory + wavNewMail);
                        player.Play();
                    }


                    SetRichTextValue(@"#Time Printing order number=" + orderId);

                    PrtContent(HtmlBody);

                    //完成后添加删除邮件
                    lstMessage.Add(message);
                }

                for (int i = lstMessage.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        lstMessage[i].MarkForDeletion();
                        Console.WriteLine(@"DELETE MAIL DONE:" + DateTime.Now.ToString("o"));
                    }
                    catch (Exception)
                    {
                        Console.WriteLine(@"DELETE MAIL FAILURE:" + DateTime.Now.ToString("o"));
                        //throw;
                    }
                }

                if (popMail != null)
                {
                    //popMail = null;
                    try
                    {
                        popMail.Disconnect();
                    }
                    catch (Exception)
                    {
                        //Console.Out.WriteLine("Error if (popMail != null)");
                        SetRichTextValue(@"Error POPMail != null");
                    }
                }
            }
            else
            {
                try
                {
                    SetRichTextValue(DateTime.Now.ToString("o") + @"#No mail received#");
                    popMail.Disconnect();
                }
                catch (Exception)
                {
                    //Console.Out.WriteLine("Error else");
                    SetRichTextValue(@"Error ELSE");
                }
            }
        }