using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using jmail;
namespace CommonFunction
{
	/// <summary>
	/// NewMail 的摘要说明。
	/// </summary>
	public class NewMail : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox TxtSmtServer;
		protected System.Web.UI.WebControls.TextBox TxtSmtpUser;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
		protected System.Web.UI.WebControls.TextBox TxtSmtpPwd;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;
		protected System.Web.UI.WebControls.TextBox TxtSmtpPort;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator9;
		protected System.Web.UI.WebControls.TextBox TxtEmail;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
		protected System.Web.UI.WebControls.TextBox TxtName;
		protected System.Web.UI.WebControls.TextBox txtContent;
		protected System.Web.UI.WebControls.TextBox txtSubject;
		protected System.Web.UI.HtmlControls.HtmlInputFile upFile;
		protected System.Web.UI.WebControls.RadioButtonList radioCheck;
		protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
		protected System.Web.UI.WebControls.Button btnSend;
		protected System.Web.UI.WebControls.TextBox txtReciver;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator10;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnSend_Click(object sender, System.EventArgs e)
		{
			jmail.MessageClass oJmailMessage = new jmail.MessageClass();//建立发邮件类

			oJmailMessage.Charset = "GB2312";// 字符集
			oJmailMessage.Encoding = "BASE64";//附件的编码格式
			oJmailMessage.ContentType = "text/html";
			oJmailMessage.ISOEncodeHeaders = false;//是否将信头编码成iso-8859-1字符集
			oJmailMessage.Priority = Convert.ToByte(1);// 优先级
		
			oJmailMessage.From = TxtEmail.Text.Trim();//发送人邮件地址
			oJmailMessage.FromName	= TxtName.Text.Trim();//发送人姓名
			oJmailMessage.Subject	= txtSubject.Text.Trim();// 邮件主题
			oJmailMessage.MailServerUserName = TxtSmtpUser.Text.Trim();//身份验证的用户名
			oJmailMessage.MailServerPassWord = TxtSmtpPwd.Text.Trim();//用户密码

			//添加一个收件人，抄送人和密送人的添加和该方法是一样的，只是分别使用AddRecipientCC和RecipientBCC两个属性
			//要是需要添加多个收件人，则重复下面的语句即可。添加多个抄送和密送人的方法一样
			oJmailMessage.AddRecipient(txtReciver.Text.Trim(),"","");
			if("" != upFile.PostedFile.FileName)
			{
				string attpath = upFile.PostedFile.FileName;
				oJmailMessage.AddAttachment(@attpath,true,attpath.Substring(attpath.LastIndexOf(".")+1,3));//添加附件
			}
			oJmailMessage.Body = txtContent.Text.Trim();//邮件内容

			if(oJmailMessage.Send(TxtSmtServer.Text.Trim(),false))
			{
				Response.Write("发送成功！");
			}
			else
			{
				Response.Write("发送失败，请检查邮件服务器的设置！");
			}

			oJmailMessage = null;
		}

		
	}
}
