在asp.net 2.0中发送邮件

1、在web.config中的<configuration>内加入如下配置信息(host—smtp服务地址;port—端口号;userName—用户名;password—密码。请自行修改)。

   < system .net >
    
< mailSettings >

      
< smtp >
        
< network  host ="smtpserver"  port ="25"  userName ="uid"  password ="pwd"   />
      
</ smtp >

    
</ mailSettings >
  
</ system.net >

2、aspx页面HTML代码

     < table  border ="0" >
        
< tr >
            
< td >
                发件人
            
</ td >
            
< td >
                
< asp:TextBox  runat ="server"  ID ="emailfrom" ></ asp:TextBox >
            
</ td >
        
</ tr >
        
< tr >
            
< td >
                收件人
            
</ td >
            
< td >
                
< asp:TextBox  runat ="server"  ID ="emailto" ></ asp:TextBox >
            
</ td >
        
</ tr >
        
< tr >
            
< td >
                主题
            
</ td >
            
< td >
                
< asp:TextBox  runat ="server"  ID ="subject" ></ asp:TextBox >
            
</ td >
        
</ tr >
        
< tr >
            
< td >
                附件
            
</ td >
            
< td >
                
< asp:FileUpload  ID ="attachment"  runat ="server"   />
            
</ td >
        
</ tr >
        
< tr >
            
< td >
                内容
            
</ td >
            
< td >
                
< asp:TextBox  runat ="server"  ID ="body"  TextMode ="MultiLine"  Columns ="50"  Rows ="10" ></ asp:TextBox >
            
</ td >
        
</ tr >
        
< tr >
            
< td  colspan ="2"  align ="center" >
                
< asp:Button  runat ="server"  ID ="btnSend"  Text ="发送"  OnClick ="btnSend_Click"   />
            
</ td >
        
</ tr >
    
</ table >

3、实例化一个MailMessage并设置其属性

        MailMessage mm  =   new  MailMessage(emailfrom.Text, emailto.Text);

        mm.Subject 
=  subject.Text;
        mm.Body 
=  body.Text;
        
//  HTML格式
        mm.IsBodyHtml  =   true ;

        
//  添加附件
        mm.Attachments.Add( new  Attachment(attachment.PostedFile.InputStream, attachment.FileName));

        
/**/ /*其他如抄送、优先级之类的都可以在MailMessage类的属性中设置*/


4、实例化一个SmtpClient,调用其Send方法,参数为MailMessage对象

        SmtpClient sc  =   new  SmtpClient();

        
//  编程方式设置smtp(不用web.config)
        
//  sc.Host = "";
        
//  sc.Port = 25;
        
//  sc.Credentials = new NetworkCredential("username", "password");

        
try
        
{
            sc.Send(mm);
            Response.Write(
"ok");
        }

        
catch  (Exception ex)
        
{
            
// 与smtp相关的错误
            if (ex is SmtpException)
            
{
                
// ex.ToString();
                Response.Write("smtp发信失败");
            }

            
else
            
{
                Response.Write(ex.ToString());
            }

        }


转载地址:http://www.cnblogs.com/webabcd/archive/2007/01/03/610735.html
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值