.NET发送邮件

一、使用System.Web.Mail命名空间发邮件

      在.Net Framework 1.x 我们需要使用 System.Web.Mail 命名空间下的类来进行发送邮件,但是功能比较弱,比如你的邮件服务器需要验证才能发送邮件。当我用VS2008写如下代码时,会出现“System.Web.Mail.MailMessage ”过时的提示。
private void SendMail()
{

            System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
            mailMsg.From = "**@**"; //我的邮箱地址
            mailMsg.To = “**@**”; //对方的邮箱地址
            mailMsg.Subject = "主题";
            mailMsg.BodyFormat = System.Web.Mail.MailFormat.Text;
            mailMsg.Body = "内容“;
            try
            {
                System.Web.Mail.SmtpMail.Send(mailMsg);
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }

}

 

二、使用System.Net.Mail 命名空间

.Net Framework 2.0 下,在 System.Net.Mail 命名空间中提供了对邮件操作的支持,他的功能更强大。比如你的邮件服务器需要验证才能发送邮件。

using System.Net.Mail;

private void SendMail(string host)
{

        MailAddress from = new MailAddress("**@**"); //我的邮箱地址
        MailAddress to = new MailAddress("**@**");//对方的邮箱地址
        MailMessage msg = new MailMessage(from, to);
        msg.Subject = "主题";
        msg.Body = "内容";
        SmtpClient client = new SmtpClient(host);

        try
            {
                client.Send(msg);

            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值