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