• C#实现邮件群发


     

    C#实现邮件群发常常应用于网络营销,在网络营销方面卖的很火,大家完全可以做一个专业的群发软件去赚钱。

    尊重作者劳动成果,转发请注明出处:http://www.cnblogs.com/minotmin/

    附上邮件发送的基本代码:本例子中使用的QQSMTP

    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;
            }
        }
        }
       

  • 相关阅读:
    HTML5程序设计--SVG
    visual studio 2012 Github
    排序算法--鸡尾酒排序
    排序算法--归并排序
    排序算法--冒泡排序
    排序算法---插入排序
    外语学习的真实方法及误区
    学习新东西的唯一方法
    如何做好一个面试官——之学习篇
    求职者和面试官如何做好电话面试
  • 原文地址:https://www.cnblogs.com/minotmin/p/2700536.html
Copyright © 2020-2023  润新知