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" /> ...
随机推荐
- 1057: [ZJOI2007]棋盘制作 - BZOJ
Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴 ...
- Extjs-4.2.1(一)——编辑 hello word
前言:在搭建好环境后,就可以利用Extjs进行开发了. 一.在项目中拷贝相关的Extjs文件,如下图: 注意:不需要整个Ext开发包全部导入,这样很容易造成eclipse卡死,因为eclipse会自动 ...
- .substr()在字符串每个字母前面加上一个1
var str = "abcdefghijklmnopq", name = "1", ary = []; for(var i = 0,len = str.len ...
- 获取属性名:PropertyNameHelper
获取属性名:PropertyNameHelper namespace NCS.Infrastructure.Querying { public static class PropertyNameHel ...
- python top project of 2013
Hi Pythonistas! 测试和调试 Testing & Debugging 框架及Web Frameworks & Web 并发 Concurrency 任务调度 Job Sc ...
- DLL 支持MFC 没有DLLMAIN函数
如果使用VC编写DLL时,需要MFC功能: 一般在源文件里就不能手动写DLLMAIN函数了 它给MFC集成了,\src\mfc\dllmodule.cpp打开它,里面有有一个DLLMAIN函数,根据源 ...
- 跌跌撞撞,用MAVEN看图说话的编译了一个JAR出来
URL: http://www.yiibai.com/maven/create-a-java-project-with-maven.html mvn archetype:generate -Dgrou ...
- USB Type-C工作原理解析
自从苹果发布了新MacBook,USB Type-C接口就成为了热议对象.我来从硬件角度解析下这个USB Type-C,以便大家更好的了解USB Type-C的工作原理. 特色 尺寸小,支持正反插,速 ...
- getchar()函数的返回值赋给char型,用if(ch=getchar() != EOF)测试,输入ctrl+z同样可以结束循环的分析
2013-07-18 21:35:58 getchar()函数的返回值赋给char型,用if(ch=getchar() != EOF)测试,输入ctrl+z同样可以结束循环的分析. char是字符型数 ...
- poj2750Potted Flower (线段树)
http://poj.org/problem?id=2750 之前做过类似的题 把一段的左连续最大.最小 右连续最大及最小及中间的连续更新出 就可以算出这段最大的连续和 注意不能全部加上 加上一特判 ...