使用C#编写简单的图形化的可发送附件的邮件客户端程序

2019-12-26 17:35:48王振洲

1.smtp服务信息设置


#region 设置Smtp服务器信息
/// <summary>
/// 设置Smtp服务器信息
/// </summary>
/// <param name="ServerName">SMTP服务名</param>
/// <param name="Port">端口号</param>
private void setSmtpClient(string ServerHost, int Port)
{
SmtpClient = new SmtpClient();
SmtpClient.Host = ServerHost;//指定SMTP服务名 例如QQ邮箱为 smtp.qq.com 新浪cn邮箱为 smtp.sina.cn等
SmtpClient.Port = Port; //指定端口号
SmtpClient.Timeout = 0; //超时时间
}
#endregion

2.验证发件人信息


#region 验证发件人信息
/// <summary>
/// 验证发件人信息
/// </summary>
/// <param name="MailAddress">发件邮箱地址</param>
/// <param name="MailPwd">邮箱密码</param>
private void setAddressform(string MailAddress, string MailPwd)
{
//创建服务器认证
NetworkCredential NetworkCredential_my = new NetworkCredential(MailAddress, MailPwd);
//实例化发件人地址
MailAddress_from = new System.Net.Mail.MailAddress(MailAddress, textBoxX4.Text);
//指定发件人信息 包括邮箱地址和邮箱密码
SmtpClient.Credentials = new System.Net.NetworkCredential(MailAddress_from.Address, MailPwd);
;
}
#endregion

3.添加附件


#region 检测附件大小
private bool Attachment_MaiInit(string path)
{
try
{
FileStream_my = new FileStream(path, FileMode.Open);
string name = FileStream_my.Name;
int size = (int)(FileStream_my.Length / 1024/1024);
FileStream_my.Close();
//控制文件大小不大于10M
if (size > 10)
{
MessageBox.Show("文件长度不能大于10M!你选择的文件大小为"+ size.ToString()+"M","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return false;
}
return true;
}
catch (IOException E)
{
MessageBox.Show(E.Message);
return false;
}
}

#endregion

4.正式发送邮件


 private void btnSend_Click(object sender, EventArgs e)
{
//检测附件大小 发件必需小于10M 否则返回 不会执行以下代码
if (txt_Path.Text != "")
{
if (!Attachment_MaiInit(txt_Path.Text.Trim()))
{
return;
}
}
if (txt_SmtpServer.Text == "")
{
MessageBox.Show("请输入SMTP服务器名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (textBoxX2.Text == "")
{
MessageBox.Show("请输入发件人邮箱地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (txtformPwd.Text == "")
{
MessageBox.Show("请输入发件人邮箱密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (dataGridViewX1.Rows.Count == 0)
{
MessageBox.Show("请添加收件人!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (MessageBox.Show("您确定要发送当前邮件吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
try
{
//初始化Smtp服务器信息
setSmtpClient("smtp." + txt_SmtpServer.Text.Trim() + comboBoxEx3.Text, Convert.ToInt32(numericUpDown1.Value));
}
catch (Exception Ex)
{
MessageBox.Show("邮件发送失败,请确定SMTP服务名是否正确!" + "n" + "技术信息:n" + Ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
//验证发件邮箱地址和密码
setAddressform(textBoxX2.Text.Trim() + comboBoxEx2.Text, txtformPwd.Text.Trim());
}
catch (Exception Ex)
{
MessageBox.Show("邮件发送失败,请确定发件邮箱地址和密码的正确性!" + "n" + "技术信息:n" + Ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//清空历史发送信息 以防发送时收件人收到的错误信息(收件人列表会不断重复)
MailMessage_Mai.To.Clear();
//添加收件人邮箱地址
foreach (DataGridViewRow row in dataGridViewX1.Rows)
{
MailAddress_to = new MailAddress(row.Cells["Column1"].Value.ToString());
MailMessage_Mai.To.Add(MailAddress_to);
}
MessageBox.Show("收件人:" + dataGridViewX1.Rows.Count.ToString() + " 个");
//发件人邮箱
MailMessage_Mai.From = MailAddress_from;

//邮件主题
MailMessage_Mai.Subject = txttitle.Text;
MailMessage_Mai.SubjectEncoding = System.Text.Encoding.UTF8;

//邮件正文
MailMessage_Mai.Body = Rtb_Message.Text;
MailMessage_Mai.BodyEncoding = System.Text.Encoding.UTF8;

//清空历史附件 以防附件重复发送
MailMessage_Mai.Attachments.Clear();

//添加附件
MailMessage_Mai.Attachments.Add(new Attachment(txt_Path.Text.Trim(), MediaTypeNames.Application.Octet));

//注册邮件发送完毕后的处理事件
SmtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

//开始发送邮件
SmtpClient.SendAsync(MailMessage_Mai, "000000000");

}
}