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.Mime;
using System.Net;
using System.IO;
using System.Data.OleDb;安全
namespace SendEmail1._0
{
public partial class mail : Form
{
public mail()
{服務器
InitializeComponent();
}
List<string> listEmail = new List<string>();
/// <summary>
/// 窗體加載
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Mail_Load(object sender, EventArgs e)
{
//添加倆個smpt服務器的名稱
cmbBoxSMTP.Items.Add("smtp.163.com");
cmbBoxSMTP.Items.Add("SMTP.QQ.COM");
cmbBoxSMTP.Items.Add("smtp.gmail.com");
//設置爲下拉列表
cmbBoxSMTP.DropDownStyle = ComboBoxStyle.DropDownList;
//默認選中第一個選項
cmbBoxSMTP.SelectedIndex = 1;
//在下面添加你想要初始化的內容,好比顯示姓名、用戶名等
}ide
private void btn_send_Click(object sender, EventArgs e)
{
try
{
MessageBox.Show(sendEmail(txtEmail.Text));
}
catch (Exception ex)
{spa
MessageBox.Show(ex.Message);
}
}orm
private string sendEmail(string ToEmail)
{
try
{
//肯定smtp服務器地址。實例化一個Smtp客戶端
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(cmbBoxSMTP.Text);
//生成一個發送地址
string strFrom = string.Empty;
if (cmbBoxSMTP.SelectedIndex == 0)
{
strFrom = txtUserName.Text + "@163.com";
}
else if (cmbBoxSMTP.SelectedIndex == 1)
{
strFrom = txtUserName.Text + "@QQ.com";
}
else
{
strFrom = txtUserName.Text + "@gmail.com";
}對象
//構造一個發件人地址對象
MailAddress from = new MailAddress(strFrom, txtDisplayName.Text, Encoding.UTF8);
//構造一個收件人地址對象
MailAddress to = new MailAddress(txtEmail.Text, txtToName.Text, Encoding.UTF8);blog
//構造一個Email的Message對象
MailMessage message = new MailMessage();
message.To.Add(ToEmail);
//message.To.Add("344283361@qq.com");
message.From = from;
//爲 message 添加附件
foreach (TreeNode treeNode in treeViewFileList.Nodes)
{
//獲得文件名
string fileName = treeNode.Text;
//判斷文件是否存在
if (File.Exists(fileName))
{
//構造一個附件對象
Attachment attach = new Attachment(fileName);
//獲得文件的信息
ContentDisposition disposition = attach.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(fileName);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(fileName);
disposition.ReadDate = System.IO.File.GetLastAccessTime(fileName);
//向郵件添加附件
message.Attachments.Add(attach);
}
else
{
MessageBox.Show("文件" + fileName + "未找到!");
}
}字符串
//添加郵件主題和內容
message.Subject = txtSubject.Text;
message.SubjectEncoding = Encoding.UTF8;
message.Body = rtxtBody.Text;
message.BodyEncoding = Encoding.UTF8;string
//設置郵件的信息
client.DeliveryMethod = SmtpDeliveryMethod.Network;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = false;it
//若是服務器支持安全鏈接,則將安全鏈接設爲true。
//gmail支持,163不支持,若是是gmail則必定要將其設爲true
if (cmbBoxSMTP.SelectedText == "smpt.163.com")
client.EnableSsl = false;
else
client.EnableSsl = true;
//設置用戶名和密碼。
//string userState = message.Subject;
client.UseDefaultCredentials = false;
string username = txtUserName.Text;
string passwd = txtPassword.Text;
//用戶登錄信息
NetworkCredential myCredentials = new NetworkCredential(username, passwd);
client.Credentials = myCredentials;
//發送郵件
client.Send(message);
//提示發送成功
return "成功:" + ToEmail;
// MessageBox.Show("發送成功!");
}
catch (Exception ex)
{
return "失敗:" + ToEmail + ";緣由:" + ex.Message;
//MessageBox.Show(ex.Message);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{ //定義並初始化一個OpenFileDialog類的對象
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = Application.StartupPath;
openFile.FileName = "";
openFile.RestoreDirectory = true;
openFile.Multiselect = false;
//顯示打開文件對話框,並判斷是否單擊了肯定按鈕
if (openFile.ShowDialog() == DialogResult.OK)
{
//獲得選擇的文件名
string fileName = openFile.FileName;
//將文件名添加到TreeView中
treeViewFileList.Nodes.Add(fileName);
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
//判斷是否選中了節點
if (treeViewFileList.SelectedNode != null)
{
//獲得選擇的節點
TreeNode tempNode = treeViewFileList.SelectedNode;
//刪除選中的節點
treeViewFileList.Nodes.Remove(tempNode);
}
else
{
MessageBox.Show("請選擇要刪除的附件。");
}
}
/// <summary>
/// 羣發
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnsends_Click(object sender, EventArgs e)
{
List<string> list = new List<string>();
int sendnum = 0;
string s = txtToEmails.Text;
string[] sArray = s.Split(new char[] { '\r', '\n' });
for (int i = 0; i < sArray.Length; i++)
{
// sArray[i] 即爲每一行內容
if (!string.IsNullOrEmpty(sArray[i]))
{
list.Add(sArray[i]);
}
}
foreach (var item in list)
{
string str = sendEmail(item);
if (str.Split(':')[0].Equals("成功"))
{
sendnum += 1;
txtlog.Text += str + System.Environment.NewLine;
}
else
{
txtlog.Text += str + System.Environment.NewLine;
}
}
MessageBox.Show("成功發送" + sendnum + "條郵件");
}
/// <summary>
/// 上傳聯繫人
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpToEmail_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = Application.StartupPath;
openFile.FileName = "";
openFile.RestoreDirectory = true;
openFile.Multiselect = false;
//顯示打開文件對話框,並判斷是否單擊了肯定按鈕
if (openFile.ShowDialog() == DialogResult.OK)
{
//獲得選擇的文件名
string fileName = openFile.FileName;
//將文件名添加到TreeView中
// txtToEmails.Text=fileName;
DataSet ds = ExcelToDS(fileName);
foreach (DataRow cRow in ds.Tables[0].Rows)
{
txtToEmails.Text += cRow["Email"].ToString() + System.Environment.NewLine;
}
} } /// <summary> /// 讀取Excel /// </summary> /// <param name="Path"></param> /// <returns></returns> public DataSet ExcelToDS(string Path) { //此鏈接只能操做Excel2007以前(.xls)文件 // string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;"; //此鏈接能夠操做.xls與.xlsx文件 (支持Excel2003 和 Excel2007 的鏈接字符串) //備註: "HDR=yes;"是說Excel文件的第一行是列名而不是數據,"HDR=No;"正好與前面的相反。 // "IMEX=1 "若是列中的數據類型不一致,使用"IMEX=1"可必免數據類型衝突。 string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Path + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel = "select * from [sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds, "table1"); return ds; } }}