一个发送邮件的小例子
public partial class SendMail : Form
{
string text = string.Empty;
public SendMail()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string fromAddress=Txt_From.Text.Trim();
string username = fromAddress.Substring(0,fromAddress.IndexOf("@"));
string mailServer = "smtp." + fromAddress.Substring(fromAddress.IndexOf("@") + 1);
string fromcheck = CheckMailAddress(Txt_From.Text.Trim());
if(!string.IsNullOrEmpty(fromcheck))
{
Txt_From.Focus();
MessageBox.Show("邮箱"+fromcheck+"存在问题!");
return ;
}
string toAddress=Txt_To.Text.Trim();
string toCheck=CheckMailAddress(toAddress);
if(!string.IsNullOrEmpty(toCheck))
{
Txt_To.Focus();
MessageBox.Show("邮箱"+fromcheck+"存在问题!");
return;
}
//lijinchang3@163.com;lijc@finchina.com
//webBrowser1.DocumentText = "测试邮件发送!";
MailAddress from = new MailAddress(fromAddress);
MailAddress to = new MailAddress(toAddress);
MailMessage mailobj = new MailMessage(from, to);
mailobj.Subject = Txt_Subject.Text.Trim();
mailobj.Body = webBrowser1.DocumentText;//发送的信息
mailobj.IsBodyHtml = true;
mailobj.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
//邮件优先级
mailobj.Priority = MailPriority.High;
string []cc = Txt_CC.Text.Trim().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string c in cc)
{
mailobj.CC.Add(c);
}
//Initializes a new instance of the System.Net.Mail.SmtpClient class
//that sends e-mail by using the specified SMTP server.
SmtpClient smtp = new SmtpClient(mailServer);
//或者用:
//SmtpClient smtp = new SmtpClient();
//smtp.Host = mailServer;
//不使用默认凭据访问服务器
smtp.UseDefaultCredentials = false;
string userPswd = "2323132";
smtp.Credentials = new NetworkCredential(username, userPswd);
//使用network发送到smtp服务器
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
//开始发送邮件
smtp.Send(mailobj);
webBrowser1.DocumentText = "测试邮件已发送!";
Thread.Sleep(100);
webBrowser1.DocumentText = text;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
private string CheckMailAddress(string adress)
{
string [] adds=adress.Split(new string[]{";"},StringSplitOptions.RemoveEmptyEntries);
foreach (string ad in adds)
{
if (!new Regex(@"(?:[a-z\d]+[_\-\+\.]?)*[a-z\d]+@(?:([a-z\d]+\-?)*[a-z\d]+\.)+([a-z]{2,})+", RegexOptions.IgnoreCase).IsMatch(ad))
{
return ad;
}
}
return string.Empty;
}
private void SendMail_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog opendialog = new OpenFileDialog();
opendialog.Filter = "html(*.htm;*.html)|*.html;*.htm";
opendialog.Title = "选择发送文件";
if (DialogResult.OK == opendialog.ShowDialog())
{
text = File.ReadAllText(opendialog.FileName, Encoding.Default);
webBrowser1.DocumentText = text;
}
}
}
定义界面
partial class SendMail
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Txt_From = new System.Windows.Forms.TextBox();
this.Txt_To = new System.Windows.Forms.TextBox();
this.lb1 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.Txt_Subject = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.button1 = new System.Windows.Forms.Button();
this.Txt_CC = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Txt_From
//
this.Txt_From.Location = new System.Drawing.Point(118, 24);
this.Txt_From.Name = "Txt_From";
this.Txt_From.Size = new System.Drawing.Size(305, 21);
this.Txt_From.TabIndex = 0;
//
// Txt_To
//
this.Txt_To.Location = new System.Drawing.Point(118, 52);
this.Txt_To.Name = "Txt_To";
this.Txt_To.Size = new System.Drawing.Size(305, 21);
this.Txt_To.TabIndex = 1;
//
// lb1
//
this.lb1.AutoSize = true;
this.lb1.Location = new System.Drawing.Point(60, 27);
this.lb1.Name = "lb1";
this.lb1.Size = new System.Drawing.Size(41, 12);
this.lb1.TabIndex = 2;
this.lb1.Text = "发送人";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(60, 55);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 3;
this.label1.Text = "接受人";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(62, 109);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 4;
this.label2.Text = "标题";
//
// Txt_Subject
//
this.Txt_Subject.Location = new System.Drawing.Point(118, 109);
this.Txt_Subject.Name = "Txt_Subject";
this.Txt_Subject.Size = new System.Drawing.Size(491, 21);
this.Txt_Subject.TabIndex = 5;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(50, 134);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(29, 12);
this.label3.TabIndex = 6;
this.label3.Text = "正文";
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(52, 149);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(722, 431);
this.webBrowser1.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(146, 605);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 8;
this.button1.Text = "发送";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Txt_CC
//
this.Txt_CC.Location = new System.Drawing.Point(118, 80);
this.Txt_CC.Name = "Txt_CC";
this.Txt_CC.Size = new System.Drawing.Size(491, 21);
this.Txt_CC.TabIndex = 9;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(62, 83);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(29, 12);
this.label4.TabIndex = 10;
this.label4.Text = "抄送";
//
// button2
//
this.button2.Location = new System.Drawing.Point(671, 106);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(103, 23);
this.button2.TabIndex = 11;
this.button2.Text = "导入发送信息";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// SendMail
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(818, 643);
this.Controls.Add(this.button2);
this.Controls.Add(this.label4);
this.Controls.Add(this.Txt_CC);
this.Controls.Add(this.button1);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.label3);
this.Controls.Add(this.Txt_Subject);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.lb1);
this.Controls.Add(this.Txt_To);
this.Controls.Add(this.Txt_From);
this.Name = "SendMail";
this.Text = "SendMail";
this.Load += new System.EventHandler(this.SendMail_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox Txt_From;
private System.Windows.Forms.TextBox Txt_To;
private System.Windows.Forms.Label lb1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox Txt_Subject;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.WebBrowser webBrowser1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox Txt_CC;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button2;
}
posted on 2011-08-25 15:12 lijinchang 阅读(232) 评论(0) 编辑 收藏 举报