如何利用System.Net.Mail类发送EMAIL

废话不多说,直接上代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace MailSender
{
    class Program
    {
        static string strHost = string.Empty;
        static string strAccount = string.Empty;
        static string strPwd = string.Empty;
        static string strFrom = string.Empty;

        static void Main(string[] args)
        {
            strHost = "smtp.qq.com";   //STMP服务器地址
            strAccount = "xxx@126.com";       //SMTP服务帐号
            strPwd = "123456";       //SMTP服务密码
            strFrom = "xxx@126.com";  //发送方邮件地址

            Console.WriteLine(sendMail("cnkker@qq.com", "这是一封测试邮件", "这是一封测试邮件的正文内容") ? "Success" : "Unsuccess");

            Console.ReadLine();
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">接收方邮件地址</param>
        /// <param name="title">邮件标题</param>
        /// <param name="content">邮件正文内容</param>
        /// <returns></returns>
        /// <author>Jailu</author>
        /// <date>2007-04-10</date>
        static 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(Exception ex)
            {
                
                return false;
            }
        }
    }
}


 

你懂的!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值