C# winform发送邮件 带附件(经测试成功通过)

发布时间:2009年10月29日      浏览次数:2078 次
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;//这一句得写前面
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; //得到附件的地址
}
}
//关闭窗体
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
-------------------------------------------
Form1.Designer.cs 窗体设计器代码
-------------------------------------------
namespace MySoft
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <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
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!