我的见解:

  模块化与组件化是编程的一种思想:提高代码的重用性,提高开发效率。

  常见的模块化就是函数与各种类型的封装,若是代码具有更高的重用价值(能够提供给别人使用),建议可以考虑封装成动态链接库(dll),直接引用使用。

  常见的组件化就是将各种需求功能封装成一系列完整的文档(比模块化要求更高、更完整),要用的时候直接引用对应的文件就可以使用。

  说了那么多,我们就直奔今天的主题:在用ASP.NET 技术开发Web网站的时候,如何实现发送邮件的功能?

  下面的是一个专门用来发送邮件的类(封装好了,可以直接编译成动态链接库,方便以后重复使用)(默认是以Html格式发送的邮件)

using System;
using System.IO;
using System.Net.Mail;
using System.Net.Mime; namespace DllLibrary.SendEmail
{
/// <summary>
/// 发送邮件的类
/// </summary>
public class Email
{
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 Email(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)
{
throw 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)
{
throw ex;
//Console.WriteLine(ex.ToString());
}
} /// <summary>
/// 邮件的发送
/// </summary>
/// <returns>发送成功返回true,否则返回false</returns>
public bool 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
{
return false;
}
return true;
}
}
}

  使用这个发送邮箱类的实例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Test.Models;
using DllLibrary.SendEmail;//引入对应命名空间 namespace Test.Controllers
{
public class HomeController : Controller
{ /// <summary>
/// 发送邮件
/// </summary>
/// <param name="title">邮件主题</param>
/// <param name="email">要发送对象的邮箱</param>
/// <param name="content">邮件内容</param>
/// <returns></returns>
public ActionResult SendMail(string title, string email, string content)
{
string senderServerIp = "smtp.163.com"; //使用163代理邮箱服务器(也可是使用qq的代理邮箱服务器,但需要与具体邮箱对相应)
string toMailAddress = email; //要发送对象的邮箱
string fromMailAddress = "XXXXXX@163.com";//你的邮箱
string subjectInfo = title; //主题
string bodyInfo = "<p>" + content + "</p>";//以Html格式发送的邮件
string mailUsername = "XXX"; //登录邮箱的用户名
string mailPassword = "xomeagyungxzhsxf"; //对应的登录邮箱的第三方密码(你的邮箱不论是163还是qq邮箱,都需要自行开通stmp服务)
string mailPort = ""; //发送邮箱的端口号
//string attachPath = "E:\\123123.txt; E:\\haha.pdf"; //创建发送邮箱的对象
Email myEmail = new Email(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false); //添加附件
//email.AddAttachments(attachPath); if (myEmail.Send())
{
return Content("<script>alert('邮件已成功发送!')</script>");
}
else
{
return Content("<script>alert('邮件发送失败!')</script>");
} } }
}

  简单说明:要发送邮件,需要开通一个stmp服务的邮箱号。(可以到163官网申请一个163邮箱,并且开通stmp服务就可以了(注:示例代码中所使用的密码是在开通stmp服务时,单独发送的第三方登录密码,而不是登录邮箱的密码))

  好了,这次分享就到这里了,下次继续。。。

  <我的博客主页>:http://www.cnblogs.com/forcheng/

