.net System.Web.Mail发送邮件 (设置发件人 只显示用户名)

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 = "[email protected]";

mail.From = "[email protected]";

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 = "[email protected]";

mail.From = "[email protected]";

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 = "[email protected]";

mail.From = "[email protected]";

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,修改发件人和收件人的名称

比如发件人的地址是[email protected],我们用outlook收到信,From一栏里将直接显示[email protected]

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

比如显示Tony Gong

方法如下:

[ C# ]

MailMessage mail = new MailMessage();

mail.To = "\"John\" <[email protected]>";

mail.From = "\"Tony Gong\" <[email protected]>";

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 = "[email protected];[email protected];[email protected]";

mail.From = "[email protected]";

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 = "[email protected]";

mail.From = "[email protected]";

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 = "[email protected]";

mail.From = "[email protected]";

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 );

时间: 2024-08-09 06:06:07

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

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

转载别人的 使用SMTP发送邮件 说到邮件发送,先提一下SMTP. SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议.它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式.SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地.SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器.  再简单介绍一下名称空间(Namespace)System.Web.Mail类库里所提供的邮件发送的对象

C# 使用System.Net.Mail;发送邮件Email

发送邮件是很简单的功能,但是会经常用到,这里写一下最简单的方法. //记得引用 using System.Net.Mail; /// <summary> /// 发送电子邮件 /// </summary> /// <param name="smtpserver">SMTP服务器</param> /// <param name="userName">登录帐号</param> /// <par

System.Web.Mail发邮件引发0x80040217错误的解决过程

 使用System.Web.Mail 写了一个发送邮件程序,一直都工作正常. 最近更换新邮件服务器后,发送邮件时出现0x80040217错误. 从网上搜了一下,有用的结果不多,有说是Web.Mail过时,要改用system.net.mail等等. 后来跟踪代码,发现是进行到验证的步骤时出的问题,于是尝试去掉"@域名",真的可以了. 那为什么不提示用户名错误呢,通过使用控制台telnet测试发现, 新邮件服务的连接,在任何指令出错后都会立即断开,从而引发代码0x80040217错误.

linux利用mail发送邮件设置

linux利用mail发送邮件 第一步检查的mail命令是否安装 第二步如果设置mailx smtp set [email protected] set smtp=smtp.163.com set smtp-auth-user=myname set smtp-auth-password=password (客户端授权密码) set smtp-auth=login 第三步163邮箱开启smtp pop3 注意:一般自己发送给自己.发送给其他账号可能出现问题. 在Linux系统下mail命令的测试

使用System.Net.Mail发送邮件

引用命名空间: using System.Net.Mail; /// <summary> /// 发送HTML邮件,有抄送和密送 /// 需要在Web.config文件中的system.net下的mailSettings节点进行配置 /// </summary> /// <param name="mailto">收件人地址</param> /// <param name="mailcc">抄送地址</

设置View只显示透明下边框、透明背景框、阴影背景框的方法

实现的效果如下: 下面的代码是实现一个带边框的xml,很常见 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@android:color/transparent" /> <str

MySQL之MySQL:prompt 设置 -登陆MySQL显示用户名和主机以及当前数据库

是开始但愿不是结束,是起点但愿不是终点 今天在做MySQL练习过程中,突然发现,登陆mysql后,命令行不能显示用户名和主机信息,同时呢,想查看数据库信息还需要查看select datebase()命令查看,特别不方便,因此特别查看了mysql的相关资料,每次通过客户端登陆怎么才能显示这些信息呢?其实MySQL的相关设置都在配置文件my.cnf中可以设置.因此就去看my.cnf文件,之前就使用下,没有细心看.my.cnf配置文件中两大部分:[client]和[mysqld],前者是客户端的配置参

System.net.mail 腾讯ssl发送邮件超时

我采用了.net 的自带组件 System.Web.Mail.MailMessage发送邮件,主要是在客户注册网站成功的时候发条欢迎邮件,最近邮件无法发送了,看了下腾讯smtp邮件配置,所有的邮件发送都换成ssl了,之前用的是25端口,现在换成了465或587,于是修改代码如下: MailMessage msgMail = new MailMessage("发件箱", "收件箱", "邮件标题", "邮件内容",2); Smt

asp.net 发送邮件代码 System.Net.Mail

前台页面 SendEmail.aspx 代码 using System.Net.Mail;using System.Net; <h2> 发送电子邮件演示 </h2> <table cellpadding="0" cellspacing="0" border="0" style="font-family: 宋体, Arial, Helvetica, sans-serif; font-size: 15px; w