asp.net发送邮件实现方法
本文实例讲述了asp.net发送邮件的实现方法。分享给大家供大家参考。具体实现方法如下:
mailmessage mailmsg = new mailmessage();
//设置收件人的邮件地址
mailmsg.to = " ";
//设置发送者的邮件地址
mailmsg.from = " ";
//设置邮件主题
mailmsg.subject = "测试 ";
//设置邮件内容
mailmsg.body = "内容 ";
mailmsg.bodyformat = mailformat.text;
mailmsg.priority = mailpriority.normal;
try
{
//设置发送邮件服务器
smtpmail.smtpserver = "localhost";
//发送邮件
smtpmail.send(mailmsg);
}
catch
{
}
//一款完整发送邮件代码
mailobj _mail = new mailobj();
_mail.sendmail("", "测试", "<b>内容</b>");
_mail.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 = ""; //发送方邮件地址
}
/// <summary>
/// 发送邮件购造函数
/// </summary>
/// <param name="strhost">stmp服务器地址:smtp.163.com</param>
/// <param name="straccount">smtp服务帐号:liugongxun</param>
/// <param name="strpwd">smtp服务密码:123456</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
}
}
希望本文所述对大家的asp.net程序设计有所帮助。
上一篇: C#实现word文件下载的代码