.NET发送邮件

1、使用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);
            }ide

}orm

2、使用System.Net.Mail 命名空间blog

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

using System.Net.Mail;string

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);it

        try
            {
                client.Send(msg);io

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

}cli

相关文章
相关标签/搜索