C#邮件发送的实现方法

 

本段代码可以实现邮件的群发,

主要是利用了SMTP的知识,测试没有错误,转发请注明出处:http://www.cnblogs.com/minotmin/

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;

namespace 邮件发送
{
    public partial class btnSend : Form
    {
        public btnSend()
        {
            InitializeComponent();
        }
      
        private void btnSendMail_Click(object sender, EventArgs e)
        {
         
            try
            {
            MailAddress Messagefrom = new MailAddress(this.txtSend.Text,"C#仰望着");  //发件人邮箱地址
            string MessageTo = this.txtTo.Text;  //收件人邮箱地址
            string MessageSubject = this.txtSubject.Text;        //邮件主题
            string MessageBody = this.txtBody.Text;    //邮件内容
            Send(MessageTo, MessageSubject, MessageBody,Messagefrom); 
            }
           catch (Exception ex)
            {
              MessageBox.Show(ex.Message);
            }
        }

        private void btnSend_Load(object sender, EventArgs e)
        {
            this.txtCounter.Text ="3";
            this.txtSend.Text = "邮箱";
            this.txtSenderPwd.Text = "邮箱密码";
            this.cbHost.SelectedIndex = 0;//combobox选择当前列表中的第一个值
        
          
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Control control in this.Controls)
                {
                    if (control is TextBox)
                    {
                        TextBox txt = control as TextBox;
                        if (txt != null)
                        {
                            txt.Text = "";
                        }
                        this.txtSend.Text = "邮箱";
                        this.txtSenderPwd.Text = "邮箱密码";
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cbHost_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbHost.Text = cbHost.SelectedItem.ToString();//combobox获得当前选择的值。
        }

        public void Send(string MessageTo, string MessageSubject, string MessageBody, MailAddress Messagefrom)
        {

            try
            {
                txtTo.Text = txtTo.Text.Replace(" ", "");//去除空格
                txtTo.Text = txtTo.Text.Trim();
                txtTo.Text = txtTo.Text.Replace((char)13, (char)0);
                MailMessage email = new MailMessage();
                email.From = Messagefrom;
                email.To.Add(MessageTo);//收件人邮箱地址可以是多个以实现群发
                email.Subject = MessageSubject;
                email.Body = MessageBody;
                email.IsBodyHtml = false; //是否为html格式 
                email.Priority = MailPriority.Normal;  //发送邮件的优先等级
                SmtpClient sc = new SmtpClient();
                sc.Host = cbHost.Text;              //指定发送邮件的服务器地址或IP
                sc.Port = 25;//指定发送邮件端口
                sc.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何发送电子邮件
                sc.UseDefaultCredentials = false;//是否随请求一起发送
                sc.EnableSsl = false;//安全连接设置
                sc.Credentials = new System.Net.NetworkCredential(this.txtSend.Text, this.txtSenderPwd.Text); //指定登录服务器的用户名和密码
                sc.Send(email);
                MessageBox.Show("邮件发送成功!","系统提示");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
      
        }

    
        #region 打开文件并且显示在richtextbox控件中
        string fileName;
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "文本文件(*.txt)|*.txt";
            op.AddExtension = true;
            op.DefaultExt = "txt";
            op.CheckFileExists = true;
            op.CheckPathExists = true;
            if (op.ShowDialog() == DialogResult.OK)
            {
                fileName = op.FileName;
                try
                {
                    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    if (fs.CanRead)
                    {
                        //读取时加入编码信息,否则读取汉字会乱码
                        StreamReader sr = new StreamReader(fs, Encoding.Default);
                           string    strline = sr.ReadLine();
                        StringBuilder sb = new StringBuilder();
                        int counter = 0;
                        while (strline != null&&counter<Convert.ToInt32(this.txtCounter.Text.ToString()))
                        {
                            strline = sr.ReadLine()+",";
                            sb = sb.Append(strline);
                            ++counter;
                        }
                        txtTo.Text = sb.ToString().Remove(sb.ToString().LastIndexOf(","), 1);
                        sr.Close();
                        //s = s.Replace(" ", "");
                      
                     
                     
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                 
                }


            }

        }
        #endregion
        #region 关闭系统的代码
        private void btnSend_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确实要退出本系统吗?", "系统提示", MessageBoxButtons.OKCancel)==DialogResult.OK)   
            {
                this.Dispose();
                Application.Exit();
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;//阻止退出系统
            }
        }
        #endregion
        #region 系统最小化的设置
        private void btnSend_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }
        #endregion

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.notifyIcon1.Visible =true;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Application.Exit();
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.notifyIcon1.Visible = true;
            this.Visible = true;
        }

        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = false;
        }
    }
    }
   

posted on 2012-09-24 19:23  程序猴chengxuhou.com  阅读(861)  评论(0编辑  收藏  举报

导航