using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Mail; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //public static string CreateTimeoutTestMessage(string server) //{ // string Success = "发送成功"; // try // { // string _to = "[email protected]"; // string _from = "[email protected]"; // string _subject = "Using the new SMTP client."; // string _body = @"Using this new feature, you can send an e-mail message from an application very easily."; // MailMessage message = new MailMessage(); // message.From = new MailAddress(_from); // //可以利用MailMessage.To.Add方法增加要发送的邮件地址 // message.To.Add(new MailAddress("[email protected]")); // message.To.Add(new MailAddress(_to)); // message.Subject = _subject; // message.Body = _body; // //添加附件 // Attachment a = new Attachment(@"C:/Users/Administrator/Desktop/kk.txt"); // message.Attachments.Add(a); // //设置邮箱的地址或IP // SmtpClient client = new SmtpClient(server); // //设置邮箱端口,pop3端口:110, smtp端口是:25 // client.Port = 25; // //设置超时时间 // client.Timeout = 9999; // //要输入邮箱用户名与密码 // client.Credentials = new NetworkCredential("[email protected]", "zddszx"); // client.Send(message); // } // catch (Exception ex) // { // Success = ex.ToString(); // } // return Success; //} private void button1_Click(object sender, EventArgs e) { string fuwuqi = textBox1.Text; string zhanghao = textBox2.Text; string mima = textBox3.Text; string dizhi = textBox4.Text; string biaoti = textBox5.Text; string neirong = textBox6.Text; SendMail(fuwuqi, zhanghao, mima, dizhi, biaoti, neirong); } public static void SendMail(string fuwuqi, string zhanghao, string mima, string dizhi, string biaoti, string neirong) { SmtpClient client = new SmtpClient(fuwuqi); //邮件类,造一个对象client client.Credentials = new System.Net.NetworkCredential(zhanghao, mima); //验证身份,通过发件人的地址和密码 client.DeliveryMethod = SmtpDeliveryMethod.Network; //指定如何发送电子邮件 System.Net.Mail.MailMessage message = new MailMessage(zhanghao, dizhi, biaoti, neirong); message.BodyEncoding = System.Text.Encoding.UTF8; //字符编码为Utf8 message.IsBodyHtml = true; //发送格式为html格式 try { client.Send(message); //讲指定的邮件发送到SMTP服务器以便传递 MessageBox.Show("发送成功"); } catch (Exception ex) { MessageBox.Show("错误信息:" + ex.Message, "错误"); } } private void Form1_Load(object sender, EventArgs e) { textBox1.Text = "smtp.163.com"; textBox2.Text = "[email protected]"; textBox3.Text = "zddszx"; textBox4.Text = "[email protected]"; } private void button2_Click(object sender, EventArgs e) { string fuwuqi = textBox1.Text; //服务器 string mima = textBox3.Text; string biaoti = textBox5.Text; //邮件标题 string neirong = textBox6.Text; //邮件的内容 for (int i = 0; i < listView1.SelectedItems.Count; i++) { string dizhi = listView1.SelectedItems[i].SubItems[1].ToString();//收件地址 string zhanghao = listView1.SelectedItems[i].SubItems[2].ToString(); //string mima = listView1.SelectedItems[i].SubItems[3].ToString(); SendMail(fuwuqi, zhanghao, mima, dizhi, biaoti, neirong); //调用发送邮件方法需要输入六个参数 System.Threading.Thread.Sleep(2000); } } int a = 0; private void button3_Click(object sender, EventArgs e) { listView1.Columns.Clear(); listView1.Columns.Add("编号"); listView1.Columns.Add("收件邮箱", 120); listView1.Columns.Add("发件箱", 120); //listView1.Columns.Add("密码", 120); listView1.GridLines = true; for (int i = 1; i <= 100; i++) { listView1.Items.Add("" + i); } listView1.Items[a].SubItems.Add(textBox4.Text); listView1.Items[a].SubItems.Add(textBox2.Text); listView1.Items[a].SubItems.Add(textBox3.Text); a++; } } }
发送邮件的邮箱要先启用SMTP,否则不能发送
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Net.Mail; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnfasong_Click(object sender, EventArgs e) { try { MailAddress EmailFrom = new MailAddress(txtsend.Text); //发件人邮箱地址 string EmailTo = txtto.Text; //收件人邮箱地址 string Emailbiaoti = txtbiaoti.Text; //邮件标题 string Emailneirong = txtnrong.Text; //邮件内容 fasong(EmailTo, Emailbiaoti, Emailneirong, EmailFrom); } catch (Exception ex) { MessageBox.Show(ex.Message); } } public void fasong(string EmailTo, string Emailbiaoti, string Emailneirong, MailAddress EmailFrom) { try { txtto.Text = txtto.Text.Replace(" ", "");//去除空格 txtto.Text = txtto.Text.Trim(); MailMessage email = new MailMessage(); email.From = EmailFrom; email.To.Add(EmailTo);//收件人邮箱地址可以是多个以实现群发 email.Subject = Emailbiaoti;//获取邮件的标题 email.Body = Emailneirong;//获取邮件的内容 email.IsBodyHtml = false; //是否为html格式 email.Priority = MailPriority.Normal; //发送邮件的优先等级 SmtpClient sc = new SmtpClient();//允许使用SMTP协议发送邮件 sc.Host =txthost.Text;//指定发送邮件的服务器地址 sc.Port = 25;//指定发送邮件端口 sc.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何发送电子邮件,通过网络发送到SMTP服务器 sc.UseDefaultCredentials = false;//是否随请求一起发送 sc.EnableSsl = false;//安全连接设置 sc.Credentials = new System.Net.NetworkCredential(txtsend.Text, txtsenderpwd.Text); //指定登录服务器的用户名和密码 sc.Send(email); MessageBox.Show("发送成功!"); } catch (Exception e) { MessageBox.Show(e.Message); } } private void Form1_Load(object sender, EventArgs e) { txthost.Text = "smtp.qq.com"; txtsend.Text = "[email protected]"; txtsenderpwd.UseSystemPasswordChar = true; } } }
时间: 2024-09-27 01:45:04