C#、ASP.NET、WinForm - 实现邮件发送的功能
转载自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html
- 发送邮件所用的核心知识点
- 微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
- 微软封装好的SmtpClient类:主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
- SmtpClient主要进行了三层的封装:Socket --> TcpClient --> SmtpClient
- 具体代码请看如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Timers;
using System.Xml; namespace MyEmailTest
{
class Program
{
static void Main(string[] args)
{
try
{
//smtp.163.com
string senderServerIp = "123.125.50.133";
//smtp.gmail.com
//string senderServerIp = "74.125.127.109";
//smtp.qq.com
//string senderServerIp = "58.251.149.147";
//string senderServerIp = "smtp.sina.com";
string toMailAddress = "mingmingruyuedlut@163.com";
string fromMailAddress = "mingmingruyuedlut@163.com";
string subjectInfo = "Test sending e_mail";
string bodyInfo = "Hello Eric, This is my first testing e_mail";
string mailUsername = "mingmingruyuedlut";
string mailPassword = ".........."; //发送邮箱的密码()
string mailPort = "";
string attachPath = "E:\\123123.txt; E:\\haha.pdf"; MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false);
email.AddAttachments(attachPath);
email.Send();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
} public class MyEmail
{
private MailMessage mMailMessage; //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等)
private SmtpClient mSmtpClient; //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等)
private int mSenderPort; //发送邮件所用的端口号(htmp协议默认为25)
private string mSenderServerHost; //发件箱的邮件服务器地址(IP形式或字符串形式均可)
private string mSenderPassword; //发件箱的密码
private string mSenderUsername; //发件箱的用户名(即@符号前面的字符串,例如:hello@163.com,用户名为:hello)
private bool mEnableSsl; //是否对邮件内容进行socket层加密传输
private bool mEnablePwdAuthentication; //是否对发件人邮箱进行密码验证 ///<summary>
/// 构造函数
///</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 toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable)
{
try
{
mMailMessage = new MailMessage();
mMailMessage.To.Add(toMail);
mMailMessage.From = new MailAddress(fromMail);
mMailMessage.Subject = subject;
mMailMessage.Body = emailBody;
mMailMessage.IsBodyHtml = true;
mMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
mMailMessage.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)
{
Console.WriteLine(ex.ToString());
}
} ///<summary>
/// 添加附件
///</summary>
///<param name="attachmentsPath">附件的路径集合,以分号分隔</param>
public void AddAttachments(string attachmentsPath)
{
try
{
string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
Attachment data;
ContentDisposition disposition;
for (int i = ; 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]);
mMailMessage.Attachments.Add(data);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} ///<summary>
/// 邮件的发送
///</summary>
public void Send()
{
try
{
if (mMailMessage != null)
{
mSmtpClient = new SmtpClient();
//mSmtpClient.Host = "smtp." + mMailMessage.From.Host;
mSmtpClient.Host = this.mSenderServerHost;
mSmtpClient.Port = this.mSenderPort;
mSmtpClient.UseDefaultCredentials = false;
mSmtpClient.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
mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM");
}
else
{
mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword);
}
mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
mSmtpClient.Send(mMailMessage);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
要开启邮箱的SmTP服务。
C#、ASP.NET、WinForm - 实现邮件发送的功能的更多相关文章
- C# Email邮件发送,功能是密码找回或者重置功能。
原文:C# Email邮件发送,功能是密码找回或者重置功能. 最近根据公司需求,写个邮件发送. 这里面的传入的地址信息的参数都是经过加密的. 主要是保证用户信息的安全. 帮助类 using ...
- C# 或 Asp.net 2.0 邮件发送模块(亲测)
using System.Net.Mail;using System.Net; public class Mail { MailMessage mm; SmtpCli ...
- C#实现邮件发送的功能
Ø 发送邮件所用的核心知识点 微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址.标题.主体.图片等等) 微软封装好的SmtpClient类:主要处理用smtp方式发送此邮 ...
- thinkphp 邮件发送
最近项目上要求,要做个邮件发送的功能,因为用到的框架是ThinkPHP,于是就自己整理一下. 引入class.phpmailer.php,大家可以去这个链接去下载: http://pan.baidu. ...
- C# QQ & 163 邮件发送
这篇文章的目的并不是说明如果进行右键的发送,因为在.net 坝坝的怀抱下邮件发送的功能实现并不会很难,当然邮件发送的代码,还是会贴上的,昨天在写一个邮件发送的功能,我直接找到了原来的代码,想着直接就可 ...
- SSH网上商城---邮件发送
注册网站账号的时候,都需要发送激活邮件,然后让注册的用户点击激活链接方可完成注册,不过话说回来,为什么注册的时候需要发送邮件呢?为什么不注册的时候直接激活呢?一定要收一封激活帐号的邮件?网站这样做的好 ...
- SpringBoot项目实现文件上传和邮件发送
前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...
- 玩转 SpringBoot2.x 之整合邮件发送
序 在实际项目中,经常需要用到邮件通知功能.比如,用户通过邮件注册,通过邮件找回密码等:又比如通过邮件发送系统情况,通过邮件发送报表信息等等,实际应用场景很多. 原文地址:https://www.mm ...
- 用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇
在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还 ...
随机推荐
- Physiological Processes of Speech Production--Reading Notes (8)
Upper Jaw The upper jaw, or the maxilla with the upper teeth, is the structure fixed to the skull, f ...
- Android FragmentPagerAdapter和FragmentStatePagerAdapter的区别
FragmentPagerAdapter官方解释: This version of the pager is best for use when there are a handful of typi ...
- JQ 一些基本方法
1.判断复选框是否有选中,bischecked 返回 ture 或 false var bischecked = $('[name=uid]').is(':checked'); 2.查看当前元素是父元 ...
- js 实现图片间隔循环轮播以及没有间隔的循环轮播
链接地址:http://blog.sina.com.cn/s/blog_75cf5f32010199dn.html 最近做了个图片循环轮播的功能.就是几张图片不断的循环滚动显示. 感觉这个方法不错所以 ...
- Ubuntu14.04 Y460闪屏问题解决方案
我的笔记本是联想Y460,安装了Ubuntu之后发现屏幕闪烁移位,而且在使用IDE的时候出现无法输入中文等问题,其实是显卡驱动的问题,N卡官网给的驱动不好用,尝试使用大黄蜂 参考:https://wi ...
- Codeforces 479D - Long Jumps
479D - Long Jumps, 480B - Long Jumps It , or . If we can already measure both x and y, output . Then ...
- 2014 HDU多校弟五场A题 【归并排序求逆序对】
这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的 ...
- python下载文件(图片)源码,包含爬网内容(爬url),可保存cookie
#coding=utf-8 ''' Created on 2013-7-17 @author: zinan.zhang ''' import re import time import httplib ...
- Sqrt(x) 牛顿迭代法
为了实现sqrt(x),可以将问题看成是求解\(x^2-y=0\) ,即sqrt(y)=x: 牛顿法是求解方程的近似方法,给定初始点\((x0,f(x0))\),迭代公式为: #include < ...
- 用tomcat搭建web服务器
链接地址:http://www.blogjava.net/qingshow/archive/2010/01/17/309846.html qingshow “不积跬步无以至千里,不积小流无以成江海”. ...