服务端的TigerApi 框架,基于.NET6 2024 版本
Ben Lin
2025-04-09 f8b74ce69663bed763f9fbc5b251a5d2da9bb4c7
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Text;
using Rhea.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tiger.Model;
 
namespace Tiger.Business
{
    /// <summary>
    /// 邮件信息
    /// </summary>
    public static class MailMessage
    {
        /// <summary>
        /// 组装邮件文本/附件邮件信息
        /// </summary>
        /// <param name="mailBodyEntity">邮件消息实体</param>
        /// <returns></returns>
        public static MimeMessage AssemblyMailMessage(MailBodyEntity mailBodyEntity)
        {
            if (mailBodyEntity == null)
            {
                throw new ArgumentNullException(nameof(mailBodyEntity));
            }
 
            var message = new MimeMessage();
 
            //设置邮件基本信息
            SetMailBaseMessage(message, mailBodyEntity);
 
            var multipart = new Multipart("mixed");
 
            //插入文本消息
            if (string.IsNullOrEmpty(mailBodyEntity.MailTextBody) == false)
            {
                var alternative = new MultipartAlternative
                {
                    AssemblyMailTextMessage(mailBodyEntity.MailTextBody, mailBodyEntity.MailBodyType)
                 };
 
                multipart.Add(alternative);
            }
 
            //插入附件
            if (mailBodyEntity.MailFilePath != null && File.Exists(mailBodyEntity.MailFilePath) == false)
            {
                var mimePart = AssemblyMailAttachmentMessage(mailBodyEntity.MailFileType, mailBodyEntity.MailFileSubType,
                     mailBodyEntity.MailFilePath);
 
                multipart.Add(mimePart);
            }
 
            //组合邮件内容
            message.Body = multipart;
 
            return message;
        }
 
        /// <summary>
        /// 设置邮件基础信息
        /// </summary>
        /// <param name="minMessag"></param>
        /// <param name="mailBodyEntity"></param>
        /// <returns></returns>
        public static MimeMessage SetMailBaseMessage(MimeMessage minMessag, MailBodyEntity mailBodyEntity)
        {
            if (minMessag == null)
            {
                throw new ArgumentNullException();
            }
 
            if (mailBodyEntity == null)
            {
                throw new ArgumentNullException();
            }
 
            //插入发件人
            minMessag.From.Add(new MailboxAddress(mailBodyEntity.Sender, mailBodyEntity.SenderAddress));
 
            //插入收件人
            foreach (var recipients in mailBodyEntity.Recipients)
            {
                minMessag.To.Add(new MailboxAddress(recipients, recipients));
            }
 
            //插入抄送人
            foreach (var cC in mailBodyEntity.Cc)
            {
                minMessag.Cc.Add(new MailboxAddress(cC, cC));
            }
 
            //插入主题
            minMessag.Subject = mailBodyEntity.Subject;
 
            return minMessag;
        }
 
        /// <summary>
        /// 组装邮件文本信息
        /// </summary>
        /// <param name="mailBody">邮件文本内容</param>
        /// <param name="textPartType">邮件文本类型(plain,html,rtf,xml)</param>
        /// <returns></returns>
        public static TextPart AssemblyMailTextMessage(string mailBody, string textPartType)
        {
            if (string.IsNullOrEmpty(mailBody))
            {
                throw new ArgumentNullException();
            }
 
            if (string.IsNullOrEmpty(textPartType))
            {
                throw new ArgumentNullException();
            }
 
            var textBody = new TextPart(textPartType)
            {
                Text = mailBody
            };
 
            return textBody;
        }
 
        /// <summary>
        /// 组装邮件附件信息
        /// </summary>
        /// <param name="fileAttachmentType">附件类型(image,application)</param>
        /// <param name="fileAttachmentSubType">附件子类型 </param>
        /// <param name="fileAttachmentPath">附件路径</param>
        /// <returns></returns>
        public static MimePart AssemblyMailAttachmentMessage(string fileAttachmentType, string fileAttachmentSubType, string fileAttachmentPath)
        {
            if (string.IsNullOrEmpty(fileAttachmentSubType))
            {
                throw new ArgumentNullException();
            }
 
            if (string.IsNullOrEmpty(fileAttachmentType))
            {
                throw new ArgumentNullException();
            }
 
            if (string.IsNullOrEmpty(fileAttachmentPath))
            {
                throw new ArgumentNullException();
            }
 
            var attachment = new MimePart(fileAttachmentType, fileAttachmentSubType)
            {
                Content = new MimeContent(File.OpenRead(fileAttachmentPath)),
                ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Base64,
                FileName = Path.GetFileName(fileAttachmentPath)
            };
 
            return attachment;
        }
 
    }
}