C#实现发邮件(原码 + 图)
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.Mail;
using System.Net.Mime;
using System.Net;
using System.IO;
namespace 发邮件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//添加俩个smpt服务器的名称
cmbBoxSMTP.Items.Add("smtp.163.com");
cmbBoxSMTP.Items.Add("smtp.sina.com");
//设置为下拉列表
cmbBoxSMTP.DropDownStyle = ComboBoxStyle.DropDownList;
//默认选中第一个选项
cmbBoxSMTP.SelectedIndex = 0;
//在下面添加你想要初始化的内容,比如显示姓名、用户名等
}
private void button1_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中
treeView1.Nodes.Add(fileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
//判断是否选中了节点
if (treeView1.SelectedNode != null)
{
//得到选择的节点
TreeNode tempNode = treeView1.SelectedNode;
//删除选中的节点
treeView1.Nodes.Remove(tempNode);
}
else
{
MessageBox.Show("请选择要删除的附件。");
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
//确定smtp服务器地址。实例化一个Smtp客户端
SmtpClient client = new SmtpClient(cmbBoxSMTP.Text);
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential(txtUserName.Text, txtUserPassword.Text); //登录的用户名和密码
MailMessage mess = new MailMessage();
//为 message 添加附件
foreach (TreeNode treeNode in treeView1.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);
//向邮件添加附件
mess.Attachments.Add(attach);
}
else
{
MessageBox.Show("文件" + fileName + "未找到!");
}
}
//收信人地址
if (cmbBoxSMTP.Text == "smtp.163.com")
mess.From = new MailAddress(txtUserName.Text.Trim() + "@163.com", txtYouName.Text);
if (cmbBoxSMTP.Text == "smtp.sina.com")
mess.From = new MailAddress(txtUserName.Text.Trim() + "@sina.com", txtYouName.Text);
//发信人地址
mess.To.Add( new MailAddress(txtSendAddress.Text.Trim(), txtSendName.Text.Trim()));
//是否提示安全连接
client.EnableSsl = false;
//邮件主题
mess.Subject = txtTopic.Text.Trim();
//邮件正文
mess.Body = txtText.Text;
//发送邮件
client.Send(mess);
MessageBox.Show("完成");
}
catch (SmtpException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
经过测试:只能使用163和sina邮箱发信 接信可是任何邮箱
此程序为交流为目地 还需要加强和完善
希望对您有所帮助
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;
using System.Net;
using System.IO;
namespace 发邮件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//添加俩个smpt服务器的名称
cmbBoxSMTP.Items.Add("smtp.163.com");
cmbBoxSMTP.Items.Add("smtp.sina.com");
//设置为下拉列表
cmbBoxSMTP.DropDownStyle = ComboBoxStyle.DropDownList;
//默认选中第一个选项
cmbBoxSMTP.SelectedIndex = 0;
//在下面添加你想要初始化的内容,比如显示姓名、用户名等
}
private void button1_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中
treeView1.Nodes.Add(fileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
//判断是否选中了节点
if (treeView1.SelectedNode != null)
{
//得到选择的节点
TreeNode tempNode = treeView1.SelectedNode;
//删除选中的节点
treeView1.Nodes.Remove(tempNode);
}
else
{
MessageBox.Show("请选择要删除的附件。");
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
//确定smtp服务器地址。实例化一个Smtp客户端
SmtpClient client = new SmtpClient(cmbBoxSMTP.Text);
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential(txtUserName.Text, txtUserPassword.Text); //登录的用户名和密码
MailMessage mess = new MailMessage();
//为 message 添加附件
foreach (TreeNode treeNode in treeView1.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);
//向邮件添加附件
mess.Attachments.Add(attach);
}
else
{
MessageBox.Show("文件" + fileName + "未找到!");
}
}
//收信人地址
if (cmbBoxSMTP.Text == "smtp.163.com")
mess.From = new MailAddress(txtUserName.Text.Trim() + "@163.com", txtYouName.Text);
if (cmbBoxSMTP.Text == "smtp.sina.com")
mess.From = new MailAddress(txtUserName.Text.Trim() + "@sina.com", txtYouName.Text);
//发信人地址
mess.To.Add( new MailAddress(txtSendAddress.Text.Trim(), txtSendName.Text.Trim()));
//是否提示安全连接
client.EnableSsl = false;
//邮件主题
mess.Subject = txtTopic.Text.Trim();
//邮件正文
mess.Body = txtText.Text;
//发送邮件
client.Send(mess);
MessageBox.Show("完成");
}
catch (SmtpException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
经过测试:只能使用163和sina邮箱发信 接信可是任何邮箱
此程序为交流为目地 还需要加强和完善
希望对您有所帮助