using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CSEmailTest
{
public partial class SendEmail : Form
{
public SendEmail()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
openFileTest.InitialDirectory = "D:\\";
openFileTest.Filter = "all files (*.*)|*.*";
openFileTest.ShowDialog();
MessageBox.Show(openFileTest.FileName.Trim());
listAttachment.Items.Add(openFileTest.FileName.Trim());
}
private void listAttachment_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
for (int i = listAttachment.SelectedItems.Count - 1; i > -1; i--)
{
listAttachment.Items.Remove(listAttachment.SelectedItems[i]);
}
}
}
private void btnSend_Click(object sender, EventArgs e)
{
Email email = new Email();
string orgialEmail=this.txtSendEmail.Text.Trim();
email.mailFrom = orgialEmail;
email.mailPwd = this.txtPwd.Text.Trim();
email.mailSubject =this.txtMySuject.Text.Trim();
email.mailBody = this.txtBody.Text.Trim();
email.isbodyHtml = true; //是否是HTML
int index =orgialEmail.LastIndexOf("@");
// email.host = "smtp."+orgialEmail.Substring(index+1);//如果是QQ邮箱则:smtp:qq.com,依次
email.host = "smtp.163.com";
List<string> listEmail = new List<string>();
foreach (var item in listToEmail.Items)
{
listEmail.Add(item.ToString());
}
email.mailToArray = listEmail.ToArray();//接收者邮件集合
List<string> listCCEmail= new List<string>();
foreach (var item in listCC.Items)
{
listCCEmail.Add(item.ToString());
}
email.mailCcArray = listCCEmail.ToArray();//抄送者邮件集合
List<string> listAtt = new List<string>();
foreach (var item in listAttachment.Items)
{
listAtt.Add(item.ToString());
}
email.attachmentsPath = listAtt.ToArray();//附件集合
if (email.Send())
{
MessageBox.Show("邮件发送成功~");
}
else
{
MessageBox.Show("邮件发送失败~");
}
}
private void btnAddToEmail_Click(object sender, EventArgs e)
{
listToEmail.Items.Add(this.txtToEmail.Text.Trim());
}
private void btnCC_Click(object sender, EventArgs e)
{
listCC.Items.Add(this.txtCCEmail.Text.Trim());
}
}
public class Email
{
/// <summary>
/// 发送者
/// </summary>
public string mailFrom { get; set; }
/// <summary>
/// 收件人
/// </summary>
public string[] mailToArray { get; set; }
/// <summary>
/// 抄送
/// </summary>
public string[] mailCcArray { get; set; }
/// <summary>
/// 标题
/// </summary>
public string mailSubject { get; set; }
/// <summary>
/// 正文
/// </summary>
public string mailBody { get; set; }
/// <summary>
/// 发件人密码
/// </summary>
public string mailPwd { get; set; }
/// <summary>
/// SMTP邮件服务器
/// </summary>
public string host { get; set; }
/// <summary>
/// 正文是否是html格式
/// </summary>
public bool isbodyHtml { get; set; }
/// <summary>
/// 附件
/// </summary>
public string[] attachmentsPath { get; set; }
public bool Send()
{
//使用指定的邮件地址初始化MailAddress实例
MailAddress maddr = new MailAddress(mailFrom);
//初始化MailMessage实例
MailMessage myMail = new MailMessage();
//向收件人地址集合添加邮件地址
if (mailToArray != null)
{
for (int i = 0; i < mailToArray.Length; i++)
{
myMail.To.Add(mailToArray[i].ToString());
}
}
//向抄送收件人地址集合添加邮件地址
if (mailCcArray != null)
{
for (int i = 0; i < mailCcArray.Length; i++)
{
myMail.CC.Add(mailCcArray[i].ToString());
}
}
//发件人地址
myMail.From = maddr;
//电子邮件的标题
myMail.Subject = mailSubject;
//电子邮件的主题内容使用的编码
myMail.SubjectEncoding = Encoding.UTF8;
//电子邮件正文
myMail.Body = mailBody;
//电子邮件正文的编码
myMail.BodyEncoding = Encoding.Default;
myMail.Priority = MailPriority.High;
myMail.IsBodyHtml = isbodyHtml;
//在有附件的情况下添加附件
try
{
if (attachmentsPath != null && attachmentsPath.Length > 0)
{
Attachment attachFile = null;
foreach (string path in attachmentsPath)
{
attachFile = new Attachment(path);
myMail.Attachments.Add(attachFile);
}
}
}
catch (Exception err)
{
throw new Exception("在添加附件时有错误:" + err);
}
SmtpClient smtp = new SmtpClient();
//指定发件人的邮件地址和密码以验证发件人身份
smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);
//设置SMTP邮件服务器
smtp.Host = host;
try
{
//将邮件发送到SMTP邮件服务器
smtp.Send(myMail);
return true;
}
catch (System.Net.Mail.SmtpException ex)
{
return false;
}
}
}
}
C#写的邮箱发送程序源代码---支持主题、正文、附件的编辑和添加!
最新推荐文章于 2020-06-11 14:45:43 发布