WinForm窗体代码如下:

<span style="font-size:14px;">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;
<span style="color:#ff0000;">//要添加的引用如下</span>
using System.Net.Mail;
using System.Net.Mime;
using System.Timers;
using System.IO; namespace NovelCS {
public partial class Form3 : Form {
public Form3() {
InitializeComponent();
} private void Form3_Load(object sender, EventArgs e) {
//一些常见的发送邮件服务器地址 一般都是 stmp.**.com **=qq 或 126 或163 或 gmail 有例外的,具体问度娘 string senderServerIp = "smtp.qq.com";
string toMailAddress = "1449740504@qq.com";
string fromMailAddress = "1449740504@qq.com";
string subjectInfo = "My First Email";
string bodyInfo =@"Hello Jackerson, \r\n
This is my first testing e_mail.But what all thess is from an author named Eric Sun.
Thank you,Eric Sun.";
string mailUsername = "1449740504";
string mailPassword = "******"; //发送人邮箱的密码
string mailPort = "25"; //邮箱端口号
string attachPath = "D:\\jsj.txt; D:\\jsj2.txt";//附件本地地址 MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false);
email.AddAttachments(attachPath);
email.Send();
} } public class MyEmail {
private MailMessage me;//主要处理发送邮件的内容,例:收发件人地址、标题、主体、图片
private SmtpClient sc; //主要处理用smtp发送邮件的配置信息,例:发送邮件服务器地址、发送端口号、验证方式
private int mSenderPort; //发送邮件所用的端口号(htmp协议默认为25)
private string mSenderServerHost; //发件箱的邮件服务器地址(IP形式或字符串形式均可)
private string mSenderUserName; //发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)
private string mSenderPassword; //发件箱的密码
private bool mEnableSsl; //是否对邮件内容进行socket层加密传输
private bool mEnablePwdAuthentication; //是否对发件人邮箱进行密码验证 ///<summary>
/// <strong><span style="color:#ff0000;">构造函数</span></strong>
///</summary>
///<param name="server">发件箱的邮件服务器地址</param>
///<param name="toMail">收件人地址(可以是多个收件人,程序中是以“;"进行区分的)</param>
///<param name="fromMail">发件人地址</param>
///<param name="subject">邮件标题</param>
///<param name="emailBody">邮件内容(可以以html格式进行设计)</param>
///<param name="username">发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)</param>
///<param name="password">发件人邮箱密码</param>
///<param name="port">发送邮件所用的端口号(htmp协议默认为25)</param>
///<param name="sslEnable">true表示对邮件内容进行socket层加密传输,false表示不加密</param>
///<param name="pwdCheckEnable">true表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证</param>
public MyEmail(string server, string toMailAddress, string fromMailAddress, string emailTitle, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable) {
try {
me = new MailMessage();
me.To.Add(toMailAddress);
me.From = new MailAddress(fromMailAddress);
me.Subject = emailTitle;
me.Body = emailBody;
me.IsBodyHtml = true;
me.BodyEncoding = System.Text.Encoding.UTF8;
me.Priority = MailPriority.Normal;
this.mSenderServerHost = server;
this.mSenderUserName = username;
this.mSenderPassword = password;
this.mSenderPort = Convert.ToInt32(port);
this.mEnableSsl = sslEnable;
this.mEnablePwdAuthentication = pwdCheckEnable;
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
} ///<summary>
/// <strong><span style="color:#ff0000;">添加附件</span></strong>
///</summary>
///<param name="attachmentsPath">附件的路径集合,以分号分隔</param>
public void AddAttachments(string attachmentsPath) {
try {
string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
Attachment data;
ContentDisposition disposition;
for (int i = 0; i < path.Length; i++) {
data = new Attachment(path[i], MediaTypeNames.Application.Octet);
disposition = data.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(path[i]);
disposition.ModificationDate = File.GetLastWriteTime(path[i]);
disposition.ReadDate = File.GetLastAccessTime(path[i]);
me.Attachments.Add(data);
}
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
} ///<summary>
/// <strong><span style="color:#ff0000;">邮件的发送</span></strong>
///</summary>
public void Send() {
try {
if (me != null) {
sc = new SmtpClient();
//mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
sc.Host = this.mSenderServerHost;
sc.Port = this.mSenderPort;
sc.UseDefaultCredentials = false;
sc.EnableSsl = this.mEnableSsl;
if (this.mEnablePwdAuthentication) {
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUserName, this.mSenderPassword);
//mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
//NTLM: Secure Password Authentication in Microsoft Outlook Express
sc.Credentials = nc.GetCredential(sc.Host, sc.Port, "NTLM");
} else {
sc.Credentials = new System.Net.NetworkCredential(this.mSenderUserName, this.mSenderPassword);
}
sc.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
sc.Send(me);
}
} catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
} } }</span>

备注:

(1)经过测试,确实可以使用。

(2)本文出处:Eric Sun(http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html)

版权声明:本文为博主原创文章,未经博主允许不得转载。

c# 发送邮件、附件 分类: C# 2014-12-17 16:41 201人阅读 评论(0) 收藏的更多相关文章

  1. iOS开发网络数据之AFNetworking使用 分类: ios技术 2015-04-03 16:35 105人阅读 评论(0) 收藏

    http网络库是集XML解析,Json解析,网络图片下载,plist解析,数据流请求操作,上传,下载,缓存等网络众多功能于一身的强大的类库.最新版本支持session,xctool单元测试.网络获取数 ...

  2. NPOI 通用导出数据到Excel 分类: C# Helper 2014-11-04 16:06 246人阅读 评论(0) 收藏

    应用场景: 在项目中,经常遇到将数据库数据导出到Excel,针对这种情况做了个程序封装.工作原理:利用NPOI将SQL语句查询出的DataTable数据导出到Excel,所见即所得. 程序界面:   ...

  3. ASP.NET 自定义URL重写 分类: ASP.NET 2014-10-31 16:05 175人阅读 评论(0) 收藏

    一.功能说明: 可以解决类似 http://****/news 情形,Url路径支持正则匹配. 二.操作步骤: 1.增加URL重写模块: using System; using System.IO; ...

  4. ASP.NET 自定义URL重写 分类: ASP.NET 2014-10-31 16:05 174人阅读 评论(0) 收藏

    一.功能说明: 可以解决类似 http://****/news 情形,Url路径支持正则匹配. 二.操作步骤: 1.增加URL重写模块: using System; using System.IO; ...

  5. Prime Path 分类: 搜索 POJ 2015-08-09 16:21 4人阅读 评论(0) 收藏

    Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14091 Accepted: 7959 Descripti ...

  6. Removing Columns 分类: 贪心 CF 2015-08-08 16:10 10人阅读 评论(0) 收藏

    Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. Hardwood Species 分类: POJ 树 2015-08-05 16:24 2人阅读 评论(0) 收藏

    Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 20619 Accepted: 8083 De ...

  8. JAVA 对象数组,加载图片实例 分类: Java Game 2014-08-14 16:57 80人阅读 评论(0) 收藏

    主函数: package com.mywork; import java.awt.Color; import java.awt.Image; import javax.swing.ImageIcon; ...

  9. Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. 解决打不开jar包

    Java应用程序jar文件可以由 JVM(Java虚拟机)直接执行,只要操作系统安装了JVM便可以运行作为Java应用程序的jar文件,其跨平台特性使得很多工具软件都用jar方式来部署分发,比如用于H ...

  2. Win32中GDI+应用(五)--GDI与GDI+编程模型的区别

    在GDI里面,你要想开始自己的绘图工作,必须先获取一个device context handle,然后把这个handle作为绘图复方法的一个参数,才能完成任务.同时,device context ha ...

  3. Jquery插件之信息弹出框showInfoDialog(成功、错误、警告、通知)

    一.信息种类说明: 1.1.操作成功信息 1.2.错误信息 1.3.警告信息 1.4.通知信息 二.使用说明 <!DOCTYPE html PUBLIC "-//W3C//DTD HT ...

  4. JS面向对象思想(OOP)

    直接看js好了,模拟创建一个奥运会 function 奥运会Class(主题) { // 删除主题 // delete this.主题; this.主题 = 主题; this.开幕时间; this.闭 ...

  5. Xcode6插件开发

    工欲善其事必先利其器,Xcode是我们做iOS Dev必须掌握的一款开发工具. Xcode本身也是一门Cocoa程序,与其来说它是一个Cocoa程序,是不是意味着,我们可以去动态去让它做某件事,或者监 ...

  6. NSURLSession -- 备忘

    NSURLSession NSURLSession是iOS7出的API,在它之前常用的原生网络库是NSURLConnection,但是因为Connection的使用起来不是很方便 所以我们一直倾向于A ...

  7. 使用Markdown在博客里插入代码

    今天尝试了一下在线使用Markdown编辑器写博客,发现想要实现下面这样的效果还真得折腾一会儿. <html> <head> <meta charset="ut ...

  8. Spring4.0整合Hibernate3 .6

    转载自:http://greatwqs.iteye.com/blog/1044271 6.5  Spring整合Hibernate 时至今日,可能极少有J2EE应用会直接以JDBC方式进行持久层访问. ...

  9. 前端图片预览,上传前预览,兼容IE7、8、9、10、11,Firefox,Chrome(学习到的知识)

    文章地址:http://www.cnblogs.com/rubylouvre/p/4597344.html 一.window.URL 在Chrome中,window.URL和window.webkit ...

  10. 关于@synchronized(self)的用法

    @synchronized 的作用是创建一个互斥锁,保证此时没有其它线程对self对象进行修改.这个是objective-c的一个锁定令牌,防止self对象在同一时间内被其它线程访问,起到线程的保护作 ...