C# 邮件发送
2011-12-06 11:02
183 查看
我直接上图 上代码吧。
代码:
以上是完整代码。
项目包下载地址:http://files.cnblogs.com/andrew-blog/SendMailExample.rar
代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.IO; namespace SendMailExample { /// <summary> /// 作者:Andrew /// Blog: http://blog.csdn.net/Andrew_wx /// </summary> public partial class FormSendMail : Form { public FormSendMail() { InitializeComponent(); } private void FormSendMail_Load(object sender, EventArgs e) { txtSmtpServer.Text = "smtp.qq.com"; txtSend.Text = "heuandmei@qq.com"; txtDisplayName.Text = "Andrew(王旭)"; txtPassword.Text = "";//密码 txtReceive.Text = "heuandmei@qq.com"; txtTitle.Text = "发信测试"; txtBody.Text = "This is a test(测试)"; rbtnNoSSL.Checked = true; } private void btnAddFiles_Click(object sender, EventArgs e) { OpenFileDialog odlg = new OpenFileDialog(); odlg.CheckFileExists = true; //只接收有效的文件名 odlg.ValidateNames = true; //允许一次选择多个文件作为附件 odlg.Multiselect = true; if (odlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { lstFiles.Items.AddRange(odlg.FileNames); } } private void btnSend_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; MailMessage mail = new MailMessage(); mail.From = new MailAddress( txtSend.Text, txtDisplayName.Text, Encoding.UTF8); mail.To.Add(txtReceive.Text); mail.Subject = txtTitle.Text; mail.SubjectEncoding = Encoding.Default; mail.Body = txtBody.Text; mail.BodyEncoding = Encoding.Default; mail.IsBodyHtml = false; mail.Priority = MailPriority.Normal; //添加附件 Attachment attachment = null; if (lstFiles.Items.Count > 0) { for (int i = 0; i < lstFiles.Items.Count; i++) { string pathFileName = lstFiles.Items[i].ToString(); string extName = Path.GetExtension(pathFileName).ToLower(); //判断附件类型 if (extName == ".rar" || extName == ".zip") { attachment = new Attachment(pathFileName, MediaTypeNames.Application.Zip); } else { attachment = new Attachment(pathFileName, MediaTypeNames.Application.Octet); } ContentDisposition cd = attachment.ContentDisposition; cd.CreationDate = File.GetCreationTime(pathFileName); cd.ModificationDate = File.GetLastWriteTime(pathFileName); cd.ReadDate = File.GetLastAccessTime(pathFileName); mail.Attachments.Add(attachment); } } SmtpClient client = new SmtpClient(); client.Host = txtSmtpServer.Text; client.Port = 25; //是否使用安全套接字层加密连接 client.EnableSsl = rbtnUseSSL.Checked; //不使用默认凭证,注意此句必须放在 client.Credentials 的上面 client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential(txtSend.Text, txtPassword.Text); //邮件通过网络直接发送到服务器 client.DeliveryMethod = SmtpDeliveryMethod.Network; try { client.Send(mail); MessageBox.Show("发送成功"); } catch (SmtpException ex) { MessageBox.Show("发送失败:" + ex.Message); } catch (Exception ex) { MessageBox.Show("发送失败:" + ex.Message); } finally { mail.Dispose(); client = null; this.Cursor = Cursors.Default; } } } }
以上是完整代码。
项目包下载地址:http://files.cnblogs.com/andrew-blog/SendMailExample.rar
相关文章推荐
- C# 发送邮件整理,包括控制台程序、WPF、WebForm 及 ASP.NET MVC
- C# 邮件的发送,抄送
- C#发送邮件
- C# 邮件发送方法【webMail方式】
- Jmail c# 使用 发送邮件完全解决方案
- C# 调用outlook 发送邮件 或添加附件发送
- 可以完成发送邮件功能的C#代码
- C#发送邮件
- c# 发送邮件
- C# 如何通过mailto标签和SMTP协议两种方式发送邮件
- C# 发送邮件
- C# 发送邮件内容嵌入图片
- c#编写邮件发送系统
- c# 发送邮件
- C#批量发送邮件程序
- C#发送邮件-C#教程
- C#实现发送邮件的三种方法
- C#发送邮件
- 学习记录 用C#.net 加 asp.net发送带附件邮件