http://blog.163.com/hao_2468/blog/static/130881568201141251642215/

.net System.Web.Mail发送邮件

2011-05-12 17:16:42|  分类: asp.net学习 |  标签:.net发送邮件  |举报|字号 订阅

 
 

net System.Web.Mail发送邮件

用System.Web.Mail发送邮件,适用于.net1.1。net2.0请用System.Net.Mail

先引用System.Web

1,发送简单邮件

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "you@yourcompany.com";

mail.Subject = "this is a test email.";

mail.Body = "this is my test email body";

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

这里的smtpserver只能是那些不需要验证的smtp服务器,像126,sina,yahoo等等的邮箱,都需要验证,所以不能用。用这些邮箱发信后面会讲到

2,发送Html邮件

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "you@yourcompany.com";

mail.Subject = "this is a test email.";

mail.BodyFormat = MailFormat.Html;

mail.Body = "this is my test email body.<br><b>this part is in bold</b>";

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

3,发送附件

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "you@yourcompany.com";

mail.Subject = "this is a test email.";

mail.Body = "this is my test email body.";

MailAttachment attachment = new MailAttachment( Server.MapPath( "test.txt" ) ); //create the attachment

mail.Attachments.Add( attachment ); //add the attachment

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

4,修改发件人和收件人的名称

比如发件人的地址是abc@126.com,我们用outlook收到信,From一栏里将直接显示abc@126.com.

能不能在From一栏里显示友好一点的名字呢?

比如显示Tony Gong

方法如下:

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "\"John\" <me@mycompany.com>";

mail.From = "\"Tony Gong\" <you@yourcompany.com>";

mail.Subject = "this is a test email.";

mail.Body = "this is my test email body.";

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

5,发送给多人

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com;him@hiscompany.com;her@hercompany.com";

mail.From = "you@yourcompany.com";

mail.Subject = "this is a test email.";

mail.Body = "this is my test email body.";

SmtpMail.SmtpServer = "localhost"; //your real server goes here

SmtpMail.Send( mail );

6,用需要Smtp验证的邮箱发信

现在为了防止垃圾邮件,绝大部分Smtp服务器需要验证了

发信方法如下:

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "abc@126.com";

mail.Subject = "this is a test email.";

mail.Body = "Some text goes here";

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); //set your username here

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); //set your password here

SmtpMail.SmtpServer = "smtp.126.com"; //your real server goes here

SmtpMail.Send( mail );

7,修改smtp服务器的端口,以及使用SSL加密

大部分smtp服务器的端口是25,但有些却不是

同时,绝大部分Smtp服务器不需要SSL登陆,有些却需要

比如Gmail,smtp端口是:465,同时支持SSL

代码如下:

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "me@mycompany.com";

mail.From = "abc@126.com";

mail.Subject = "this is a test email.";

mail.Body = "Some text goes here";

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "abc"); //set your username here

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "your password"); //set your password here

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport",465);

mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");

SmtpMail.SmtpServer = "smtp.126.com"; //your real server goes here

SmtpMail.Send( mail );

