zoukankan      html  css  js  c++  java
  • .Net 发送邮件

    private static string Host = System.Configuration.ConfigurationManager.AppSettings["SMTPURL"].ToString(); //SMTP服务器地址
    private static string Account = System.Configuration.ConfigurationManager.AppSettings["SMTPACCOUNT"].ToString(); //SMTP服务帐号
    private static string Pwd = System.Configuration.ConfigurationManager.AppSettings["SMTPPWD"].ToString(); //SMTP服务密码
    private static string From = System.Configuration.ConfigurationManager.AppSettings["SMTPFROM"].ToString(); //发送方邮件地址

            public static int SendMail(string subject, string body, string toMail,ref string msg,string icon="")
            {
                int reslult = -1;
                string To = System.Web.HttpUtility.UrlDecode(toMail.Trim());   // 收件方邮件地址
    
                SmtpClient _smtpClient = new SmtpClient();
                _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
                _smtpClient.Host = Host; ;//指定SMTP服务器
                _smtpClient.Credentials = new System.Net.NetworkCredential(Account, Pwd);//用户名和密码
                MailMessage _mailMessage = new MailMessage(From, To);
    
                AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
                if (!string.IsNullOrEmpty(icon))
                {
                    LinkedResource lrImage = new LinkedResource(icon, "image/gif");
                    lrImage.ContentId = "weblogo";
                    htmlBody.LinkedResources.Add(lrImage);
                    _mailMessage.AlternateViews.Add(htmlBody);
                }
    
                _mailMessage.Subject = System.Web.HttpUtility.UrlDecode(subject); //主题 
                _mailMessage.Body = System.Web.HttpUtility.UrlDecode(body);//内容
                _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
                _mailMessage.IsBodyHtml = true;//设置为HTML格式
                _mailMessage.Priority = MailPriority.High;//优先级
                try
                {
                    _smtpClient.Send(_mailMessage);
                    reslult = 1;
                    msg = "发送成功";
                }
                catch (Exception ex)
                {
                    reslult = -1;
                    msg = ex.Message;
                }
                return reslult;
            }
    View Code
  • 相关阅读:
    【LeetCode】两个有序数组合成一个有序数组(NEW)
    swiftmonkey 源码剖析及二次开发思路
    CentOS7 + Python3 + Django(rest_framework) + MySQL + nginx + uwsgi 部署 API 开发环境, 记坑篇
    Vue 5小时学习小教程
    【LeetCode】两数相加
    (vue.js)vue中引用了别的组件 ,如何使this指向Vue对象
    Monkey for Mac 环境配置
    [Vue] 初识Vue-常用指令
    利用Tkinter做的自动生成JSONSchema的小工具
    Linux下如何删除非空目录
  • 原文地址:https://www.cnblogs.com/plming/p/8142975.html
Copyright © 2011-2022 走看看