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 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); } }