邮件发送

 protected void Send_Click(object sender, EventArgs e)
        {
            Email email = new Email();
            email.mailFrom = "***@sina.com";
            email.mailPwd = "***";
            email.mailSubject = "邮件主题";
            email.mailBody = "邮件内容";
            email.isbodyHtml = true;    //是否是HTML
            email.host = "smtp.sina.com";//如果是QQ邮箱则:smtp:qq.com,依次类推
            email.mailToArray = new string[] { "****@qq.com"};//接收者邮件集合
            //email.mailCcArray = new string[] { "***@qq.com" };//抄送者邮件集合
            if (email.Send())
            {
                Response.Write("");//发送成功则提示返回当前页面;

            }
            else
            {
                Response.Write("");
            }
        }

  

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

namespace WebApplication1
{
    public class Email
    {
        /// 
        /// 发送者
        /// 
        public string mailFrom { get; set; }

        /// 
        /// 收件人
        /// 
        public string[] mailToArray { get; set; }

        /// 
        /// 抄送
        /// 
        public string[] mailCcArray { get; set; }

        /// 
        /// 标题
        /// 
        public string mailSubject { get; set; }

        /// 
        /// 正文
        /// 
        public string mailBody { get; set; }

        /// 
        /// 发件人密码
        /// 
        public string mailPwd { get; set; }

        /// 
        /// SMTP邮件服务器
        /// 
        public string host { get; set; }

        /// 
        /// 正文是否是html格式
        /// 
        public bool isbodyHtml { get; set; }

        /// 
        /// 附件
        /// 
        public string[] attachmentsPath { get; set; }

        public bool Send()
        {
            //使用指定的邮件地址初始化MailAddress实例
            MailAddress maddr = new MailAddress(mailFrom);
            //初始化MailMessage实例
            MailMessage myMail = new MailMessage();


            //向收件人地址集合添加邮件地址
            if (mailToArray != null)
            {
                for (int i = 0; i < mailToArray.Length; i++)
                {
                    myMail.To.Add(mailToArray[i].ToString());
                }
            }

            //向抄送收件人地址集合添加邮件地址
            if (mailCcArray != null)
            {
                for (int i = 0; i < mailCcArray.Length; i++)
                {
                    myMail.CC.Add(mailCcArray[i].ToString());
                }
            }
            //发件人地址
            myMail.From = maddr;

            //电子邮件的标题
            myMail.Subject = mailSubject;

            //电子邮件的主题内容使用的编码
            myMail.SubjectEncoding = Encoding.UTF8;

            //电子邮件正文
            myMail.Body = mailBody;

            //电子邮件正文的编码
            myMail.BodyEncoding = Encoding.Default;

            myMail.Priority = MailPriority.High;

            myMail.IsBodyHtml = isbodyHtml;

            //在有附件的情况下添加附件
            try
            {
                if (attachmentsPath != null && attachmentsPath.Length > 0)
                {
                    Attachment attachFile = null;
                    foreach (string path in attachmentsPath)
                    {
                        attachFile = new Attachment(path);
                        myMail.Attachments.Add(attachFile);
                    }
                }
            }
            catch (Exception err)
            {
                throw new Exception("在添加附件时有错误:" + err);
            }

            SmtpClient smtp = new SmtpClient();
            //指定发件人的邮件地址和密码以验证发件人身份
            smtp.EnableSsl = true;

            smtp.UseDefaultCredentials = false;

            //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式        


            smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);


            //设置SMTP邮件服务器
            smtp.Host = host;

            try
            {
                //将邮件发送到SMTP邮件服务器
                smtp.Send(myMail);
                return true;

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                return false;
            }

        }
    }
}

  

转载于:https://www.cnblogs.com/lushixiong/p/6693662.html

你可能感兴趣的:(邮件发送)