.net System.Web.Mail发送邮件 (设置发件人 只显示用户名)的更多相关文章

  1. System.Web.mail ----虚拟发件人发送邮件

     转载别人的 使用SMTP发送邮件   说到邮件发送,先提一下SMTP. SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议.它是一组用于从源地址到目的 ...

  2. C# 发送电子邮件(含附件)用到的类 system.web.mail

    主要是用到了System.Web.Mail命名空间,用到了此空间的三个类,分别是: ●MailMessage类,用于构造电子邮件●MailAttachment类,用于构造电子邮件附件●SmtpMail ...

  3. C#中使用System.Web.Mail.MailMessage类无法CC多人的问题

    从.NET 2.0 开始,引入了一个新的类,System.Net.Mail.MailMessage.该类用来取代 .NET 1.1 时代System.Web.Mail.MailMessage类.Sys ...

  4. 利用System.Net.Mail 发送邮件

    我这里只是试了一下发mail的功能,感觉.net自带的发mail是比较全的,还是直接上我的code 参数文章:System.Net.Mail 发送邮件 SMTP协议 using System; usi ...

  5. C# System.Web.Mail.MailMessage 发邮件

    C# System.Web.Mail.MailMessage 发邮件 新建控制台Console项目,然后添加 System.Web引用 代码如下: using System; using System ...

  6. C#使用 System.Net.Mail发送邮件功能

    .NET 里包含了很多很丰富的邮件发送与接受的API在 System.Net.Mail命名空间里,使得我们开发发送和接受邮件相关功能变得简单,下面是一个简单发送邮件的功能: private void ...

  7. 使用System.Net.Mail发送邮件

    引用命名空间: using System.Net.Mail; /// <summary> /// 发送HTML邮件,有抄送和密送 /// 需要在Web.config文件中的system.n ...

  8. echarts将图表Y坐标刻度设置成只显示整数

    echarts的配置项中没有直接将坐标刻度强制设为整数的选项,但可以通过minInterval属性将刻度以整数形式显示,在配置项的yAxis对象中添加属性: minInterval: 1 表示将刻度的 ...

  9. 转 EasyUi日期控件datebox设置,只显示年月,也只能选择年月

    1.引入Jquery和easyui,注低版本的Jquery和easy不能使用,这里使用的Jquery是1.8.2easyui是1.6.1.1.easyui下载地址:http://www.jeasyui ...

随机推荐

  1. Handler 机制(一)—— Handler的实现流程

    由于Android采用的是单线程模式,开发者无法在子线程中更新 UI,所以系统给我提供了 Handler 这个类来实现 UI 更新问题.本贴主要说明 Handler 的工作流程. 1. Handler ...

  2. Python之路【第二十八篇】:生成器与迭代器

    #!/usr/bin/env python # -*- coding:utf-8 -*- #只要函数的代码里面出现了yield关键字,这个函数就不再是一个普通的函数了,叫做生成器函数 #执行生成器函数 ...

  3. 是时候实现 SOC 2.0 了

    本文讲的是是时候实现 SOC 2.0 了,SOC,安全运营中心,为取得其最佳效果,以及真正最小化网络风险,需要全员就位,让安全成为每个人的责任. 早在几年前,企业就开始创建SOC来集中化威胁与漏洞的监 ...

  4. R 语言命令行参数处理

    在unix.windows外部需要调用R脚本执行,然后又需要输入不同的参数,类似shell脚本的命令行参数输入,可以使用Rcript命令实现. 命令格式:Rscript [options] [-e e ...

  5. The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 A Who is better?

    A After Asgard was destroyed, tanker brought his soldiers to earth, and at the same time took on the ...

  6. python selenium(用例断言)

    1.if ...else ...判断进行断言 from time import * from selenium import webdriver "): driver = webdriver ...

  7. Linux服务器有大量的TIME_WAIT状态

    我们经常会遇到在服务器上看到大量的TIME_WAIT,它们占用进程不释放,最后会导致所有进程数被耗完,服务器负载增高等生产事故,具体是什么原因导致的呢?我们先来看看TCP的三次握手四次挥手都是怎样的一 ...

  8. Jenkins如何进行权限管理

    一.安装插件 插件名:Role-based Authorization Strategy 二.配置授权策略 三.创建用户 四.添加并配置权限 4.1.添加Global Role 普通角色拥有全局只读权 ...

  9. TCP 可靠传输

    TCP报文段首部 序号: TCP是面向字节流的.在一个TCP连接中传送的字节流中的每一个字节都按顺序编号.整个要传送的字节流的起始序号必须在连接建立时设置.首部中的序号字段值则指的是本报文段所发送的数 ...

  10. Web框架,Hibernate向数据库插入数据,数据库没有值怎么办?

    用web框架技术,使用Hibernate向数据库添加信息,控制台显示插入成功的语句,可是数据库却没有值:错误如下: (1)不要自己创建数据库!!,Web框架可以自己自动生成,自己创建可能会报错! (2 ...