一直想把发送邮件的功能掌握,总是各种情况拖着了,这两天终于看了一下,整理一下,希望能帮到想学的。

  发送邮件使用SMTP服务器,有两种方案,一种是使用IIS的SMTP功能;另一种是直接使用邮件供应商的SMTP,比如Gmail、Sina、QQ等,使用这些SMTP服务器必须得注册帐号,一般可以直接用邮箱及密码,但是有些邮箱必须开启POP3/SMTP服务才可以,比如QQ邮箱默认是关闭的,可以在“设置”->“账户”里面找到。我今天整理的都是用的第二种。

  早期的.NET版本用的是 System.Web.Mail 类提供的功能来发邮件;2.0版本推出了 System.Net.Mail 类来代替 System.Web.Mail ,但是我在 WebForm 里面使用的时候用System.Net.Mail 老是触发异常,后来改用 System.Web.Mail 才成功了,可以看下我代码里的区别;ASP.NET MVC 3 提供了 WebMail 类来发送邮件,更加方便。

  控制台程序、WPF、WebForm 及 ASP.NET MVC 我都测试了一下,控制台程序和 WPF 都用了 System.Net.Mail ,WebForm 和 ASP.NET MVC 都可以用 System.Web.Mail ,而 ASP.NET MVC 3 直接用 WebMail 更方便。下面我把代码分别贴出来。

  控制台程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;//添加引用 namespace SendEmail
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
bool flag = p.SendEmail();
if (flag)
{
Console.Write("Success !");
} Console.Read();
} public bool SendEmail()
{
MailMessage msg = new MailMessage();
msg.To.Add("1234567@qq.com");//邮件接收者的帐号
msg.From = new MailAddress("1234567@qq.com", "nickname", System.Text.Encoding.UTF8);//发送邮件的帐号及显示名称和字符编码
msg.Subject = "Subject";//邮件标题
msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
msg.Body = "Content";//邮件内容
msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
msg.IsBodyHtml = false;//是否是HTML邮件
msg.Priority = MailPriority.High;//邮件优先级 SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("1234567@qq.com", "password");//注册的邮箱和密码,就QQ邮箱而言,如果设置了独立密码,要用独立密码代替密码
client.Host = "smtp.qq.com";//QQ邮箱对应的SMTP服务器
object userState = msg;
try
{
client.SendAsync(msg, userState);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
}

  WPF:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;//添加引用
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WPFSendMail
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void Button_Click(object sender, RoutedEventArgs e)
{
bool flag = SendMail();
if (flag)
{
MessageBox.Show("Success !","Tips");
}
} public bool SendMail()
{
MailMessage message = new MailMessage();
message.To.Add("1234567@qq.com");
message.From = new MailAddress("1234567@qq.com","nickname",Encoding.UTF8);
message.Subject = "Sending e-mail in WPF !";
message.SubjectEncoding = Encoding.UTF8;
message.Body = "Awesome";
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = false;
message.Priority = MailPriority.Normal;
Attachment att = new Attachment("text.txt");//添加附件,确保路径正确
message.Attachments.Add(att); SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential("1234567@qq.com","password");
smtp.Host = "smtp.qq.com";
object userState = message; try
{
smtp.SendAsync(message,userState);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"Tips");
return false;
}
}
}
}

  WebForm:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mail;//添加引用 namespace WebFormSendEmail
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
bool flag = SendMail();
if (flag)
{
Response.Write("<script>alert('Success !');</script>");
}
} public bool SendMail()
{
MailMessage message = new MailMessage();
message.To = "1234567@qq.com";
message.From = "1234567@qq.com";
message.Subject = "Sending e-mail in Web !";
message.Body = "Awesome";
//基本权限
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "");
//用户名
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "1234567@qq.com");
//密码
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password"); SmtpMail.SmtpServer = "smtp.qq.com";
try
{
SmtpMail.Send(message);
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
}
}
}

  ASP.NET MVC :

