C#发送邮件(添加附件)!
2012-09-04 11:16
489 查看
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Mail; namespace WindowsFormsApplication63 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //打开上传附件的对话框 private void btnUP_Click(object sender, EventArgs e) { oFDialogSFile.InitialDirectory = "C:\\";//设置对话框的初始目录为C盘 oFDialogSFile.Filter = "all files (*.*)|*.*";//筛选字符串为所有文件 oFDialogSFile.RestoreDirectory = true; oFDialogSFile.ShowDialog(); cboxAccessories.Items.Add(oFDialogSFile.FileName.Trim());//当选择好文件后将文件名赋值给下拉框 } //发送邮件 private void btnSend_Click(object sender, EventArgs e) { try { string file = Application.StartupPath + "testXML.xml"; //SmtpClient下的一个对象,用以设置邮件的主题和内容 System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage(); //发送端到接收端的邮箱地址 myMail = new System.Net.Mail.MailMessage(txtSEmail.Text.Trim(), txtCEmail.Text.Trim()); myMail.Subject = txtETitle.Text.Trim(); myMail.Body = txtEContent.Text.Trim(); if (cboxAccessories.Items.Count > 0) { for (int i = 0; i < cboxAccessories.Items.Count; i++) { //建立邮件附件类的一个对象,语法格式为System.Net.Mail.Attachment(文件名,文件格式) System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment( cboxAccessories.Items[i].ToString(), System.Net.Mime.MediaTypeNames.Application.Octet); //MIME协议下的一个对象,用以设置附件的创建时间,修改时间以及读取时间 System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); //用smtpclient对象里attachments属性,添加上面设置好的myattachment myMail.Attachments.Add(myAttachment); } } //建立发送对象client,验证邮件服务器,服务器端口,用户名,以及密码 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(txtSService.Text.Trim(), Convert.ToInt32(txtServicePort.Text.Trim())); client.Credentials = new System.Net.NetworkCredential(txtUPwd.Text.Trim(), txtCEmail.Text.Trim()); client.Send(myMail); MessageBox.Show("邮件发送成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void 删除上传文件_Click(object sender, EventArgs e) { if (cboxAccessories.Text == "") { MessageBox.Show("没有附件可删!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { cboxAccessories.Items.Remove(cboxAccessories.Text.Trim()); } } } }
效果图如下:
点击发送,效果如下
总结一下,发送邮件的过程如下:
1.建立System.Net.Mail.MailMessage下的对象,设置邮件的内容和主题。
2.如果要添加附件,建立System.Net.Mail.Attatch下的对象,用1中的对象中的添加附件的属性读取它。
3.SEND邮件(MAIL+附件)。
如果要实现群发,则可以将发送邮件的代码(TRY附近的几行)写成方法,循环调用即可。
相关文章推荐
- C#编程实现发送邮件的方法(可添加附件)
- C# 发送邮件(内容中可以插入图片,邮件可以添加附件,163邮箱、QQ邮箱等都可以使用)
- C# 发送邮件(内容中可以插入图片,邮件可以添加附件,163邮箱、QQ邮箱等都可以使用)
- C#邮件发送 批量发送邮件 发送邮件嵌入图片 添加附件
- C# 调用outlook 发送邮件 或添加附件发送
- C# 调用outlook 发送邮件 或添加附件发送
- C# 调用outlook 发送邮件 或添加附件发送
- C#实现邮件发送,可以添加附件,发件人、收件人信息从xml文件读取
- C# 发送邮件(内容中可以插入图片,邮件可以添加附件,163邮箱、QQ邮箱等都可以使用)
- C#发送邮件(包含添加附件)
- java 发送邮件(并添加附件)
- [C#]邮件发送类(带附件)
- C#发送带附件邮件
- Python自动发送邮件并添加附件的主程序
- java邮件自动发送时添加网络附件
- C#发送带附件邮件
- android利用javamail实现后台发送可添加附件的邮件
- C#实现邮件的发送与接受(包括附件)
- [C#]邮件发送类(带附件)
- 使用c#给outlook添加任务、发送邮件