找传奇、传世资源到传世资源站!

c# smtp邮件发送示例源码(带附件/支持中文)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

c# smtp邮件发送示例源码(带附件/支持中文) C#语言基础-第1张using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net.Mail;using System.IO;using System.Net;namespace SendEmail{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } //保存名称 private string fileName = ""; //文件操作 FileStream fs = null; //设置Smtp协议 SmtpClient smtpClient = null; //设置发信人地址 需要密码 MailAddress AddressFrom = null; //设置收信人地址 不需要密码 MailAddress AddressTo = null; //邮件信息 MailMessage Message = null; //设置Smtp服务器信息 private void SetSmtpClient(string serverHost, int Port) { smtpClient = new SmtpClient(); //指定Smtp服务名 //QQ:smtp.qq.com //sina:smtp.sina.cn smtpClient.Host = serverHost; smtpClient.Port = Port; smtpClient.Timeout = 0; } //验证发件人信息 private void SetAddressFrom(string mailAddress, string mailPwd) { //创建服务器验证 NetworkCredential networkCreadential_My = new NetworkCredential(mailAddress,mailPwd); //实例化发件人地址 AddressFrom = new MailAddress(mailAddress,tbShowName.Text); //指定发件人信息 邮箱地址和密码 smtpClient.Credentials = new NetworkCredential(AddressFrom.Address, mailPwd); } //发送 private void btnSend_Click(object sender, EventArgs e) { if (MessageBox.Show("您确定要发送当前邮件吗?", "发送提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)==DialogResult.Yes) { try { //设置Smtp服务器信息 SetSmtpClient("smtp." cmbSmtp.Text cmbHZ.Text, Convert.ToInt32(nudPort.Value)); } catch (Exception err) { MessageBox.Show("邮件发送失败,请确定Smtp服务器名是否正确!" "\n" "技术信息:" err.Message); return; } try { if (tbPwd.Text == "") { MessageBox.Show("对不起,需要密码验证,请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); tbPwd.Focus(); return; } //设置发送地址信息 SetAddressFrom(tbSendEmail.Text.Trim() cmbSendHZ.Text, tbPwd.Text); } catch (Exception) { MessageBox.Show("邮件发送失败,请确认发送邮箱地址和密码的正确性!"); return; } //message信息 Message = new MailMessage(); //发信人地址 Message.From = AddressFrom; //收信人地址 AddressTo = new MailAddress(tbReceiveEmail.Text.Trim() cmbReceiveHZ.Text); //添加收信人地址 Message.To.Add(AddressTo); //信息的主题 Message.Subject = tbTitle.Text; //主题的编码方式 Message.SubjectEncoding = System.Text.Encoding.UTF8; //邮件正文 Message.Body = tbContent.Text; //邮件正文的编码 Message.BodyEncoding = System.Text.Encoding.UTF8; //清除附件信息 Message.Attachments.Clear(); //添加附件 if (tbAttachment.Text != "") { Message.Attachments.Add(new Attachment(tbAttachment.Text.Trim())); } //发送 smtpClient.SendAsync(Message, "000000"); //发送完成回调函数 smtpClient.SendCompleted = new SendCompletedEventHandler(SendBack); } } //页面载入 private void Form1_Load(object sender, EventArgs e) { //初始化下拉框 cmbHZ.SelectedIndex = 0; cmbReceiveHZ.SelectedIndex = 0; cmbSendHZ.SelectedIndex = 0; cmbSmtp.SelectedIndex = 0; } //回调函数 private void SendBack(object sender, AsyncCompletedEventArgs e) { MessageBox.Show("您的邮件已成功发送!","恭喜",MessageBoxButtons.OK,MessageBoxIcon.Information); } //选择附件 private void btn_SelectAttach_Click(object sender, EventArgs e) { //打开文件对话框 OpenFileDialog open = new OpenFileDialog(); if (open.ShowDialog() == DialogResult.OK) { //保存选择的文件名 fileName = open.SafeFileName; tbAttachment.Text = open.SafeFileName; } } //文本框变化事件 private void tbAttachment_EditValueChanged(object sender, EventArgs e) { //判断时候可以上传 if (CanAddAttachment(tbAttachment.Text)) { //判断大小是否允许 if (CanAddSize(tbAttachment.Text)) { lblState.Visible = true; lblState.Text = "添加成功!"; } else { MessageBox.Show("上传的文件过大!"); btnSend.Enabled = false; return; } } else { MessageBox.Show("不支持该格式的附件!"); btnSend.Enabled = false; return; } } //判断附件类型是否合法 private bool CanAddAttachment(string path) { //实例化文件操作对象 fs = new FileStream(path, FileMode.Open); //设置允许添加的附件类型 string[] allowExtensions = new string[] { ".jpg", ".jpeg", ".doc", ".docx", ".txt", ".excel",".rar",".zip"}; //获取文件的后缀 string name = System.IO.Path.GetExtension(fs.Name); for (int i = 0; i < allowExtensions.Length; i ) { if (name == allowExtensions[i]) { return true; } } fs.Flush(); fs.Close(); return false; } //获取文件大小 private bool CanAddSize(string path) { double size = Convert.ToDouble(fs.Length) / 1024.0 / 1024.0; //设置最大为10M if (size > 10) { return false; } fs.Flush(); fs.Close(); return true; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复