窗体设计:


C#实现简单邮件发送_简单邮件发送

窗体代码:

using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.IO;
using System.Net;
using System.Net.Mime;
namespace SendMail
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }
        //保存附件名
        public static string filename;
        //窗体加载
        private void MainFrm_Load(object sender, EventArgs e)
        {
            youbox.Text = "QQ.com";
            hebox.Text = "QQ.com";
        }
        //添加附件
        private void addfilebtn_Click(object sender, EventArgs e)
        {
            checkfile.ShowDialog();
            filename = checkfile.FileName;
            if (filename != "")
            {
                filelab.Visible = true;
                filelab.Text = "当前附件(双击删除):" + filename;
            }
        }
        //删除附件
        private void filelab_DoubleClick(object sender, EventArgs e)
        {
            filename = "";
            filelab.Text = "当前附件:" + filename;
        }
        //发送邮件
        private void sendbtn_Click(object sender, EventArgs e)
        {
            try
            {
                //实例发件人smtp客户端
                SmtpClient client = new SmtpClient("smtp." + youbox.Text);
                //生成发送地址
                string strFrom = string.Empty;
                if (youmailtxt.Text != "")
                {
                    strFrom = youmailtxt.Text + "@" + youbox.Text;
                }
                else
                {
                    MessageBox.Show("请输入你的邮箱地址!", "错误");
                    youmailtxt.Focus();
                    return;
                }
                //生成收件人地址
                string strHe = string.Empty;
                if (hemailtxt.Text != "")
                {
                    strHe = hemailtxt.Text + "@" + hebox.Text;
                }
                else
                {
                    MessageBox.Show("请输入收件人的邮箱地址!", "错误");
                    hemailtxt.Focus();
                    return;
                }
                //构造发收件人对象
                MailAddress you = new MailAddress(strFrom, strFrom, Encoding.UTF8);
                MailAddress he = new MailAddress(strHe, strHe, Encoding.UTF8);
                //构造msb对象
                MailMessage msg = new MailMessage(you, he);
                //添加附件
                if (File.Exists(filename))
                {
                    //构造附件对象
                    Attachment mailfile = new Attachment(filename);
                    //得到文件信息
                    ContentDisposition disposition = mailfile.ContentDisposition;
                    // disposition.CreationDate = File.GetCreationTime(filename);
                    // disposition.ModificationDate = File.GetLastWriteTime(filename);
                    // disposition.ReadDate = File.GetLastAccessTime(filename);
                    //添加附件到msg
                    msg.Attachments.Add(mailfile);
                }
                //添加主题和内容
                msg.Subject = titletxt.Text;
                msg.SubjectEncoding = Encoding.UTF8;
                msg.Body = contentbox.Text;
                msg.BodyEncoding = Encoding.UTF8;
                //设置邮件发送方式
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                msg.IsBodyHtml = false;
                //给gmail开启ssl
                if (youbox.Text == "gmail.com")
                {
                    client.EnableSsl = true;
                }
                else
                {
                    client.EnableSsl = false;
                }
                //设置用户名和密码
                client.UseDefaultCredentials = false;
                string username = youmailtxt.Text;
                string passwd = passtxt.Text;
                //用户登录信息
                NetworkCredential mycre = new NetworkCredential(username, passwd);
                client.Credentials = mycre;
                //发送邮件
                this.Hide();
                client.Send(msg);
                this.Visible = true;
                MessageBox.Show("邮件成功发送到\"" + strHe + "\"!", "发送成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
              
        }
    }
}