大家在自己的项目可能会经常用到给用户发送邮件什么,像一些新年祝福、最新技术信息推送等,也是比较实用的功能,话不多说,源代码贴出来同时自己也作为备忘,直接复制过去用就行。

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

namespace BLL
{
    public class send_mail
    {
        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="toMail">收件方邮件</param>
        /// <param name="ccMail">抄送人</param>
        /// <param name="bccMail">暗送人</param>
        /// <param name="subject">主题</param>
        /// <param name="body">邮件体</param>
        /// <param name="format">发送格式</param>
        /// <param name="serverFileName">附件</param>
        /// <returns>是否发送成功</returns>
        public bool Send_Mail(string toMail, string ccMail, string bccMail, string subject, string body, SendMark format, string serverFileName)
        {
        string fromMail = "moppop@126.com";//发送方邮件
         string fromPassword = "************";//发送方密码
         string host = "smtp.126.com";//smtp服务器
         string fromName = "青岛火凤凰网络科技";//发件人

            try
            {
                MailMessage myMail = new MailMessage();
                myMail.From = new MailAddress(fromMail, fromName);
                myMail.To.Add(new MailAddress(toMail));
                if (!string.IsNullOrEmpty(ccMail))
                {
                    myMail.CC.Add(new MailAddress(ccMail));
                }
                if (!string.IsNullOrEmpty(bccMail))
                {
                    myMail.Bcc.Add(new MailAddress(bccMail));
                }
                myMail.Subject = subject;
                myMail.Body = body;
                if (format == SendMark.Html)
                {
                    myMail.IsBodyHtml = true;
                }
                else
                {
                    myMail.IsBodyHtml = false;
                }

                //附件
                if (!string.IsNullOrEmpty(serverFileName))
                {
                    myMail.Attachments.Add(new Attachment(serverFileName));
                }

                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential(fromMail, fromPassword);
                client.Host = host;
                client.Send(myMail);
                return true;
            }
            catch
            {
                return false;
            }
        }
   
    }

    public enum SendMark
    {
        Html = 1,
        Body = 0
    }

}
 

   具体页面代码中调用格式如下:

       string username ="miaomiao";
            string email = “miaomiao@126.com”;
            send_mail mail = new send_mail();
            string content = @"<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>注册激活</title>
</head>
<body>
<table cellspacing='0' border='0' style='font-size:14px; font-family:宋体,arial,verdana,sans-serif;'>
  <tr>
    <td colspan='2' style='height:30px; font-weight:bold;'>尊敬的用户:</td>
  </tr>
  <tr>
    <td style='width:40px; height:30px;'></td>
    <td style=' font-weight:bold;'>您好![NAME]</td>
  </tr>
  <tr>
    <td style='height:30px;'></td>
    <td>您申请了火凤凰网用户名,请点击下面的按钮进行操作</td>
  </tr>
   <tr>
    <td style='height:30px;'></td>
    <td style='line-height:18px; font-size:12px; padding-top:20px;'>客服中心<br/>[TIME]</td>
  </tr>
  </table>
</body>
</html>
";
            content = content.Replace("[NAME]", username);
            content = content.Replace("[TIME]", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
            bool b = mail.Send_Mail(email, "", "", "注册激活", content, SendMark.Html, "");
            if (b)
            {
                Response.Write("成功");
            }
            else {
                Response.Write("失败");
            }