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.Net.Mail;
using System.Net.NetworkInformation;
using System.IO;
using System.Data.SqlClient;
using System.Text.RegularExpressions;//验证格式
namespace MailBox
{
public partial class MailForm : Form
{
public MailForm()
{
InitializeComponent();
}
List<string> Emailed = new List<string>();
List<string> EmailedF = new List<string>();
//发送邮件按钮
private void button1_Click(object sender, EventArgs e)
{
//填写验证,本验证只验证是否为空,不对输入的内容格式进行验证!
//设置邮件的各种属性
if (textBox3.Text == "" || !Regex.IsMatch(this.textBox3.Text, @"^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+"))
{ MessageBox.Show("请输入正确的邮箱账号。如:MMMM@XXX.com", "敬告"); return; }
if (textBox4.Text == "") { MessageBox.Show("请输入邮箱密码。", "敬告"); return; }
string[]toList = textBox5.Text.Trim().Split(',');
foreach(var v in toList)
{
MailMessage mail = new MailMessage();
//邮件的发件人
string user; user = textBox3.Text;
//发件人name
string[] sendusername = user.Split('@');
mail.From = new MailAddress(user, sendusername[0]);
//邮件的收件人
string to = v;
if (to == "" || !Regex.IsMatch(to, @"^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+"))
{ MessageBox.Show(to+"邮箱格式不对", "敬告"); return; }
mail.To.Add(new MailAddress(to, ""));
//邮件的抄送人
string cc; cc = textBox6.Text.Trim();
if (textBox6.Text != "")
{
if (!Regex.IsMatch(this.textBox6.Text, @"^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+"))
{ MessageBox.Show("请输入正确的抄送人地址!", "敬告"); return; }
else
{ mail.CC.Add(new MailAddress(cc, "")); }
}
//邮件的标题
if (textBox7.Text == "") { MessageBox.Show("请填写邮件主题。", "敬告"); return; }
if (textBox8.Text == "") { MessageBox.Show("如果不写邮件内容可能会被邮件服务器作为垃圾邮件而导致发送失败!", "敬告"); return; }
string subj; subj = textBox7.Text.Trim();
mail.Subject = subj;
//邮件的内容
string body; body = textBox8.Text.Trim();
mail.Body = body;
//邮件的格式
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
//邮件的发送级别
mail.Priority = MailPriority.Normal;
string mailattachment;
mailattachment = textBox9.Text.Trim();
if (textBox9.Text != "")
{
mail.Attachments.Add(new Attachment(mailattachment));
}
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
//设置SMTP服务器
SmtpClient client = new SmtpClient();
client.Host = "smtp." + sendusername[1].ToString(); //设置邮件协议
client.UseDefaultCredentials = false;
string passw; passw = textBox4.Text.Trim();
client.Credentials = new System.Net.NetworkCredential(user, passw);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//发送前检查网络
bool a = check("www.baidu.com");
if (a == false)
{
MessageBox.Show("离线状态,请检查网络!");
}
else
{
try
{
client.Send(mail);
Emailed.Add(v);
//textBox3.Text = "";
//textBox4.Text = "";
//textBox5.Text = "";
//textBox6.Text = "";
//textBox7.Text = "";
//textBox8.Text = "";
//textBox9.Text = "";
}
catch (System.Net.Mail.SmtpException ee)
{
EmailedF.Add(v+"提示:"+ee);
}
finally
{
mail.Dispose();
}
}
}
if (Emailed.Count > 0)
MessageBox.Show("已成功发送Email:" + string.Join(",", Emailed.ToArray()) ?? "none", "温柔提示");
if (EmailedF.Count > 0)
MessageBox.Show("邮件发送失败:" + string.Join(",", EmailedF.ToArray()) ?? "none", "温柔提示");
}
//网络测试
bool check(string ipadress)
{
Ping ping = new Ping();
PingReply res;
try
{
res = ping.Send(ipadress);
if (res.Status == System.Net.NetworkInformation.IPStatus.Success)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
//附件选择按钮
private void button2_Click(object sender, EventArgs e)
{
//打开附件路径
OpenFileDialog ofd = new OpenFileDialog();
//文件的默认路径
ofd.InitialDirectory = @"C:\Users\zorro_zuo\Desktop";
if (ofd.ShowDialog() == DialogResult.OK)
{
//显示文件路径
textBox9.Text = ofd.FileName;
}
}
//附件删除按钮
private void button3_Click(object sender, EventArgs e)
{
if (textBox9.Text == "")
MessageBox.Show("没有附件!");
else
{
textBox9.Text = "";
}
}
string strcon = MailBox.Properties.Settings.Default.CACHEMAILConnectionString;
}
}