C#编程实现发送邮件的方法(可添加附件)
C#编程实现发送邮件的方法(可添加附件)
发布时间:2016-12-28 来源:查字典编辑
摘要:本文实例讲述了C#编程实现发送邮件的方法。分享给大家供大家参考,具体如下:usingSystem.Collections.Generic;u...

本文实例讲述了C#编程实现发送邮件的方法。分享给大家供大家参考,具体如下:

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

效果图如下:

C#编程实现发送邮件的方法(可添加附件)1

点击发送,效果如下

C#编程实现发送邮件的方法(可添加附件)2

总结一下,发送邮件的过程如下:

1.建立System.Net.Mail.MailMessage下的对象,设置邮件的内容和主题。

2.如果要添加附件,建立System.Net.Mail.Attatch下的对象,用1中的对象中的添加附件的属性读取它。

3.SEND邮件(MAIL+附件)。

如果要实现群发,则可以将发送邮件的代码(TRY附近的几行)写成方法,循环调用即可。

希望本文所述对大家C#程序设计有所帮助。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类