日期:2014-05-18  浏览次数:20863 次

如何实现发带有附件的邮件
请大家帮忙,给段代码,实现发带有附件的邮件,谢谢

------解决方案--------------------
http://wenku.baidu.com/view/87be056f1eb91a37f1115cfa.html

这里有DEMO,你可以看一下,
------解决方案--------------------
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.but