zoukankan      html  css  js  c++  java
  • C#邮件发送类 简单实用 可自定义发件人名称

    上图看效果

    MailHelper:

    public class MailHelper
        {
            public bool SendMail(MailSender sender,out string errorMsg)
            {
                //声明一个Mail对象
                MailMessage mymail = new MailMessage();
                //发件人地址
                //如是自己,在此输入自己的邮箱
                mymail.From = new MailAddress(sender.SendAddress,sender.SendNickName,Encoding.UTF8);
                //收件人地址
                mymail.To.Add(new MailAddress(sender.ReceiveAddress));
                //邮件主题
                mymail.Subject = sender.Title;
                //邮件标题编码
                mymail.SubjectEncoding = Encoding.UTF8;
                //发送邮件的内容
                mymail.Body = sender.Content;
                //邮件内容编码
                mymail.BodyEncoding = Encoding.UTF8;
                //添加附件
                foreach (var attachment in sender.Attachments)
                {
                    mymail.Attachments.Add(attachment);
                }
                
                //抄送到其他邮箱
                foreach (var str in sender.Cc)
                {
                    mymail.CC.Add(new MailAddress(str));
                }
                
                //是否是HTML邮件
                mymail.IsBodyHtml = sender.IsBodyHtml;
                //邮件优先级
                mymail.Priority = sender.MailPriority;
                //创建一个邮件服务器类
                SmtpClient myclient = new SmtpClient();
                myclient.Host = sender.Host;
                //SMTP服务端口
                myclient.Port = sender.Port;
                //验证登录
                myclient.Credentials = new NetworkCredential(sender.Username, sender.Password);//"@"输入有效的邮件名, "*"输入有效的密码
                try
                {
                    myclient.Send(mymail);
                    errorMsg = "";
                    return true;
                }
                catch (Exception ex)
                {
                    errorMsg = ex.Message;
                    return false;
                }
                
            } 
    

      调用:

    MailSender sender=new MailSender();
                sender.SendAddress = "***@***.com";
                sender.SendNickName = "辰晓晨";
                sender.ReceiveAddress = "***@***.com";
                sender.Host = "smtp.***.com";
                sender.Port = 25;
                sender.Username = "****@***.com";
                sender.Password = "******";
                sender.Title = "来自辰晓晨的邮件";
                sender.Content = "来自辰晓晨的邮件";
                sender.IsBodyHtml = false;
                sender.MailPriority=MailPriority.Normal;
                MailHelper.MailHelper mailHelper = new MailHelper.MailHelper();
                string errorMsg;
                if (mailHelper.SendMail(sender, out errorMsg))
                {
                    Console.WriteLine("发送成功");
                    
                }
                else
                {
                    Console.WriteLine("发送失败:"+errorMsg);
                    
                }
    

      

  • 相关阅读:
    Windows下安装并设置Redis
    Android平台相机接口的应用
    Struts(十五):主题
    Struts(十四):通用标签-form表单
    Struts(十三):通用标签
    Struts(十二):异常处理:exception-mapping元素
    Struts(十一):OGNL表达式(二)
    Struts(十):OGNL表达式(一)
    A*算法
    Centos:如何查找安装的jdk的目录
  • 原文地址:https://www.cnblogs.com/stulzq/p/5129509.html
Copyright © 2011-2022 走看看