private void Page_Load(object sender, System.EventArgs e)
{
string sReturn = string.Empty;
MailMessage mailMsg = new MailMessage();
mailMsg.BodyFormat = MailFormat.Html;
mailMsg.To = "****@163.com";
mailMsg.From = "***@163.com";
mailMsg.Subject = "email.Subject";
mailMsg.Body = "email.Body";
// mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
// mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
// mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码");
//
SmtpMail.SmtpServer ="smtp.126.com";//smpt服务器
try
{
SmtpMail.Send(mailMsg);
this.Label1.Text = "发送成功";
}
catch (Exception err)
{
this.Label2.Text = "<font color=red>发送失败" + err.Message.ToString() + "</font>";
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace Logic
{
public class Mail
{
public bool ServiceSendMail(string strTo, string strSubject, string strBody, string strEncoding)
{
return common(strTo, "", strSubject, strBody, strEncoding);
}
/// <summary>
/// 邮件发送(Service邮箱),默认编码为GB2312
/// </summary>
/// <param name="strTo"></param>
/// <param name="strCc"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <param name="strEncoding">编码,如果为空,默认为GB2312</param>
/// <returns></returns>
public bool ServiceSendMail(string strTo, string strCc, string strSubject, string strBody, string strEncoding)
{
return common(strTo, strCc, strSubject, strBody, strEncoding);
}
/// <summary>
/// 邮件发送默认编码为GB2312
/// </summary>
/// <param name="strTo"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <returns></returns>
public bool ServiceSendMail(string strTo, string strSubject, string strBody)
{
return common(strTo, "", strSubject, strBody, "");
}
private static bool common(string strTo, string strCc, string strSubject, string strBody, string strEncoding)
{
bool bState = false;
try
{
//编码暂硬性规定为GB2312
Encoding encoding = Encoding.GetEncoding(936);
MailMessage Message = new MailMessage(
new MailAddress("hanjunhui127@sina.com", "hanjunhui127", encoding),
new MailAddress(strTo));
Message.SubjectEncoding = encoding;
Message.Subject = strSubject;
Message.BodyEncoding = encoding;
Message.Body = strBody;
if (strCc != "")
{
Message.CC.Add(new MailAddress(strCc));
}
SmtpClient smtpClient = new SmtpClient("smtp.sina.com");
smtpClient.Credentials = new NetworkCredential("hanjunhui127","1984127");
smtpClient.Timeout = 999999;
smtpClient.Send(Message);
bState = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return bState;
}
}
}
你把上面的代码编译成dll,在web程序里面引用那个dll
在aspx.cs里面你可以这样写
using System;
using System.Data;
using System.Configuration;
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 Logic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Logic.Mail m = new Mail();
m.ServiceSendMail("hjh127@163.com", "你好", "我好啊```````````````");
Response.Write("成功!");
}
}
{
string sReturn = string.Empty;
MailMessage mailMsg = new MailMessage();
mailMsg.BodyFormat = MailFormat.Html;
mailMsg.To = "****@163.com";
mailMsg.From = "***@163.com";
mailMsg.Subject = "email.Subject";
mailMsg.Body = "email.Body";
// mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
// mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "用户名");
// mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "密码");
//
SmtpMail.SmtpServer ="smtp.126.com";//smpt服务器
try
{
SmtpMail.Send(mailMsg);
this.Label1.Text = "发送成功";
}
catch (Exception err)
{
this.Label2.Text = "<font color=red>发送失败" + err.Message.ToString() + "</font>";
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
namespace Logic
{
public class Mail
{
public bool ServiceSendMail(string strTo, string strSubject, string strBody, string strEncoding)
{
return common(strTo, "", strSubject, strBody, strEncoding);
}
/// <summary>
/// 邮件发送(Service邮箱),默认编码为GB2312
/// </summary>
/// <param name="strTo"></param>
/// <param name="strCc"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <param name="strEncoding">编码,如果为空,默认为GB2312</param>
/// <returns></returns>
public bool ServiceSendMail(string strTo, string strCc, string strSubject, string strBody, string strEncoding)
{
return common(strTo, strCc, strSubject, strBody, strEncoding);
}
/// <summary>
/// 邮件发送默认编码为GB2312
/// </summary>
/// <param name="strTo"></param>
/// <param name="strSubject"></param>
/// <param name="strBody"></param>
/// <returns></returns>
public bool ServiceSendMail(string strTo, string strSubject, string strBody)
{
return common(strTo, "", strSubject, strBody, "");
}
private static bool common(string strTo, string strCc, string strSubject, string strBody, string strEncoding)
{
bool bState = false;
try
{
//编码暂硬性规定为GB2312
Encoding encoding = Encoding.GetEncoding(936);
MailMessage Message = new MailMessage(
new MailAddress("hanjunhui127@sina.com", "hanjunhui127", encoding),
new MailAddress(strTo));
Message.SubjectEncoding = encoding;
Message.Subject = strSubject;
Message.BodyEncoding = encoding;
Message.Body = strBody;
if (strCc != "")
{
Message.CC.Add(new MailAddress(strCc));
}
SmtpClient smtpClient = new SmtpClient("smtp.sina.com");
smtpClient.Credentials = new NetworkCredential("hanjunhui127","1984127");
smtpClient.Timeout = 999999;
smtpClient.Send(Message);
bState = true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return bState;
}
}
}
你把上面的代码编译成dll,在web程序里面引用那个dll
在aspx.cs里面你可以这样写
using System;
using System.Data;
using System.Configuration;
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 Logic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Logic.Mail m = new Mail();
m.ServiceSendMail("hjh127@163.com", "你好", "我好啊```````````````");
Response.Write("成功!");
}
}