C# winform發送郵件 帶附件(經測試成功通過)

發布時間:2009年10月29日      浏覽次數:2063 次
Form1.cs 文件代碼
-----------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
namespace MySoft
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//發送郵件
private void button1_Click(object sender, EventArgs e)
{
string fjrtxt = fjr.Text; //發件者郵箱地址
string mmtxt = mm.Text; //發件者郵箱密碼
string sjrtxt = sjr.Text;//收件人收箱地址
string zttxt = zt.Text;//主題
string fjtxt = fj.Text;//附件
string nrtxt = nr.Text;//内容
string[] fasong = fjrtxt.Split('@');
string[] fs = fasong[1].Split('.');
//發送
SmtpClient client = new SmtpClient("smtp." + fs[0].ToString().Trim() + ".com"); //設置郵件協議
client.UseDefaultCredentials = false;//這一(yī)句得寫前面
client.DeliveryMethod = SmtpDeliveryMethod.Network; //通過網絡發送到Smtp服務器
client.Credentials = new NetworkCredential(fasong[0].ToString(), mmtxt); //通過用戶名和密碼 認證
MailMessage mmsg = new MailMessage(new MailAddress(fjrtxt), new MailAddress(sjrtxt)); //發件人和收件人的郵箱地址
mmsg.Subject = zttxt; //郵件主題
mmsg.SubjectEncoding = Encoding.UTF8; //主題編碼
mmsg.Body = nrtxt; //郵件正文
mmsg.BodyEncoding = Encoding.UTF8; //正文編碼
mmsg.IsBodyHtml = true; //設置爲HTML格式
mmsg.Priority = MailPriority.High; //優先級
if (fj.Text.Trim() != ""){
mmsg.Attachments.Add(new Attachment(fj.Text));//增加附件
}
try
{
client.Send(mmsg);
MessageBox.Show("郵件已發成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//添加附件
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fj.Text = openFileDialog1.FileName; //得到附件的地址
}
}
//關閉窗體(tǐ)
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
-------------------------------------------
Form1.Designer.cs 窗體(tǐ)設計器代碼
-------------------------------------------
namespace MySoft
{
partial class Form1
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資(zī)源。
/// </summary>
/// <param name="disposing">如果應釋放(fàng)托管資(zī)源,爲 true;否則爲 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體(tǐ)設計器生(shēng)成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.fjr = new System.Windows.Forms.TextBox();
this.mm = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.sjr = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.zt = new System.Windows.Forms.TextBox();
this.fj = new System.Windows.Forms.TextBox();
this.nr = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 19);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 0;
this.label1.Text = "發件人:";
//
// fjr
//
this.fjr.Location = new System.Drawing.Point(71, 16);
this.fjr.Name = "fjr";
this.fjr.Size = new System.Drawing.Size(162, 21);
this.fjr.TabIndex = 1;
//
// mm
//
this.mm.Location = new System.Drawing.Point(71, 55);
this.mm.Name = "mm";
this.mm.PasswordChar = '*';
this.mm.Size = new System.Drawing.Size(162, 21);
this.mm.TabIndex = 1;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 58);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 12);
this.label2.TabIndex = 0;
this.label2.Text = "密 碼:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 95);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(53, 12);
this.label3.TabIndex = 0;
this.label3.Text = "收件人:";
//
// sjr
//
this.sjr.Location = new System.Drawing.Point(71, 92);
this.sjr.Name = "sjr";
this.sjr.Size = new System.Drawing.Size(389, 21);
this.sjr.TabIndex = 1;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(12, 131);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(53, 12);
this.label4.TabIndex = 0;
this.label4.Text = "主 題:";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(12, 169);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(53, 12);
this.label5.TabIndex = 0;
this.label5.Text = "附 件:";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(12, 204);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(53, 12);
this.label6.TabIndex = 0;
this.label6.Text = "内 容:";
//
// zt
//
this.zt.Location = new System.Drawing.Point(71, 128);
this.zt.Name = "zt";
this.zt.Size = new System.Drawing.Size(389, 21);
this.zt.TabIndex = 1;
//
// fj
//
this.fj.Location = new System.Drawing.Point(71, 166);
this.fj.Name = "fj";
this.fj.Size = new System.Drawing.Size(303, 21);
this.fj.TabIndex = 1;
//
// nr
//
this.nr.Location = new System.Drawing.Point(71, 201);
this.nr.Multiline = true;
this.nr.Name = "nr";
this.nr.Size = new System.Drawing.Size(389, 276);
this.nr.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(94, 504);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 2;
this.button1.Text = "發 送";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(313, 504);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "退 出";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(380, 164);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "添加附件";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(494, 539);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.nr);
this.Controls.Add(this.fj);
this.Controls.Add(this.zt);
this.Controls.Add(this.sjr);
this.Controls.Add(this.mm);
this.Controls.Add(this.fjr);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox fjr;
private System.Windows.Forms.TextBox mm;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox sjr;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox zt;
private System.Windows.Forms.TextBox fj;
private System.Windows.Forms.TextBox nr;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
}
}
文章來源:http://hi.baidu.com/%C4%E3%D5%E6%B5%C3%BA%DC%BA%C3/blog/item/96dc0477d30d251cb051b91e.html
免責聲明:本站相關技術文章信息部分(fēn)來自網絡,目的主要是傳播更多信息,如果您認爲本站的某些信息侵犯了您的版權,請與我(wǒ)(wǒ)們聯系,我(wǒ)(wǒ)們會即時妥善的處理,謝謝合作!