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

  发送邮件使用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. 2338: [HNOI2011]数矩形 - BZOJ

    因为已经看了一眼题解,知道是算中点和长度,相同时构成一个矩形,所以就把所有的线段算出来,然后排序,相同的就更新答案 为了避免误差,我们都用整数存,中点直接相加就行了,没必要除2,长度也只要平方就行了, ...

  2. Web网站架构设计

    目录 [隐藏/显示] 1 - Web负载均衡   1.1 - 使用商业硬件实现   1.2 - 使用开源软件   1.3 - 使用windows自带的互载均衡软件   1.4 - 总结2 - 静态网站 ...

  3. 深入js的面向对象学习篇(封装是一门技术和艺术)——温故知新(二)

    下面全面介绍封装和信息隐藏. 通过将一个方法或属性声明为私用的,可以让对象的实现细节对其它对象保密以降低对象之间的耦合程度,可以保持数据的完整性并对其修改方式加以约束.在代码有许多人参与设计的情况下, ...

  4. hdu 1085

    额    母函数 #include <cstdio> #include <cstring> int a[3],b[3]= {1,2,5}; int c1[10001],c2[1 ...

  5. 16进制字符串转数字(C/C++,VB/VB.net,C#)

    这个问题看是很简单,但是在不同语言中实现的方式却千差万别,如果不知道方法,还真是麻烦,我就是在C#中遇到该问题,让我费了很大的周折,才在msdn查到. 一.16进制字符串转数字      1.C/C+ ...

  6. 【leetcode】Longest Palindromic Substring (middle) 经典

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. Objective-C 协议(protocol)

    协议(protocol)是Objective-c中一个非常重要的语言特性,从概念上讲,非常类似于JAVA中接口. 一个协议其实就是一系列有关联的方法的集合(为方便后面叙述,我们把这个协议命名为myPr ...

  8. 安装Redis完整过程

    概述    首先报告一下我系统的版本: [root@firefish init.d]# cat /etc/issue 系统版本信息如下: 引用 CentOS release 6.4 (Final) K ...

  9. valgrind基本使用

    1.valgrind是一个内存检测工具,类似的还有purify,insure++等 2.测试文件test.c test.c : main(){ int* a=new int[100]; return ...

  10. 使用typeid(变量或类型).name()来获取常量或变量的类型---gyy整理

    使用typeid(变量或类型).name()来获取常量或变量的类型 <typeinfo>  该头文件包含运行时类型识别(在执行时确定数据类型)的类 typeid的使用   typeid操作 ...