C# 发送邮件整理,包括控制台程序、WPF、WebForm 及 ASP.NET MVC
一直想把发送邮件的功能掌握,总是各种情况拖着了,这两天终于看了一下,整理一下,希望能帮到想学的。
发送邮件使用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的更多相关文章
- WebForm和Asp.Net MVC的理解
我对WebForm和Asp.Net MVC的理解 比较WebForm和Mvc的请求处理方式 首先简单了解一下Asp.Net中怎么对页面进行请求处理的: 在管道的第7-8个事件之间,有一个MapHt ...
- 性能差异 ASP.NET WebForm与ASP.NET MVC
一.为什么说 ASP.NET WebForm 比 ASP.NET MVC 要差? WebForm 顾名思义,微软一向主打简单化,窗体模式,拖拽控件就能做网站了, 然而这也引发了许多 Java 和 .N ...
- ASP.NET Webform和ASP.NET MVC的区别
ASP.NET WebForm ASP.NET Webform提供了一个类似于winform的事件响应GUI模型(event-driven GUI),隐藏了HTTP.HTML.JavaScript等细 ...
- 关于ASP.NET WebForm与ASP.NET MVC的比较
WebForm的理解 1. WebForm概念 ASP.NETWebform提供了一个类似于Winform的事件响应GUI模型(event-drivenGUI),隐藏了HTTP.HTML.Java ...
- [Entity Framework+MVC复习总结1]-WebForm与Asp.Net MVC
一.Web开发方式的比较 二.web Form开发模型 WebForm开发优点: 1.支持事件模型开发.得益于丰富的服务器端组件,webfrom开发可以迅速的搭建web应用 2.使用方便,入门容易 3 ...
- Response.End()在Webform和ASP.NET MVC下的表现差异
前几天在博问中看到一个问题--Response.End()后,是否停止执行?MVC与WebForm不一致.看到LZ的描述后,虽然奇怪于为何用Response.End()而不用return方式去控制流程 ...
- ASP.NET WebForm与ASP.NET MVC的不同点
ASP.NET WebForm ASP.NET MVC ASP.NET Web Form 遵循传统的事件驱动开发模型 ASP.NET MVC是轻量级的遵循MVC模式的请求处理响应的基本开发模型 ASP ...
- ABP示例程序-使用AngularJs,ASP.NET MVC,Web API和EntityFramework创建N层的单页面Web应用
本片文章翻译自ABP在CodeProject上的一个简单示例程序,网站上的程序是用ABP之前的版本创建的,模板创建界面及工程文档有所改变,本文基于最新的模板创建.通过这个简单的示例可以对ABP有个更深 ...
- ASP.NET Webform或者ASP.NET MVC站点部署到IIS下,默认情况下.json文件是不能被访问的,如果请求访问.json文件,则会出现找不到文件的404错误提示
解决方法 <system.webServer> <staticContent> <remove fileExtension=".woff" /> ...
随机推荐
- Java 数组的三种创建方法
public static void main(String[] args) { //创建数组的第一种方法 int[] arr=new int[6]; int intValue=arr[5]; //S ...
- google protobuf 使用示例
定义.proto接口文件 package tutorial; message Person { required ; required int32 id = ; //unique ID number ...
- VB断点大全
MultiByteToWideChar, ANSI字符串转换成Unicode字符串WideCharToMultiByte, Unicode字符串转换成ANSI字符串 //--------------- ...
- CSRF攻击原理解析与对策研究
1.引言 跨站点请求伪造(Cross—Site Request Forgery).以下简称CSRF.是一种广泛存在的网站漏洞.Gmail.YouTube等著名网站都有过CSRF漏洞.甚至包 ...
- MCM试题原文及翻译 AB题 2014美国数学建模竞赛
MCM试题原文及翻译 AB题 2014美国数学建模竞赛 原创翻译,如有瑕疵,敬请谅解. 转载请注明:过客小站 » MCM试题原文及翻译 AB题 2014美国数学建模竞赛 PROBLEM A: The ...
- Nagios监控部署(转)
转自 http://kyhack.blog.51cto.com/490370/213355 ky.blog 一.nagios简介 nagios是一款用于系统和网络监控的应用程序,它可以在 ...
- Head First 设计模式笔记:单例模式
单例模式 确保一个类只有一个实例,并提供一个全局访问点. 类图: Singleton static uniqueInstance //其他属性... static getInstance() //其他 ...
- PJSIP在windows(xp或者win7)下的编译,编译工具是vs2008,PJSIP版本2.3
PJSIP是一个开源的SIP协议库,它实现了SIP.SDP.RTP.STUN.TURN和ICE.PJSIP作为基于SIP的一个多媒体通信框架提供了非常清晰的API,以及NAT穿越的功能.PJSIP具有 ...
- 算法总结之欧拉函数&中国剩余定理
算法总结之欧拉函数&中国剩余定理 1.欧拉函数 概念:在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)( ...
- Django自定义用户认证系统Customizing authentication
扩展已有的用户模型Extending the existing User model 有两种方法来扩展默认的User Model而不用重写自己的模型.如果你不需要改变存储在数据库中的字段,而只是需要改 ...