Exemple #1
0
 private void dgvEmailInfo_CellClick(object sender, DataGridViewCellEventArgs e)//下载附件
 {
     try
     {
         if (e.ColumnIndex == 2)
         {
             mailMessage = popMail.Messages[e.RowIndex + 1];
             atts        = mailMessage.Attachments;
             if (atts.Count != 0)
             {
                 for (int k = 0; k < atts.Count; k++)
                 {
                     att = atts[k];
                     string attname = att.Name;
                     saveFileDialog.FileName = attname;//令对话框的初始路径包含文件名
                     if (saveFileDialog.ShowDialog() == DialogResult.OK)
                     {
                         string mailPath = saveFileDialog.FileName.ToString(); //获取对话框中选好的路径
                         att.SaveToFile(mailPath);                             //将附件存储到相应路径
                         MessageBox.Show("下载成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     }
                 }
             }
         }
     }
     catch
     {
         MessageBox.Show("下载失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Exemple #2
0
        // 下载附件
        private void btnDownLoad_Click(object sender, EventArgs e)
        {
            if (lstViewMailList.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择阅读的邮件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else if (lstViewMailList.SelectedItems[0].SubItems[3].Text == "无")
            {
                MessageBox.Show("该邮件没有附件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            int index = lstViewMailList.SelectedItems[0].Index;

            messageMail = popClient.Messages[index + 1];
            attachments = messageMail.Attachments;
            for (int i = 0; i < attachments.Count; i++)
            {
                attachment = attachments[i];
                string         attachName     = attachment.Name;
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.FileName = attachName;
                saveFileDialog.Filter   = "所有文件(*.*)|(*.*)";
                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                {
                    continue;
                }

                string filepath = saveFileDialog.FileName;
                attachment.SaveToFile(filepath);
                MessageBox.Show("以保存:\r\n" + attachment.Name, "下载完毕", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Exemple #3
0
 private void dgvEmailInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)//双击相应单元格得到详细邮件信息
 {
     try
     {
         strFrom = strSubject = strDate = strAttachment = string.Empty;
         strFrom = dgvEmailInfo.Rows[e.RowIndex].Cells[0].Value.ToString();
         if (dgvEmailInfo.Rows[e.RowIndex].Cells[1].Value == null)
         {
             strSubject = null;
         }
         else
         {
             strSubject = dgvEmailInfo.Rows[e.RowIndex].Cells[1].Value.ToString();
         }
         strDate     = dgvEmailInfo.Rows[e.RowIndex].Cells[3].Value.ToString();
         mailMessage = popMail.Messages[e.RowIndex + 1];
         atts        = mailMessage.Attachments;
         for (int k = 0; k < atts.Count; k++)
         {
             att = atts[k];
             if (strAttachment == string.Empty)
             {
                 strAttachment = att.Name;
             }
             else
             {
                 strAttachment += ";" + att.Name;//得到所有的附件名并以逗号隔开
             }
         }
         frmEmailInfo frmemailinfo = new frmEmailInfo();
         frmemailinfo.ShowDialog();
     }
     catch
     {
     }
 }
Exemple #4
0
        // 发送邮件
        private void btnSend_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            // 实例化一个发送的邮件
            // 相当于与现实生活中先写信,程序中把信(邮件)抽象为邮件类了
            MailMessage mailMessage = new MailMessage();

            // 指明邮件发送的地址,主题,内容等信息
            // 发信人的地址为登录收发器的地址,这个收发器相当于我们平时Web版的邮箱或者是OutLook中配置的邮箱
            mailMessage.From = new MailAddress(tbxUserMail.Text);
            mailMessage.To.Add(txbSendTo.Text);
            mailMessage.Subject         = txbSubject.Text;
            mailMessage.SubjectEncoding = Encoding.Default;
            mailMessage.Body            = richtbxBody.Text;
            mailMessage.BodyEncoding    = Encoding.Default;
            // 设置邮件正文不是Html格式的内容
            mailMessage.IsBodyHtml = false;
            // 设置邮件的优先级为普通优先级
            mailMessage.Priority = MailPriority.Normal;
            //mailMessage.ReplyTo = new MailAddress(tbxUserMail.Text);

            // 封装发送的附件
            System.Net.Mail.Attachment attachment = null;
            if (cmbAttachment.Items.Count > 0)
            {
                for (int i = 0; i < cmbAttachment.Items.Count; i++)
                {
                    string fileNamePath = cmbAttachment.Items[i].ToString();
                    string extName      = Path.GetExtension(fileNamePath).ToLower();
                    if (extName == ".rar" || extName == ".zip")
                    {
                        attachment = new System.Net.Mail.Attachment(fileNamePath, MediaTypeNames.Application.Zip);
                    }
                    else
                    {
                        attachment = new System.Net.Mail.Attachment(fileNamePath, MediaTypeNames.Application.Octet);
                    }

                    // 表示MIMEContent-Disposition标头信息
                    // 对于ContentDisposition具体类的解释大家可以参考MSDN
                    // 这里我就不重复贴出来了,给个地址: http://msdn.microsoft.com/zh-cn/library/System.Net.Mime.ContentDisposition.aspx (着重看备注部分)
                    ContentDisposition cd = attachment.ContentDisposition;
                    cd.CreationDate     = File.GetCreationTime(fileNamePath);
                    cd.ModificationDate = File.GetLastWriteTime(fileNamePath);
                    cd.ReadDate         = File.GetLastAccessTime(fileNamePath);
                    // 把附件对象加入到邮件附件集合中
                    mailMessage.Attachments.Add(attachment);
                }
            }

            // 发送写好的邮件
            try
            {
                // SmtpClient类用于将邮件发送到SMTP服务器
                // 该类封装了SMTP协议的实现,
                // 通过该类可以简化发送邮件的过程,只需要调用该类的Send方法就可以发送邮件到SMTP服务器了。
                smtpClient.Send(mailMessage);
                MessageBox.Show("邮件发送成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (SmtpException smtpError)
            {
                MessageBox.Show("邮件发送失败:[" + smtpError.StatusCode + "];["
                                + smtpError.Message + "];\r\n[" + smtpError.StackTrace + "]."
                                , "错误", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
            }
            finally
            {
                mailMessage.Dispose();
                this.Cursor = Cursors.Default;
            }
        }