C# 后端发送邮件 支持附件

  /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="subject">邮件主题</param>
        /// <param name="msg">邮件内容</param>
        /// <param name="filePath">附件地址,如果不添加附件传null或""</param>
        /// <param name="senderEmail">发送人邮箱地址</param>
        /// <param name="senderPwd">发送人邮箱密码</param>
        /// <param name="recipientEmail">接收人邮箱</param>
        public static int SendMail(string subject, string msg, string filePath, string senderEmail, string senderPwd, string smtp,string name, params string[] recipientEmail)
        {
            if (!CheckIsNotEmptyOrNull(subject, msg, senderEmail, senderPwd) || recipientEmail == null || recipientEmail.Length == 0)
            {
                throw new Exception("输入信息无效");
            }
            try
            {
                string[] sendFromUser = senderEmail.Split('@');

                //构造一个Email的Message对象
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

                //确定smtp服务器地址。实例化一个Smtp客户端
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtp);

                //构造发件人地址对象
                message.From = new MailAddress(senderEmail, name, Encoding.UTF8);//sendFromUser[0]

                //构造收件人地址对象
                foreach (string userName in recipientEmail)
                {
                    message.To.Add(new MailAddress(userName, userName.Split('@')[0], Encoding.UTF8));
                }

                if (!string.IsNullOrEmpty(filePath))
                {
                    Attachment attach = new Attachment(filePath);
                    //得到文件的信息
                    ContentDisposition disposition = attach.ContentDisposition;
                    disposition.CreationDate = System.IO.File.GetCreationTime(filePath);
                    disposition.ModificationDate = System.IO.File.GetLastWriteTime(filePath);
                    disposition.ReadDate = System.IO.File.GetLastAccessTime(filePath);
                    //向邮件添加附件
                    message.Attachments.Add(attach);
                }

                //添加邮件主题和内容
                message.Subject = subject;
                message.SubjectEncoding = Encoding.UTF8;
                message.Body = msg;
                message.BodyEncoding = Encoding.UTF8;

                //设置邮件的信息
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;

                //如果服务器支持安全连接,则将安全连接设为true。
                //gmail,qq支持,163不支持
                switch (sendFromUser[1])
                {
                    case "gmail.com":
                    case "qq.com":
                        //client.EnableSsl = true;
                        break;
                    default:
                        client.EnableSsl = false;
                        break;
                }

                //设置用户名和密码。
              //  client.UseDefaultCredentials = true;
                client.Port = 587;
        
                client.UseDefaultCredentials = false;
                client.EnableSsl = true;
               
                //用户登陆信息
                NetworkCredential myCredentials = new NetworkCredential(senderEmail, senderPwd);
                client.Credentials = myCredentials;
                //发送邮件
                client.Send(message);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
                throw (ex);
               
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值