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 System.Web.Mail;
namespace MymailSys
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public partial class WebMail : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlForm Form2;
protected 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()
{
}
#endregion
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
System.Net.WebProxy proxy = new System.Net.WebProxy("192.168.0.1:80");
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Net.GlobalProxySelection.Select = proxy;
MailMessage m = new MailMessage();
m.From = tbFrom.Text;
m.To = tbTo.Text;
m.Subject = tbSubject.Text;
m.Body = tbBody.Text;
//优先级
switch(ddlp.SelectedIndex)
{
case 0:
m.Priority = MailPriority.High;
break;
case 1:
m.Priority = MailPriority.Low;
break;
default:
m.Priority = MailPriority.Normal;
break;
}
//格式
if(ddlp.SelectedIndex==0)
m.BodyFormat = MailFormat.Text;
else
m.BodyFormat = MailFormat.Html;
//以下设置服务器
if(tbServer.Text!="")
{
//以下代码适用于Framework1.1以上版本。
//SmtpMail.SmtpServer = tbServer.Text;
// m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",
// "1"); //basic authentication
// m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",
// tbUserName.Text); //set your username here
// m.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",
// tbPass.Text); //set your password here
}
//以下处理附件
string strFileName = FileSelect.PostedFile.FileName;
if(strFileName!="")
m.Attachments.Add(new MailAttachment(strFileName));
SmtpMail.Send(m);
}
}
}
(from msdn 邵老师的讲座)