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" /> ...
随机推荐
- 记一次apt-get无法安装git的问题
解决apt-get安装过程中出现的Size mismatch和Hash Sum mismatch问题. 事情起因 我从单位复制了一个Virtualbox虚拟机(ubuntu 15.04 Desktop ...
- 2002: [Hnoi2010]Bounce 弹飞绵羊 - BZOJ
Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置 ...
- uva10943
递推 还是比较容易的 /************************************************************************* > Author: ...
- CodeForces 32C
额 找找规律吧 要用long long 才过. #include <cstdio> #include <algorithm> using namespace std; in ...
- c++ ANSI、UNICODE、UTF8互转
static std::wstring MBytesToWString(const char* lpcszString); static std::string WStringToMBy ...
- spring IOC容器实例化Bean的方式与RequestContextListener应用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...
- UML各种图画法总结
<UML 2.4.1 教程> http://www.sparxsystems.cn/resources/uml2_tutorial/ <UML总结(对九种图的认识和如何使用Ratio ...
- 编写高效的C程序与C代码优化
本文地址:http://www.cnblogs.com/archimedes/p/writing-efficient-c-and-code-optimization.html,转载请注明源地址. 说明 ...
- freemarker得到数组的长度
取得list的长度:${fields?size}.用?size不是用?length,代码如下所示: <#list properties as item> <#assign layer ...
- 读取Excel任务列表并显示在Outlook日历上
前几天,公司发了一个任务安排,时间不固定,但要求准时到,为了给自己加一个提醒,也为了回顾一下以前的技术,特做了一个Demo. 读取Excel就不多说了,代码很简单,但支持老版本Excel和的版本Exc ...