vs2003 和 vs2005 下的发邮件代码

vs2003下的代码:

using System;



using System.Collections;



using System.ComponentModel;



using System.Data;



using System.Drawing;



using System.Web;



using System.Web.Util;



using System.Web.Mail;



using System.Web.SessionState;



using System.Web.UI;



using System.Web.UI.WebControls;



using System.Web.UI.HtmlControls;







namespace LotteryInfo



{



	/// <summary>



	/// email 的摘要说明。



	/// </summary>



	public class email : System.Web.UI.Page



	{



		protected System.Web.UI.WebControls.TextBox TextBox1;



		protected System.Web.UI.WebControls.TextBox TextBox2;



		protected System.Web.UI.WebControls.TextBox TextBox3;



		protected System.Web.UI.WebControls.TextBox TextBox5;



		protected System.Web.UI.HtmlControls.HtmlInputFile attachment;



		protected System.Web.UI.WebControls.Label Label1;



		protected System.Web.UI.WebControls.TextBox TextBox4;



		protected System.Web.UI.WebControls.Button Button1;



	



		private void Page_Load(object sender, System.EventArgs e)



		{



			// 在此处放置用户代码以初始化页面



		}







		#region Web 窗体设计器生成的代码



		override protected void OnInit(EventArgs e)



		{



			//



			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。



			//



			InitializeComponent();



			base.OnInit(e);



		}



		



		/// <summary>



		/// 设计器支持所需的方法 - 不要使用代码编辑器修改



		/// 此方法的内容。



		/// </summary>



		private void InitializeComponent()



		{    



			this.Button1.Click += new System.EventHandler(this.Button1_Click);



			this.Load += new System.EventHandler(this.Page_Load);







		}



		#endregion







		private void Button1_Click(object sender, System.EventArgs e)



		{



			MailMessage mailobj=new MailMessage();



			mailobj.From=TextBox1.Text.Trim();



			mailobj.To=TextBox2.Text.Trim();



			mailobj.Subject=TextBox3.Text.Trim();



			



			string s_body=TextBox5.Text.ToString();



	



			s_body=s_body.Replace("\r\n","<br/>");



			s_body=s_body.Replace("\n","<br/>");



			s_body=s_body.Replace(" ","&nbsp");



			mailobj.Body=s_body;



			mailobj.BodyFormat=MailFormat.Html;



			mailobj.Priority=MailPriority.High;



		    int i=TextBox1.Text .Trim().ToString().IndexOf("@");



			string username=TextBox1.Text.Trim().Substring(0,i);



			string pwd=TextBox4.Text .Trim();







		   // mailobj.BodyEncoding=Mail



			



			



			if (attachment.PostedFile.ContentLength>0)



			{



				mailobj.Attachments.Add(new MailAttachment(attachment.PostedFile.FileName));



			}







			try



			{



			mailobj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");  



			mailobj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", username);   



			mailobj.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",pwd); 



				//mailobj.Fields.Add()



			



			



			SmtpMail.SmtpServer="smtp.163.com";



		



			SmtpMail.Send(mailobj);



			Label1.Text="邮件发送成功!";



				TextBox5.Text="";







		



			Response.Write("ok;");



		   }



		catch



		



			{



			Response.Write("<script language ='javascript'>alert('sorry!');<script>");



		   }



			



		}



	}



}



vs2005  下的

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 s_to = TextBox3.Text.Trim().ToString();
        string pwd=TextBox2.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");

        int i = s_from.IndexOf("@");
        string username = s_from.Substring(0,i);


        MailAddress from = new MailAddress(s_from);
        MailAddress to = new MailAddress(s_to);
        MailMessage mailobj = new MailMessage(from ,to);
        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 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!");
        }
       
        
         }
}

因为用的邮箱都是163的,所以就设置了smtp的地址是163的地址,如果用别的邮箱,只要smtp的地址改下就可以了.

你可能感兴趣的:(2003)