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); } }
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 { } }
private void frmMain_Load(object sender, EventArgs e) { string MailSubject = ""; for (int i = 0; i < dgvEmailInfo.Columns.Count; i++) { dgvEmailInfo.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } strsever = login.Pop; user = login.User; pwd = login.Password; popMail = new jmail.POP3Class(); try { popMail.Connect(user, pwd, strsever, 110);//通过jmail提供的方法直接连接服务器获取邮件 TLabUser.Text = user; TLabNum.Text = popMail.Count + "封"; if (popMail.Count != 0) { dgvEmailInfo.RowCount = popMail.Count;//显示与邮件封数相同的行数 if (popMail != null) { if (popMail.Count > 0) { for (int i = 1; i < popMail.Count + 1; i++) { mailMessage = popMail.Messages[i];//获取某一封邮件 transCode1(ref MailSubject, mailMessage); mailMessage.Charset = "GB18030"; mailMessage.Encoding = "Base64"; mailMessage.ISOEncodeHeaders = false; string priority = mailMessage.Priority.ToString(); //邮件编码等设置 dgvEmailInfo.Rows[i - 1].Cells[0].Value = mailMessage.From; //将发信人填到第一格 dgvEmailInfo.Rows[i - 1].Cells[1].Value = MailSubject; //将主题填到第二格 dgvEmailInfo.Rows[i - 1].Cells[3].Value = mailMessage.Date; //将时间填到第四格 if ((popMail.Count >= 1) && (i <= popMail.Count)) { atts = mailMessage.Attachments;//实例化邮件附件 if (atts.Count > 0) { dgvEmailInfo.Rows[i - 1].Cells[2].Value = "附件下载"; } else { dgvEmailInfo.Rows[i - 1].Cells[2].Value = "无附件"; } } dgvEmailInfo.Rows[0].Selected = false;//信息填充好后清除第一行被选中的状态 } } } } else { dgvEmailInfo.Rows.Clear(); } } catch { MessageBox.Show("该用户邮箱不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }