用System.Net.Mail发送邮件

最近有不少朋友询问关于如何在.NET 2.0中发送邮件的问题,这里写一个简单的例子供朋友参考。

MSDN参考( 点击查看MSDN的中英文对照):
            SmtpClient( http://msdn2.microsoft.com/zh-cn/library/system.net.mail.smtpclient(VS.80).aspx)
            MailMessage( http://msdn2.microsoft.com/zh-cn/library/system.net.mail.mailmessage(VS.80).aspx)

下面是一段简单的发邮件代码(控制台程序):
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
using  System.Net.Mail;
None.gif
None.gif
namespace  MailSender
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static string strHost = string.Empty;
InBlock.gif        
static string strAccount = string.Empty;
InBlock.gif        
static string strPwd = string.Empty;
InBlock.gif        
static string strFrom = string.Empty;
InBlock.gif
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strHost 
= "smtp.163.com";   //STMP服务器地址
InBlock.gif
            strAccount = "****";       //SMTP服务帐号
InBlock.gif
            strPwd = "123456789";       //SMTP服务密码
InBlock.gif
            strFrom = "***@163.com";  //发送方邮件地址
InBlock.gif

InBlock.gif            Console.WriteLine(sendMail(
"***@qq.com""这是一封测试邮件""这是一封测试邮件的正文内容"? "Success" : "Unsuccess");
InBlock.gif
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 发送邮件
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="to">接收方邮件地址</param>
InBlock.gif        
/// <param name="title">邮件标题</param>
InBlock.gif        
/// <param name="content">邮件正文内容</param>
InBlock.gif        
/// <returns></returns>
InBlock.gif        
/// <author>Jailu</author>
ExpandedSubBlockEnd.gif        
/// <date>2007-04-10</date>

InBlock.gif        static bool sendMail(string to, string title, string content)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SmtpClient _smtpClient 
= new SmtpClient();
InBlock.gif            _smtpClient.DeliveryMethod 
= SmtpDeliveryMethod.Network;//指定电子邮件发送方式
InBlock.gif
            _smtpClient.Host = strHost; ;//指定SMTP服务器
InBlock.gif
            _smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//用户名和密码
InBlock.gif

InBlock.gif            MailMessage _mailMessage 
= new MailMessage(strFrom,to);
InBlock.gif            _mailMessage.Subject 
= title;//主题
InBlock.gif
            _mailMessage.Body = content;//内容
InBlock.gif
            _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
InBlock.gif
            _mailMessage.IsBodyHtml = true;//设置为HTML格式
InBlock.gif
            _mailMessage.Priority = MailPriority.High;//优先级
InBlock.gif

InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _smtpClient.Send(_mailMessage);
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值