vs2005 下的发邮件代码

sendemail.aspx 
< body >
    
< form  id ="form1"  runat ="server" >
        
< div >
            发件人:
            
< asp:TextBox  ID ="TextBox1"  runat ="server" ></ asp:TextBox >< br  />
            密码:
            
< asp:TextBox  ID ="TextBox2"  runat ="server"  TextMode ="Password" ></ asp:TextBox >< br  />
            收件人:
            
< asp:TextBox  ID ="TextBox3"  runat ="server" ></ asp:TextBox >< br  />
            主题:
            
< asp:TextBox  ID ="TextBox4"  runat ="server" ></ asp:TextBox >< br  />
            内容:
            
< asp:TextBox  ID ="TextBox5"  runat ="server"  TextMode ="MultiLine" ></ asp:TextBox >< br  />
            附件:
            
< asp:FileUpload  ID ="FileUpload1"  runat ="server"   />< br  />
            
< asp:Button  ID ="Button1"  runat ="server"  Text ="发送"   OnClick ="Button1_Click" />
        
</ div >
    
</ form >
</ body >

后台代码

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Net;
using  System.Net.Mail;
public  partial  class  sendemail : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
string s_from = TextBox1.Text.Trim().ToString();
        
string pwd = TextBox2.Text.Trim().ToString();
        
string s_to = TextBox3.Text.Trim().ToString();

        
string s_body = TextBox5.Text.Trim().ToString();
        
//s_body = s_body.Replace("\r\n", "<br/>");
        
//s_body = s_body.Replace("\n", "<br/>");
        
//s_body = s_body.Replace(" ", "&nbsp");
        s_body = Server.HtmlEncode(s_body);
        
int i = s_from.IndexOf("@");
        
string username = s_from.Substring(0, i);

        
//构建MailMessage对象
        MailAddress from = new MailAddress(s_from);
        MailAddress to 
= new MailAddress(s_to);
        MailMessage mailobj 
= new MailMessage(from, to);
        
//完善MailMessage对象
        mailobj.Subject = TextBox4.Text.Trim().ToString();
        mailobj.Body 
= s_body;
        mailobj.IsBodyHtml 
= true;
        mailobj.BodyEncoding 
= System.Text.Encoding.GetEncoding("GB2312");
        mailobj.Priority 
= MailPriority.High;
        
if (FileUpload1.PostedFile.ContentLength > 0)
            mailobj.Attachments.Add(
new Attachment(FileUpload1.PostedFile.FileName));
        
//构建SmtpClient对象
        SmtpClient smtp = new SmtpClient("smtp.163.com");// smtp.Host = "smtp.163.com";
        smtp.UseDefaultCredentials = false;
        smtp.Credentials 
= new NetworkCredential(username, pwd);
        smtp.DeliveryMethod 
= SmtpDeliveryMethod.Network;

        
try
        
{
            smtp.Send(mailobj);
            Response.Write(
"ok!");
        }

        
catch
        
{
            Response.Write(
"sorry!");
        }

    }

}

你可能感兴趣的:(邮件)