一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net三种发送邮件代码(stmp,无组件邮件发送)

时间:2010-07-12 编辑:简简单单 来源:一聚教程网

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("");
return true;
}
catch
{
response.write("");
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 = "system.net.mail";
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
{
    ///


    ///邮件发送
    ///

    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";  //发送方邮件地址
        }

        ///


        /// 发送邮件购造函数
        ///

        /// stmp服务器地址:smtp.163.com
        /// smtp服务帐号:liugongxun
        /// smtp服务密码:www.111com.net
        /// 发送方邮件地址:liugongxun@163.com
        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", "测试111com.net", "内容");
        _mail.dispose();

热门栏目