单发邮箱 群发邮箱 程序 Email winform

时间:2022-01-08 06:09:56

单发邮箱  群发邮箱 程序 Email winform

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;
//在下面添加你想要初始化的内容,比如显示姓名、用户名等
}

private void btn_send_Click(object sender, EventArgs e)
{
try
{
MessageBox.Show(sendEmail(txtEmail.Text));
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}

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);

//构造一个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;

//设置邮件的信息
client.DeliveryMethod = SmtpDeliveryMethod.Network;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = false;

//如果服务器支持安全连接,则将安全连接设为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;
}
}
}

单发邮箱 群发邮箱 程序 Email winform的更多相关文章

  1. Node&period;js 把抓取到的电影节目列表单发或者群发到QQ邮箱

    代码地址如下:http://www.demodashi.com/demo/12381.html 一.前言 上一节我们演示了如何用Node的各种包去抓取电影天堂最新电影列表,接下来我们会讲解如何发送我们 ...

  2. C&num;邮件发送&lpar;最坑爹的邮箱-QQ邮箱&rpar;

    最近工作挺清闲的,有空的时候陪妹子出去玩玩,自己看看小说,看看电影,日子过的挺欢乐的,这个星期幡然悔悟,代码才是我的最爱,做点小东西,就写个邮件发送程序.说的邮件发送相信工作过基本上都会用到过,用户注 ...

  3. C&num;邮件发送&lpar;最坑爹的邮箱-QQ邮箱&rpar;---转发(SmallFlyElephant)

    C#邮件发送(最坑爹的邮箱-QQ邮箱) 最近工作挺清闲的,有空的时候陪妹子出去玩玩,自己看看小说,看看电影,日子过的挺欢乐的,这个星期幡然悔悟,代码才是我的最爱,做点小东西,就写个邮件发送程序.说的邮 ...

  4. 黑马程序员&plus;Winform基础&lpar;上&rpar;

    黑马程序员+Winform基础 ---------------<a href="http://edu.csdn.net"target="blank"&gt ...

  5. 十、&period;NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件

    十..NET使用本地Outlook邮箱指定邮箱用户名和密码发送邮件 1.添加Microsoft.Office.Interop.Outlook引用 2.封装发送邮件方法 using System; us ...

  6. 模拟邮箱输入邮箱地址、收藏标签。input框输入内容后回车,内容显示成小方块并带删除按钮。

    模拟邮箱输入邮箱地址.收藏标签: 文本框输入文字后按回车键或者分号键,输入框中的文字变成小块并带删除按钮和操作. 页面代码: <!DOCTYPE html> <%@ page lan ...

  7. &period;Net 桌面程序(winform,wpf,跨平台avalonia)打安装包部署到windows 入门

    .Net 桌面程序(winform,wpf,跨平台avalonia)部署到windows 入门 本文以为avalonia为例,用Setup Factory 将.Net桌面程序(winform,wpf, ...

  8. C&num;-Excel导入工资条群发邮箱

    第一次写随笔,一名在实习的程序猿,做的一个小应用,需要的朋友可以参考参考, 使用WinForm实现了一个导入Excel,群发工资条的功能.功能已经实现,还不够完善,. 大致运用了OleDbConnec ...

  9. Delphi中,indy控件实现收发邮件的几点学习记录&lpar; 可以考虑加入多线程,用多个邮箱做一个邮箱群发器&rpar; 转

    关于用Delphi中的Indy控件实现收发邮件的几点学习记录             这几天心里颇不宁静,不是因为项目延期,而是因为自己几个月前做的邮件发送程序至今无任何进展,虽然一向谦虚的人在网上发 ...

随机推荐

  1. easyui Datagrid查询报错Uncaught TypeError&colon;Cannot read property &&num;39&semi;length&&num;39&semi; of undefined

    1.问题描述 easyui中datagrid执行loadData方法出现如下异常:Cannot read property 'length' of undefined 2.一开始怀疑是js或者页面的问 ...

  2. 什么是领域驱动设计&lpar;Domain Driven Design&rpar;&quest;

    本文是从 What is Domain Driven Design? 这篇文章翻译而来. ”…在很多领域,专家的作用体现在他们的专业知识上而不是智力上.“ -- Don Reinertsen 领域驱动 ...

  3. VISO下载地址

    http://pan.baidu.com/share/home?uk=4011207371#category/type=0

  4. 【转】JAVA之动态代理

    转自:像少年啦飞驰 代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 代理模式的结构如下图所示. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 代理模式示 ...

  5. Suricata&comma; to 10Gbps and beyond(X86架构)

    Introduction Since the beginning of July 2012, OISF team is able to access to a server where one int ...

  6. hdu4758 Walk Through Squares &lpar;AC自己主动机&plus;DP&rpar;

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...

  7. hdu&lowbar;5145&lowbar;NPY and girls&lpar;莫队算法&plus;组合&rpar;

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5145 题意:给你n,m,共有n个女孩,标号为1—n,n个数xi表示第ith个女孩在第xi个教室,然后下 ...

  8. &lbrack;模拟赛&rsqb; T2 不等数列

    Description 将1到n任意排列,然后在排列的每两个数之间根据他们的大小关系插入">"和"<".问在所有排列中,有多少个排列恰好有k个&qu ...

  9. Builgen 插件——IntelliJ IDEA和Eclipse Java Bean Builder模式代码生成器-比lombok更符合需求

    builder模式在越来越多的项目中使用,类似于alibaba fastjson JSONObject.fluentPut(),调用一个方法后返回这个对象本身,特别适合构建一些参数超级多的对象,代码优 ...

  10. NOIP2016 组合数问题

    https://www.luogu.org/problem/show?pid=2822 题目描述 组合数表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以 ...