一 SmtpMail类发送邮件简单实例
核心代码:
using System.Web.Mail;
private void Button1_Click(object sender, System.EventArgs e)
{//邮件发送
if(Page.IsValid==true)
{
string to2,from2,topic2,content2;
to2=to.Text;
from2=from.Text;
topic2=topic.Text;
content2=content.Text;
try
{
//SmtpMail.SmtpServer="smtp.mail.com";//设置SMTP服务器
SmtpMail.Send(from2,to2,topic2,content2);
Response.Write("发送成功!");
}
catch(Exception ex)
{
Response.Write("出现异常,发送失败!");
Response.Write(ex.ToString());
}
}
}
二 SmtpMail类发送邮件高级实例
核心代码:
using System.Web.Mail;
private void Button1_Click(object sender, System.EventArgs e)
{//发送邮件程式
if(Page.IsValid==true){
string to2,from2,cc2,bcc2,topic2,bodyformail2,prority2,content2;
to2=to.Text;
from2=from.Text;
cc2=cc.Text;
bcc2=bcc.Text;
topic2=topic.Text;
bodyformail2=bodyformail.SelectedItem.Value;
prority2=prority.SelectedItem.Value;
content2=content.Text;
MailMessage mm=new MailMessage();
mm.From=from2.ToString();
mm.To=to2.ToString();
mm.Cc=cc2.ToString();
mm.Bcc=bcc2.ToString();
mm.Subject=topic2.ToString();
mm.Priority=(MailPriority)prority.SelectedIndex;
//mm.BodyEncoding="Gb2312"//邮件的编码方式
mm.BodyFormat=(MailFormat)bodyformail.SelectedIndex;
mm.Body=content2.ToString();
try{
SmtpMail.Send(mm);
Response.Write("发送成功!");
}
catch(Exception ex){
Response.Write(ex.ToString());
}
}
}