asp教程.net三种发送邮件代码(stmp,无组件邮件发送)
public bool sendmails()
{
smtpclient _smtpclient = new smtpclient();
_smtpclient.deliverymethod = smtpdeliverymethod.network;//指定电子邮件发送方式
_smtpclient.host = "ip地址";//指定smtp服务器
_smtpclient.credentials = new system.net.networkcredential(_straccount, _strpwd);//用户名和密码mailmessage _mailmessage = new mailmessage("molizuqiuba@163.com", "11111111111@qq.com");
_mailmessage.subject = "邮件测试";//主题
_mailmessage.body = "邮件发送成功...........";//内容
_mailmessage.bodyencoding = system.text.encoding.utf8;//正文编码
_mailmessage.isbodyhtml = true;//设置为html格式
_mailmessage.priority = mailpriority.high;//优先级
try
{
_smtpclient.send(_mailmessage);
response.write("<script>alert('邮件发送成功.............');window.history(-1)</script>");
return true;
}
catch
{
response.write("<script>alert('邮件发送shibai.............');window.history(-1)</script>");
return false;
}
}
%>
需要三个类:mailmessage、smtpclient、networkcredential。
mailmessage、smtpclient 的名称空间是:
system.net.mail
networkcredential 的名称空间是:system.net
mailmessage mail = new mailmessage("发送方邮件地址", "接收方邮件地址");
mail.subjectencoding = encoding.utf8;
mail.subject = "邮件标题";
mail.isbodyhtml = true; //是否允许内容为 html 格式
mail.bodyencoding = encoding.utf8;
mail.body = "<strong>system.net.mail</strong>";
mail.attachments.add(new attachment("e:\foo.txt")); //添加一个附件smtpclient smtp = new smtpclient("smtp 服务器地址");
smtp.credentials = new networkcredential("登录名", "密码"); //smtp 验证
smtp.send(mail);mail.attachments.dispose(); //邮件发送完毕,释放对附件的锁定
<%
//看个无组件发送邮件代码using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.collections.generic;
using system.net.mail;
using system.text;namespace ec
{
/// <summary>
///邮件发送
/// </summary>
public class mailobj
{
private string _strhost = string.empty;
private string _straccount = string.empty;
private string _strpwd = string.empty;
private string _strfrom = string.empty;#region 构造与析构函数
public mailobj()
{
_strhost = "smtp.163.com"; //stmp服务器地址
_straccount = "aa"; //smtp服务帐号
_strpwd = "123456"; //smtp服务密码
_strfrom = "aa@163.com"; //发送方邮件地址
}/// <summary>
/// 发送邮件购造函数
/// </summary>
/// <param name="strhost">stmp服务器地址:smtp.163.com</param>
/// <param name="straccount">smtp服务帐号:liugongxun</param>
/// <param name="strpwd">smtp服务密码:www.111cn.net</param>
/// <param name="strfrom">发送方邮件地址:liugongxun@163.com</param>
public mailobj(string strhost, string straccount, string strpwd, string strfrom)
{
_strhost = strhost;
_straccount = straccount;
_strpwd = strpwd;
_strfrom = strfrom;
}~mailobj()
{
dispose();
}public void dispose()
{
gc.suppressfinalize(this);
}
#endregion#region 发送邮件
public bool sendmail(string to, string title, string content)
{
smtpclient _smtpclient = new smtpclient();
_smtpclient.deliverymethod = smtpdeliverymethod.network;//指定电子邮件发送方式
_smtpclient.host = _strhost;//指定smtp服务器
_smtpclient.credentials = new system.net.networkcredential(_straccount, _strpwd);//用户名和密码mailmessage _mailmessage = new mailmessage(_strfrom, to);
_mailmessage.subject = title;//主题
_mailmessage.body = content;//内容
_mailmessage.bodyencoding = system.text.encoding.utf8;//正文编码
_mailmessage.isbodyhtml = true;//设置为html格式
_mailmessage.priority = mailpriority.high;//优先级
try
{
_smtpclient.send(_mailmessage);
return true;
}
catch
{
return false;
}
}
#endregion
}
}
//调用方法
mailobj _mail = new mailobj();
_mail.sendmail("lxx@qq.com", "测试111cn.net", "<b>内容</b>");
_mail.dispose();