username@email.com
2023-12-07 c7ca1bd2476a1e45a900a24a4ea220710e3415b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using DTO;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;
 
namespace CommonToolsCore
{
    public class SendMailHelper
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static ResultEntity SendMail(MailModel model)
        {
            ResultEntity resultEntity = new ResultEntity();
            try
            {
              
                //MailAddress receiver = new MailAddress(model.ReceiverAddress, model.ReceiverName);
               
               
                MailMessage message = new MailMessage();
                if (model.receivers != null && model.receivers.Count > 0)
                {
                    foreach (var receiver in model.receivers)
                    {
                        message.To.Add(receiver);//收件人
                    }
                }
                model.Host = "smtp.qq.com";
                model.Port = 587;
                model.SenderName = "政采咨询网";
                model.SenderAddress = "3543725713@qq.com";//272629192@qq.com
                model.SenderPassword = "gumcmfdbcfnjdajd"; //mnooicotjaflbhdd
                MailAddress sender = new MailAddress(model.SenderAddress, model.SenderName);
                message.From = sender;//发件人
                //message.To.Add(receiver);//收件人
                message.BodyEncoding = System.Text.Encoding.Default;//正文编码  
                message.Priority = MailPriority.High;//优先级  
                //message.CC.Add(sender);//抄送人
                message.Subject = model.Title;//标题
                message.Body = model.Content;//内容
                message.IsBodyHtml = true;//是否支持内容为HTML
 
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.exmail.qq.com";
                if (!string.IsNullOrWhiteSpace(model.Host))
                {
                    client.Host = model.Host;
                }
                client.Port = 587;
                if (model.Port>0)
                {
                    client.Port = model.Port;
                }
                client.EnableSsl = true;//是否启用SSL
                client.Timeout = 10000;//超时
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.UseDefaultCredentials = false;
                client.Credentials = new NetworkCredential(model.SenderAddress, model.SenderPassword);
                client.Send(message);
                resultEntity.Result = true;
            }
            catch (Exception e)
            {
 
                resultEntity.Result = false;
                resultEntity.Message = "发送失败" + e.Message;
 
                return resultEntity;
            }
            return resultEntity;
        }
 
        /// <summary>
        /// 邮件结构体
        /// </summary>
        public struct MailModel
        {
            /// <summary>
            /// 收件人地址
            /// </summary>
            public string ReceiverAddress { get; set; }
            /// <summary>
            /// 收件人姓名
            /// </summary>
            public string ReceiverName { get; set; }
 
            public List<MailAddress>  receivers { get; set; }
            /// <summary>
            /// 标题
            /// </summary>
            public string Title { get; set; }
 
            /// <summary>
            /// 服务器
            /// </summary>
            public string Host { get; set; }
 
            /// <summary>
            /// 端口
            /// </summary>
            public int Port { get; set; }
 
            /// <summary>
            /// 内容
            /// </summary>
            public string Content { get; set; }
            /// <summary>
            /// 发件人地址(非必填)
            /// </summary>
            public string SenderAddress { get; set; }
            /// <summary>
            /// 发件人姓名(非必填)
            /// </summary>
            public string SenderName { get; set; }
            /// <summary>
            /// 发件人密码(非必填)
            /// </summary>
            public string SenderPassword { get; set; }
        }
    }
 
}