如何把C#邮件发送代码改成群发机?长知识了!

  • 发表时间:2021-08-23 00:03:34
  • 文章来源:网络

– -,

我直接上图 上代码吧。

 

代码:

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

参考:http://www.wxzzz.com/?id=66