Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)

平台之大势何人能挡? 带着你的Net飞奔吧!

http://www.cnblogs.com/dunitian/p/4822808.html

 

邮箱系列:https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/3.常用技能/07.Email

 

1.QQ邮箱:

他生成的是:http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=7oKBmoqAmq6fn8CNgYM

后来我把后面加密字符串换成明文的邮箱==》发现一样用 :http://mail.qq.com/cgi-bin/[email protected]

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第1张图片

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第2张图片

代码案例:

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第3张图片





    
    


    官方生成代码:(其实就是把你的邮件加了下密)



逆天通用土方法:(邮件可以任意替换)

邮我

效果:

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第4张图片

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第5张图片

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第6张图片

订阅系列可以参考:http://www.cnblogs.com/dunitian/p/4554084.html

——————————————————————————————————————————————————————————————————————————————————————————————

邮件发送的前提设置:

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第7张图片

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第8张图片

QQ邮箱设置

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第9张图片

授权码生成:

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第10张图片

 

2.邮件案例:

简单熟悉邮件系列:点我就看基础案例

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第11张图片

Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)_第12张图片

代码示例:

        #region 附件路径
        /// 
        /// 附件路径
        /// 
        public static List filePathList = new List();
        #endregion

        #region 文件上传
        /// 
        /// LoTUploader-文件上传
        /// 
        /// 
        public JsonResult FileUpload(System.Web.HttpPostedFileBase file)
        {
            if (file == null) { return Json(new { status = false, msg = "文件提交失败" }); }
            if (file.ContentLength > 10485760) { return Json(new { status = false, msg = "文件10M以内" }); }
            string filterStr = ".gif,.jpg,.jpeg,.bmp,.png|.rar,.7z,.zip";
            string fileExt = Path.GetExtension(file.FileName).ToLower();
            if (!filterStr.Contains(fileExt)) { return Json(new { status = false, msg = "请上传图片或压缩包" }); }
            //防止黑客恶意绕过,判断下文件头文件
            if (!file.InputStream.CheckingExt("7173", "255216", "6677", "13780", "8297", "55122", "8075"))
            {
                //todo:一次危险记录
                return Json(new { status = false, msg = "请上传图片或压缩包" });
            }
            //todo: md5判断一下文件是否已经上传过,如果已经上传直接返回 return Json(new { status = true, msg = sqlPath });
            string path = string.Format("{0}/{1}", "/lotFiles", DateTime.Now.ToString("yyyy-MM-dd"));
            string fileName = string.Format("{0}{1}", Guid.NewGuid().ToString("N"), fileExt);
            string sqlPath = string.Format("{0}/{1}", path, fileName);
            string dirPath = Request.MapPath(path);

            if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); }
            try
            {
                file.SaveAs(Path.Combine(dirPath, fileName));
                file.InputStream.Dispose();
                filePathList.Add(Path.Combine(dirPath, fileName));
            }
            catch { return Json(new { status = false, msg = "文件保存失败" }); }
            return Json(new { status = true, msg = sqlPath });
        }
        #endregion

        #region 发邮件
        /// 
        /// 发邮件
        /// 
        /// 
        /// 
        public async Task SendMsg(MailModel model)
        {
            var obj = new AjaxOption();

            #region 校验系列
            if (model == null)
            {
                obj.Msg = "内容不能为空";
            }

            if (string.IsNullOrWhiteSpace(model.MailSubject))
            {
                obj.Msg = "邮件主题不能为空";
            }

            if (string.IsNullOrWhiteSpace(model.MailContent))
            {
                obj.Msg = "邮件内容不能为空";
            }

            #region 收件人邮箱
            if (model.MailToList != null)
            {
                foreach (var item in model.MailToList)
                {
                    if (!item.IsEmail())
                    {
                        model.MailToList.Remove(item);
                    }
                }
            }
            else
            {
                obj.Msg = "收件人邮箱不能为空";
            }

            //这个一定要加
            if (model.MailToList.Count == 0)
            {
                obj.Msg = "收件人邮箱不能为空";
            }
            #endregion

            if (model.MailCCList.ExistsData())
            {
                foreach (var item in model.MailCCList)
                {
                    if (!item.IsEmail())
                    {
                        model.MailCCList.Remove(item);
                    }
                }
            }
            #endregion

            //内容解码
            model.MailContent = System.Web.HttpUtility.UrlDecode(model.MailContent);

            //添加附件
            if (filePathList.ExistsData())
            {
                model.AttachmentList=filePathList;
            }

            if (obj.Msg.IsNullOrWhiteSpace())
                obj.Status = await EmailHelper.SendAsync(model);

            return Json(obj);
        }
        #endregion


3.项目应用:https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/3.常用技能/07.Email/2.EmailAPI

其实项目里面基本上是不用他附件功能的,比如你注册的时候发一个邮件给你来激活,你敏感操作的时候给你一个提示(比如异地登陆或者修改密码)

简单封装了一个api,一般每个项目里面都有这个发邮件的功能,很多公司把这些诸如上传,发邮件,短信通知的功能都封装成api,用的时候调用一下就可以了

效果图就不贴了,和上面差不多,就是没上传附件了

