C#实现发送邮件——核心部分代码

       在KS系统中有个发送邮件的功能需要做上网查阅资料以后,通过自己的部分修改实现了发送邮件的功能话不多说先来个界面:

 

邮件发送分一下步骤:

1.smtp服务信息设置

2.验证发件人信息

3.添加附件

4.正式发送邮件

5.发送邮件后处理

 

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_1(object sender, EventArgs e)
        {
            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 (MessageBox.Show("您确定要发送当前邮件吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {

                try
                {
                    //初始化Smtp服务器信息
                    setSmtpClient("smtp." + txt_SmtpServer.Text.Trim() + comboBoxEx3.Text, Convert.ToInt32(numericUpDown1.Text));
                }
                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 = new MailMessage(); 
                MailMessage_Mai.To.Clear();
                //添加收件人邮箱地址
              
                MailAddress_to = new MailAddress(textBox4.Text.Trim() + comboBox4.Text.Trim());
                MailMessage_Mai.To.Add(MailAddress_to);

                //发件人邮箱
                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();

                //注册邮件发送完毕后的处理事件
                SmtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                //开始发送邮件
                SmtpClient.SendAsync(MailMessage_Mai, "000000000");

            }

        }


 

5.发送邮件后处理

 #region 发送邮件后所处理的函数
        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            try
            {
                if (e.Cancelled)
                {
                    MessageBox.Show("发送已取消!");
                }
                if (e.Error != null)
                {

                    MessageBox.Show("邮件发送失败!" + "\n" + "技术信息:\n" + e.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
                else
                {
                    MessageBox.Show("邮件成功发出!", "恭喜!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show("邮件发送失败!" + "\n" + "技术信息:\n" + Ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        #endregion


 

参考资料连接:http://www.cnblogs.com/maiweibiao/articles/1837821.html

 代码:http://download.csdn.net/detail/gwblue/6353807
posted @ 2013-09-30 17:30  夏至冬末  阅读(296)  评论(0编辑  收藏  举报