http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 26154|回复: 4

[Asp.Net] c#Asp.Net简单实现发送电子邮件的功用

[复制链接]
发表于 2012-7-9 10:55:30 | 显示全部楼层 |阅读模式
什么不用多说就是一个方法大家直接看下代码吧
[C#] 纯文本查看 复制代码
 /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="MailFromAddress">发件人地址</param>
        /// <param name="MailFromUserName">用户名</param>
        /// <param name="MailFormPwd">密码</param>
        /// <param name="MailTitle">邮件标题</param>
        /// <param name="MailBody">邮件内容</param>
        /// <param name="MailTo">收件人地址</param>
        /// <returns>发送成功返回True否则返回False</returns>
        public static  Boolean SendMail(string MailFromAddress, string MailFormPwd, string MailTitle, string MailBody, string MailTo)
        {
            try
            {
                MailMessage objMail = new MailMessage();
                //发件人地址
                objMail.From = new MailAddress(MailFromAddress);
                //邮件标题
                objMail.Subject = MailTitle;
                //邮件标题编码 
                objMail.SubjectEncoding = System.Text.Encoding.UTF8;
                //邮件内容
                objMail.Body = MailBody;
                //邮件内容编码 
                objMail.BodyEncoding = System.Text.Encoding.UTF8;
                //收件人地址在这里可以加多个
                objMail.To.Add(MailTo);
                //用SMTP发送邮件的方式
                SmtpClient client = new SmtpClient();
                //用户名和密码
                client.Credentials = new System.Net.NetworkCredential(MailFromAddress, MailFormPwd);
                string StrMail = "";
                for (int i = 0; i < MailFromAddress.Length; i++)
                {
                    if (MailFromAddress.Substring(i, 1).ToString().Trim() == "@")
                    {
                        StrMail = MailFromAddress.Substring(i + 1, MailFromAddress.Length - i - 1);
                    }
                }
                //服务器名
                client.Host = "smtp." + StrMail.ToString().Trim();
                //发送
                client.Send(objMail);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

使用的方法也很简单
[C#] 纯文本查看 复制代码
       MailMessage objMail = new MailMessage();

            //发件人地址
            objMail.From = new MailAddress(txtForm.Text.ToString().Trim());
            //邮件标题
            objMail.Subject = txttitle.Text.ToString().Trim();
            //邮件标题编码 
            objMail.SubjectEncoding = System.Text.Encoding.UTF8;
            //邮件内容
            objMail.Body = txtBody.Text.ToString().Trim();
            //邮件内容编码 
            objMail.BodyEncoding = System.Text.Encoding.UTF8;
            //收件人地址在这里可以加多个
            objMail.To.Add(txtTO.Text.ToString().Trim());
            //用SMTP发送邮件的方式
            SmtpClient client = new SmtpClient();
            //用户名和密码
            client.Credentials = new System.Net.NetworkCredential(txtForm.Text.ToString().Trim(), txtPwd.Text.ToString().Trim());
            //服务器名
            client.Host = "SMTP.163.com";
            //发送
            client.Send(objMail);

里面的MailMessage 大家自己写一下吧就是一个简单的实体类没什么特别之处
在这里呢多给大家提供两个发送邮件的类方法吧,大家可以做为参考
[C#] 纯文本查看 复制代码
using System;
using System.Web.Mail;
using System.Collections;

/**/
/// <summary>
/// 支持批量(发送、抄送、密送)
/// 支持多语言、带多个附件
/// 支持smtp发送邮件服务器验证
/// </summary>
public class DotNetSendMail
{
    public DotNetSendMail()
    { }

    /**/
    /// <summary>
    /// 邮件发送结果。如果发送过程出现错误,该值为捕获到的异常提示;否则,该值为“OK”。
    /// </summary>
    public string SendMailResult
    {
        get
        {
            return this.m_SendMailResult;
        }
        set
        {
            this.m_SendMailResult = value;
        }
    }
    private string m_SendMailResult;

    /**/
    /// <summary>
    /// 抄送地址,多个以逗号隔开
    /// </summary>
    public string Cc
    {
        set
        {
            this.m_Cc = value;
        }
    }
    private string m_Cc;

    /**/
    /// <summary>
    /// 密送地址,多个以逗号隔开
    /// </summary>
    public string Bcc
    {
        set
        {
            this.m_Bcc = value;
        }
    }
    private string m_Bcc;

    /**/
    /// <summary>
    /// 邮件格式。可以赋值为“1”或“2”,“1”表示Html格式,“2”表示Text格式。
    /// </summary>
    public int BodyFormat
    {
        set
        {
            this.m_BodyFormat = value;
        }
    }
    private int m_BodyFormat = 1;

    /**/
    /// <summary>
    /// 邮件格式。可以赋值为“1”、“2”或“3”。
    ///“1”表示不紧急邮件,“2”表示普通邮件, “3”表示紧急邮件
    /// </summary>
    public int MailPriority
    {
        set
        {
            this.m_MailPriority = value;
        }
    }
    private int m_MailPriority = 2;

    /**/
    /// <summary>
    /// 附件地址列表,用该属性Add方法加入多个附件,附件地址必须是绝对路径
    /// </summary>
    public ArrayList AttachFiles
    {
        get
        {
            return this.m_AttachFiles;
        }
        set
        {
            this.m_AttachFiles = value;
        }
    }
    ArrayList m_AttachFiles = new ArrayList();

    /**/
    /// <summary>
    /// 发送邮件方法,方法中几个参数是发邮件时所必须的
    /// </summary>
    /// <param name="p_From">发件人</param>
    /// <param name="p_To">邮件人,多个收件人逗号隔开</param>
    /// <param name="p_Subject">邮件标题</param>
    /// <param name="p_Body">正文</param>
    /// <param name="p_SmtpServer">发送邮件服务器</param>
    /// <param name="p_SmtpUsername">发件服务器登录名</param>
    /// <param name="p_SmtpPassword">发件服务器登录密码</param>
    /// <returns></returns>
    public bool SendMail(string p_From, string p_To, string p_Subject, string p_Body, string p_SmtpServer, string p_SmtpUsername, string p_SmtpPassword)
    {
        try
        {
            System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
            mail.From = p_From;
            mail.To = p_To;
            mail.Subject = p_Subject;
            mail.Body = p_Body;
            SmtpMail.SmtpServer = p_SmtpServer;
            //邮件服务器验证,需要输入您在邮件发送服务器上注册的邮箱用户名和密码
            if (p_SmtpUsername != "")
            {
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", p_SmtpUsername);
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", p_SmtpPassword);
            }
            //以上是发邮件时所必需的,下面是额外的一些功能设置
            if (this.m_Cc != "")
            {
                mail.Cc = this.m_Cc;
            }
            if (this.m_Bcc != "")
            {
                mail.Bcc = this.m_Bcc;
            }
            switch (this.m_BodyFormat)
            {
                case 1:
                    mail.BodyFormat = MailFormat.Html;
                    break;
                case 2:
                    mail.BodyFormat = MailFormat.Text;
                    break;
                default:
                    break;
            }
            switch (this.m_MailPriority)
            {
                case 1:
                    mail.Priority = System.Web.Mail.MailPriority.Low;
                    break;
                case 2:
                    mail.Priority = System.Web.Mail.MailPriority.Normal;
                    break;
                case 3:
                    mail.Priority = System.Web.Mail.MailPriority.High;
                    break;
                default:
                    break;
            }
            if (this.m_AttachFiles.Count > 0)
            {
                foreach (string file in m_AttachFiles)
                {
                    if (file.Trim() != "")
                    {
                        mail.Attachments.Add(new MailAttachment(file.Trim()));
                    }
                }
            }
            SmtpMail.Send(mail);
            this.m_SendMailResult = "OK";
            return true;
        }
        catch (Exception ex)
        {
            this.m_SendMailResult = ex.ToString();
            return false;
        }
    }
}

下面还有一个我个人最喜欢这个
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace BLL.Function
{
    public class EmailServices
    {
        ///<summary>
        /// 发送邮件
        ///</summary>
        ///<param name="sendEmailAddress">发件人邮箱</param>
        ///<param name="sendEmailPwd">发件人密码</param>
        ///<param name="msgToEmail">收件人邮箱地址</param>
        ///<param name="title">邮件标题</param>
        ///<param name="content">邮件内容</param>
        ///<returns>0:失败。1:成功!</returns>
        public static int SendEmail(string sendEmailAddress, string sendEmailPwd, string[] msgToEmail, string title, string content)
        {
            //发件者邮箱地址
            string fjrtxt = sendEmailAddress;
            //发件者邮箱密码
            string mmtxt = sendEmailPwd;
            ////收件人收箱地址
            //string sjrtxt = msgToEmail;
            //主题
            string zttxt = title;
            //附件
            //string fjtxt = fj.Text;
            //内容
            string nrtxt = content;
            string[] fasong = fjrtxt.Split('@');
            string[] fs = fasong[1].Split('.');
            //发送
            //设置邮件协议
            SmtpClient client = new SmtpClient("smtp." + fs[0].ToString().Trim() + ".com");
            client.UseDefaultCredentials = false;
            //通过网络发送到Smtp服务器
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //通过用户名和密码 认证
            client.Credentials = new NetworkCredential(fasong[0].ToString(), mmtxt);
            //发件人和收件人的邮箱地址
            MailMessage mmsg = new MailMessage();
            mmsg.From = new MailAddress(fjrtxt);
            for (int i = 0; i < msgToEmail.Length; i++)
            {
                mmsg.To.Add(new MailAddress(msgToEmail));
            }
            //邮件主题
            mmsg.Subject = zttxt;
            //主题编码
            mmsg.SubjectEncoding = Encoding.UTF8;
            //邮件正文
            mmsg.Body = nrtxt;
            //正文编码
            mmsg.BodyEncoding = Encoding.UTF8;
            //设置为HTML格式
            mmsg.IsBodyHtml = true;
            //优先级
            mmsg.Priority = MailPriority.High;
            //if (fj.Text.Trim() != "")
            //{
            ////增加附件
            //    mmsg.Attachments.Add(new Attachment(fj.Text));
            //}
            try
            {
                client.Send(mmsg);
                return 1;
            }
            catch
            {
                return 0;
            }

        }
    }
}


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-5-26 16:13:02 | 显示全部楼层
嘿;你好请问,这个是可以跨互联网发送的吗?
发表于 2014-7-17 22:37:09 | 显示全部楼层
先学习学习
发表于 2014-7-18 00:07:58 | 显示全部楼层
学习ing
回复

使用道具 举报

发表于 2014-9-5 23:28:05 | 显示全部楼层
强烈支持楼主ing……不错不错
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-18 04:39

© 2014-2021

快速回复 返回顶部 返回列表