ASP.NET MVC 模块与组件(一)——发送邮件的更多相关文章

  1. ASP.NET MVC 模块与组件(二)——定制图片验证码

     本着简洁直接,我们就直奔主题吧! 下面是一个生成数字和字母随机组合的验证码类源代码: using System; using System.Drawing; using System.Drawing ...

  2. asp.net mvc 模型验证组件——FluentValidation

    asp.net mvc 模型验证组件——FluentValidation 示例 using FluentValidation; public class CustomerValidator: Abst ...

  3. asp.net MVC通用分页组件 使用方便 通用性强

    asp.net MVC通用分页组件 使用方便 通用性强   该分页控件的显示逻辑: 1 当前页面反色突出显示,链接不可点击 2 第一页时首页链接不可点击 3 最后一页时尾页链接不可点击 4 当前页面左 ...

  4. 理解ASP.NET MVC的DependencyResolver组件

    一.前言 DependencyResolver是MVC中一个重要的组件,从名字可以看出,它负责依赖对象的解析,可以说它是MVC框架内部使用的一个IOC容器.MVC内部很多对象的创建都是通过它完成的,或 ...

  5. 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC

    系列文章 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递 七天学会ASP.NET MVC (三)— ...

  6. 通过一个模拟程序让你明白ASP.NET MVC是如何运行的

    ASP.NET MVC的路由系统通过对HTTP请求的解析得到表示Controller.Action和其他相关的数据,并以此为依据激活Controller对象,调用相应的Action方法,并将方法返回的 ...

  7. 7 天玩转 ASP.NET MVC — 第 1 天

    0. 前言正如标题「7 天玩儿转 ASP.NET MVC」所言,这是个系列文章,所以将会向大家陆续推出 7 篇.设想一下,一天一篇,你将从一个愉快的周一开始阅读,然后在周末成为一个 ASP.NET M ...

  8. 7 天玩转 ASP.NET MVC - 第 1 天

    0. 前言 正如标题「7 天玩儿转 ASP.NET MVC」所言,这是个系列文章,所以将会向大家陆续推出 7 篇.设想一下,一天一篇,你将从一个愉快的周一开始阅读,然后在周末成为一个 ASP.NET ...

  9. ASP.NET MVC总结

    一.概述 1.单元测试的NUnit, MBUnit, MSTest, XUnit以及其他的框架 2.ASP.NET MVC 应用的默认目录结构有三个顶层目录: Controllers.Models.V ...

随机推荐

  1. SQL Azure (15) SQL Azure 新的规格

    <Windows Azure Platform 系列文章目录> 在以前的文章中,笔者给大家介绍了Microsoft Azure SQL Database (以前被称为SQL Azure)的 ...

  2. 注册OCX失败

    今天注册某个OCX时,Windows报告以下错误: 模块“XXX.ocx”已加载,但对 DllRegisterServer 的调用失败,错误代码为 0x80040200. 这是Windows权限引起的 ...

  3. C语言 · 特殊回文数

    问题描述 123321是一个非常特殊的数,它从左边读和从右边读是一样的. 输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n . 输入格式 输入一行,包含一个正整数n. 输 ...

  4. 修改windows自带的Ctrl+Space输入法切换快捷键

    使用场景: 多为我等码农使用一些编辑器时,编辑器的默认代码提示热键为 ctrl+space ,但这个热键被系统的输入法开关占用.如果遇到可以设置快捷键的编辑器还好,要是不能设置的话(比如火狐浏览器的代 ...

  5. volatile用法

    1.volatile 主要是 其 "可见性",在java内存模型中,变量都是放在主内存中,每条线程里面有自己的工作内存,当一个变量被volatile 修饰时候,其他的线程会得到该变 ...

  6. 使用JAVA编写电话薄程序,具备添加,查找,删除等功能

    //该程序需要连接数据库.根据word文档要求所有功能均已实现.//大部分方法基本差不多,//在查询修改的时候能输出 最大ID号 和最小ID号,并且可以对输入的ID号进行判断是否存在(具体方法请查看 ...

  7. 数据库的Disk Space usage

    SQL Server占用的存储空间,包含数据库file占用的存储空间,数据库对象占用的存储空间. 一,数据库file占用的存储空间 1,使用 sys.master_files 查看数据库中各个file ...

  8. Task C# 多线程和异步模型 TPL模型

    Task,异步,多线程简单总结 1,如何把一个异步封装为Task异步 Task.Factory.FromAsync 对老的一些异步模型封装为Task TaskCompletionSource 更通用, ...

  9. Android获取可存储文件所有路径

    引言:大家在做app开发的时候,基本都会保存文件到手机,android存储文件的地方有很多,不像ios一样,只能把文件存储到当前app目录下,并且android手机由于厂家定制了rom,sdcard的 ...

  10. 使用Python和Perl绘制北京跑步地图

    当你在一个城市,穿越大街小巷,跑步跑了几千公里之后,一个显而易见的想法是,如果能把在这个城市的所有路线全部画出来,会是怎样的景象呢? 文章代码比较多,为了不吊人胃口,先看看最终效果,上到北七家,下到南 ...