try
{
WebMail.SmtpServer = "smtp.qq.com";
WebMail.SmtpPort = ;//端口号,不同SMTP服务器可能不同,可以查一下
WebMail.EnableSsl = false;//禁用SSL
WebMail.UserName = "1234567@qq.com";
WebMail.Password = "password";
WebMail.From = "1234567@qq.com";
WebMail.Send("1234567@qq.com","Subject",“Content");
}
catch (Exception)
{ }

C# 发送邮件整理,包括控制台程序、WPF、WebForm 及 ASP.NET MVC的更多相关文章

  1. WebForm和Asp.Net MVC的理解

    我对WebForm和Asp.Net MVC的理解   比较WebForm和Mvc的请求处理方式 首先简单了解一下Asp.Net中怎么对页面进行请求处理的: 在管道的第7-8个事件之间,有一个MapHt ...

  2. 性能差异 ASP.NET WebForm与ASP.NET MVC

    一.为什么说 ASP.NET WebForm 比 ASP.NET MVC 要差? WebForm 顾名思义,微软一向主打简单化,窗体模式,拖拽控件就能做网站了, 然而这也引发了许多 Java 和 .N ...

  3. ASP.NET Webform和ASP.NET MVC的区别

    ASP.NET WebForm ASP.NET Webform提供了一个类似于winform的事件响应GUI模型(event-driven GUI),隐藏了HTTP.HTML.JavaScript等细 ...

  4. 关于ASP.NET WebForm与ASP.NET MVC的比较

      WebForm的理解 1. WebForm概念 ASP.NETWebform提供了一个类似于Winform的事件响应GUI模型(event-drivenGUI),隐藏了HTTP.HTML.Java ...

  5. [Entity Framework+MVC复习总结1]-WebForm与Asp.Net MVC

    一.Web开发方式的比较 二.web Form开发模型 WebForm开发优点: 1.支持事件模型开发.得益于丰富的服务器端组件,webfrom开发可以迅速的搭建web应用 2.使用方便,入门容易 3 ...

  6. Response.End()在Webform和ASP.NET MVC下的表现差异

    前几天在博问中看到一个问题--Response.End()后,是否停止执行?MVC与WebForm不一致.看到LZ的描述后,虽然奇怪于为何用Response.End()而不用return方式去控制流程 ...

  7. ASP.NET WebForm与ASP.NET MVC的不同点

    ASP.NET WebForm ASP.NET MVC ASP.NET Web Form 遵循传统的事件驱动开发模型 ASP.NET MVC是轻量级的遵循MVC模式的请求处理响应的基本开发模型 ASP ...

  8. ABP示例程序-使用AngularJs,ASP.NET MVC,Web API和EntityFramework创建N层的单页面Web应用

    本片文章翻译自ABP在CodeProject上的一个简单示例程序,网站上的程序是用ABP之前的版本创建的,模板创建界面及工程文档有所改变,本文基于最新的模板创建.通过这个简单的示例可以对ABP有个更深 ...

  9. ASP.NET Webform或者ASP.NET MVC站点部署到IIS下,默认情况下.json文件是不能被访问的,如果请求访问.json文件,则会出现找不到文件的404错误提示

    解决方法 <system.webServer> <staticContent> <remove fileExtension=".woff" /> ...

随机推荐

  1. 一个 XSD 实例

    一个 XSD 实例 本节会为您演示如何编写一个 XML Schema.您还将学习到编写 schema 的不同方法. XML 文档 让我们看看这个名为 "shiporder.xml" ...

  2. 使用Redis构建简单的ORM

    Reids相关的资料引用 http://www.tuicool.com/articles/bURJRj [Reids各种数据类型的应用场景] https://github.com/antirez/re ...

  3. PAT-乙级-1021. 个位数统计 (15)

    1021. 个位数统计 (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个k位整数N = dk-1 ...

  4. tomcat部署javaweb项目的三种方式

    一.将项目文件夹或war包直接拷贝到tomcat的webapps下 二.在Tomcat\conf\Catalina\localhost下建立xml文件 修改内容如下<Context path=& ...

  5. 【转】linux C++ 获取文件信息 stat函数详解

    stat函数讲解 表头文件:    #include <sys/stat.h>             #include <unistd.h>定义函数:    int stat ...

  6. Java List详解

    就是一种集合对象,将所有的对象集中到一起存储. list里面可以放java对象,可以直接放值. List list = new ArrayList(); list.add("AAA" ...

  7. GridBagLayout()的使用方法

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...

  8. SQL数据库还原时备份集中的数据库备份与现有的数据库不同的解决办法

    SQL Server 2005数据库还原出错错误具体信息为:备份集中的数据库备份与现有的A数据库不同 具体操作如下:第一次:新建了数据库A,数据库文件放在E:\DB\A目录下,选中该数据库右键-任务- ...

  9. java遍历树(深度遍历和广度遍历

    java遍历树如现有以下一颗树:A     B          B1               B11          B2               B22     C          C ...

  10. linux文件系统-基本磁盘2

    直入主题-基本磁盘 硬盘数据按照不同特点和作用大致分为5部分:MBR区.DBR区.FAT区.DIR区和DATA区 1.MBR MBR(Main Boot Record 主引导记录区)位于整个硬盘的0磁 ...