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. 为什么要你们现在要学习python

    说学习python之前,我们先来聊聊其他的.我们都认为成功靠的是勤奋和努力,但是事实是只靠勤奋和努力是不一定会成功的,而且很大一部分都不会成功. 你有没有想过,同样是做企业,有些公司年收入百万,而腾讯 ...

  2. CtsVerifier-Bluetooth-LE-SEcure-ClientServer-Test测试pass但是无法选择passbutton

    [问题描述] CtsVerifier-Bluetooth-LE-SEcure-ClientServer-Test测试pass但是无法选择Pass-Button 工具版本:9.0-r11 其他信息: 上 ...

  3. #Week4 Logistic Regression

    一.Classification 主要讨论二元分类. 线性回归处理分类问题显然不靠谱,所以采用逻辑回归. 二.Hypothesis Representation 假设函数变为\(h_\theta(x) ...

  4. VScode像Codeblocks一样,不启动调试和Debug直接运行

    要是配置C++ 编译环境,这边走 用了VScode童鞋,都知道,写C++是不保留窗口的,除非打上断点或者: system("pause"); 这里给大家分享一种不需要,F5或者Ct ...

  5. Composition API

    介绍 Composition API的主要思想是,我们将它们定义为从新的 setup 函数返回的JavaScript变量,而不是将组件的功能(例如state.method.computed等)定义为对 ...

  6. windows远程执行命令总结

    1. 利用Impacket Impacket是一个Python类库,用于对SMB1-3或IPv4 / IPv6 上的TCP.UDP.ICMP.IGMP,ARP,IPv4,IPv6,SMB,MSRPC, ...

  7. C - A Plug for UNIX POJ - 1087 网络流

    You are in charge of setting up the press room for the inaugural meeting of the United Nations Inter ...

  8. ACM入门问题:最大利益问题

    设最大的利益为maxv,最小值为minv 1.求最大利益的简单算法 ;j<=n-;j++) ;i<=j-;i++) maxv =(maxv与R[j]-R[i]中较大的一个) maxv=ma ...

  9. Coursera课程笔记----计算导论与C语言基础----Week 4

    感性认识计算机程序(Week 4) 引入 编程序 = 给计算机设计好运行步骤 程序 = 人们用来告诉计算机应该做什么的东西 问题➡️该告诉计算机什么?用什么形式告诉? 如果要创造一门"程序设 ...

  10. leetcode_雇佣 K 名工人的最低成本(优先级队列,堆排序)

    题干: 有 N 名工人. 第 i 名工人的工作质量为 quality[i] ,其最低期望工资为 wage[i] . 现在我们想雇佣 K 名工人组成一个工资组.在雇佣 一组 K 名工人时,我们必须按照下 ...