@@ -99,41 +99,20 @@ public async Task SendEMailAsync(string subject, string content, IEnumerable<Mai
9999 var message = new MimeMessage ( ) ;
100100 message . From . AddRange ( fromAddress ) ;
101101 message . To . AddRange ( toAddress ) ;
102- if ( ccAddress != null && ccAddress . Any ( ) )
102+ if ( ! this . IsEmpty ( ccAddress ) )
103103 {
104- message . Cc . AddRange ( ccAddress ) ;
104+ message . Cc . AddRange ( ccAddress ) ;
105105 }
106- if ( bccAddress != null && bccAddress . Any ( ) )
106+ if ( ! this . IsEmpty ( bccAddress ) )
107107 {
108- message . Bcc . AddRange ( bccAddress ) ;
108+ message . Bcc . AddRange ( bccAddress ) ;
109109 }
110110 message . Subject = subject ;
111111 var body = new TextPart ( textFormat )
112112 {
113113 Text = content
114- } ;
115- MimeEntity entity = body ;
116- if ( attachments != null )
117- {
118- var mult = new Multipart ( "mixed" )
119- {
120- body
121- } ;
122- foreach ( var att in attachments )
123- {
124- if ( att . Stream != null )
125- {
126- var attachment = string . IsNullOrWhiteSpace ( att . ContentType ) ? new MimePart ( ) : new MimePart ( att . ContentType ) ;
127- attachment . Content = new MimeContent ( att . Stream ) ;
128- attachment . ContentDisposition = new ContentDisposition ( ContentDisposition . Attachment ) ;
129- attachment . ContentTransferEncoding = att . ContentTransferEncoding ;
130- attachment . FileName = ConvertHeaderToBase64 ( att . FileName , Encoding . UTF8 ) ; //解决附件中文名问题
131- mult . Add ( attachment ) ;
132- }
133- }
134- entity = mult ;
135- }
136- message . Body = entity ;
114+ } ;
115+ message . Body = this . GetMimeEntity ( body , attachments ) ;
137116 message . Date = DateTime . Now ;
138117 using ( var client = new SmtpClient ( ) )
139118 {
@@ -173,5 +152,34 @@ private string ConvertHeaderToBase64(string inputStr, Encoding encoding)
173152 }
174153 return inputStr ;
175154 }
155+ private bool IsEmpty < T > ( IEnumerable < T > source )
156+ {
157+ return source == null || ! source . Any ( ) ;
158+ }
159+ private MimeEntity GetMimeEntity ( MimePart body , IEnumerable < AttachmentInfo > attachments )
160+ {
161+ MimeEntity entity = body ;
162+ if ( ! this . IsEmpty ( attachments ) )
163+ {
164+ var mult = new Multipart ( "mixed" )
165+ {
166+ body
167+ } ;
168+ foreach ( var att in attachments )
169+ {
170+ if ( att . Stream != null )
171+ {
172+ var attachment = string . IsNullOrWhiteSpace ( att . ContentType ) ? new MimePart ( ) : new MimePart ( att . ContentType ) ;
173+ attachment . Content = new MimeContent ( att . Stream ) ;
174+ attachment . ContentDisposition = new ContentDisposition ( ContentDisposition . Attachment ) ;
175+ attachment . ContentTransferEncoding = att . ContentTransferEncoding ;
176+ attachment . FileName = ConvertHeaderToBase64 ( att . FileName , Encoding . UTF8 ) ; //解决附件中文名问题
177+ mult . Add ( attachment ) ;
178+ }
179+ }
180+ entity = mult ;
181+ }
182+ return entity ;
183+ }
176184 }
177185}
0 commit comments