#region 发邮件
        /// 
        /// 发邮件
        /// 
        /// 
        /// 
        [CrossSite]
        public async Task Post([FromUri]MailModel model)
        {
            var obj = new AjaxOption();

            #region 校验系列
            if (model == null)
            {
                obj.Msg = "内容不能为空";
            }

            if (string.IsNullOrWhiteSpace(model.MailSubject))
            {
                obj.Msg = "邮件主题不能为空";
            }

            if (string.IsNullOrWhiteSpace(model.MailContent))
            {
                obj.Msg = "邮件内容不能为空";
            }

            #region 收件人邮箱
            if (model.MailToList != null)
            {
                foreach (var item in model.MailToList)
                {
                    if (!item.IsEmail())
                    {
                        model.MailToList.Remove(item);
                    }
                }
            }
            else
            {
                obj.Msg = "收件人邮箱不能为空";
            }

            //这个一定要加
            if (model.MailToList.Count == 0)
            {
                obj.Msg = "收件人邮箱不能为空";
            }
            #endregion

            if (model.MailCCList.ExistsData())
            {
                foreach (var item in model.MailCCList)
                {
                    if (!item.IsEmail())
                    {
                        model.MailCCList.Remove(item);
                    }
                }
            }
            #endregion

            //内容解码
            model.MailContent = System.Web.HttpUtility.UrlDecode(model.MailContent);

            if (obj.Msg.IsNullOrWhiteSpace())
                obj.Status = await EmailHelper.SendAsync(model);

            return obj.ObjectToJson();
        }
        #endregion





    
    邮件系列
    
    
    


    





 

EmailHelper:

using System.Net.Mail;
using System.Configuration;
using System.Threading.Tasks;
using System.Collections.Generic;

#region MailModel
/// 
/// MailModel
/// 
public class MailModel
{
    /// 
    /// 邮箱主题
    /// 
    public string MailSubject { get; set; }
    /// 
    /// 邮箱内容
    /// 
    public string MailContent { get; set; }
    /// 
    /// 收件人邮箱
    /// 
    public List MailToList { get; set; }
    /// 
    /// 抄送人邮箱
    /// 
    public List MailCCList { get; set; }
    /// 
    /// 附件路径
    /// 
    public List AttachmentList { get; set; }
}
#endregion

public class EmailHelper
{
    private static string mailFrom = ConfigurationManager.AppSettings["EmailForm"]; //登陆用户名
    private static string mailPass = ConfigurationManager.AppSettings["EmailPass"]; //登陆密码
    private static string mailSmtp = ConfigurationManager.AppSettings["EmailSmtp"]; //SMTP服务器

    #region 发送邮件
    /// 
    /// 发送邮件
    /// 
    /// 邮箱主题
    /// 邮箱内容
    /// 收件人邮箱
    /// 抄送人邮箱
    /// 附件路径
    /// 返回发送邮箱的结果
    public static async Task SendAsync(MailModel model)
    {
        #region 基本校验(一般都是调用前就验证了)
        //if (model == null || string.IsNullOrWhiteSpace(model.MailSubject) || string.IsNullOrWhiteSpace(model.MailContent) || model.MailTo == null)
        //{
        //    return false;
        //} 
        #endregion
        //邮件服务设置
        using (var smtpClient = new SmtpClient())
        {
            smtpClient.Host = mailSmtp;                                                     //指定SMTP服务器
            smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, mailPass);  //用户名和密码

            using (var mailMsg = new MailMessage())
            {
                //发信人邮箱
                mailMsg.From = new MailAddress(mailFrom);
                //收件人邮箱
                foreach (var item in model.MailToList)
                {
                    mailMsg.To.Add(item);
                }
                //抄送人邮箱
                if (model.MailCCList != null && model.MailCCList.Count > 0)
                {
                    foreach (var item in model.MailCCList)
                    {
                        mailMsg.CC.Add(item);
                    }
                }
                //附件系列
                if (model.AttachmentList != null)
                {
                    foreach (var item in model.AttachmentList)
                    {
                        try { mailMsg.Attachments.Add(new Attachment(item)); }
                        catch (System.Exception ex) { }
                    }
                }
                mailMsg.Subject = model.MailSubject;                //主题
                mailMsg.Body = model.MailContent;                   //内容
                mailMsg.BodyEncoding = System.Text.Encoding.UTF8;   //编码
                mailMsg.IsBodyHtml = true;                          //HTML格式
                try
                {
                    await smtpClient.SendMailAsync(mailMsg); //发送邮件
                    return true;
                }
                catch (System.Exception ex)
                {
                    mailMsg.Dispose();
                    smtpClient.Dispose();
                    return false;
                }
            }
        }
    }
    #endregion
}

如果没有账号,我提供几个测试账号:

mail.host = smtp.163.com
mail.username = [email protected]
mail.password = php1234
mail.smtp.from = [email protected]
mail.smtp.auth = true

——————————————————

mail.host=smtp.yeah.net
mail.port=25
mail.smtp.auth=true
mail.smtp.timeout=25000
[email protected]
mail.password=2436chao

——————————————————

mail.server.host=smtp.yeah.net
mail.server.post=25
mail.server.validate=true
mail.server.username=zmc330563778
mail.server.password=zmc586858
[email protected]

你可能感兴趣的:(json,xhtml,javascript)