Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-email
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-email
Commits
ae9d3984
Commit
ae9d3984
authored
Sep 28, 2025
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
push
parent
09eddd52
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
12 deletions
+34
-12
yd-email-api/src/main/java/com/yd/email/api/service/impl/ApiEmailServiceImpl.java
+34
-12
No files found.
yd-email-api/src/main/java/com/yd/email/api/service/impl/ApiEmailServiceImpl.java
View file @
ae9d3984
...
...
@@ -12,12 +12,14 @@ import org.springframework.util.CollectionUtils;
import
javax.activation.DataHandler
;
import
javax.activation.DataSource
;
import
javax.activation.FileDataSource
;
import
javax.activation.URLDataSource
;
import
javax.mail.*
;
import
javax.mail.internet.InternetAddress
;
import
javax.mail.internet.MimeBodyPart
;
import
javax.mail.internet.MimeMessage
;
import
javax.mail.internet.MimeMultipart
;
import
java.io.File
;
import
java.net.URL
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Properties
;
...
...
@@ -109,30 +111,34 @@ public class ApiEmailServiceImpl implements ApiEmailService {
// 将正文部分添加到多部分容器中
multipart
.
addBodyPart
(
messageBodyPart
);
//
判断是否有附件路径
//
修改附件处理部分:
if
(
StringUtils
.
isNotBlank
(
dto
.
getAttachmentPath
()))
{
// 按分号分割多个附件路径
String
[]
attachmentPaths
=
dto
.
getAttachmentPath
().
split
(
";"
);
for
(
String
attachmentPath
:
attachmentPaths
)
{
// 去除路径两端的空格
attachmentPath
=
attachmentPath
.
trim
();
// 检查路径是否非空
if
(
StringUtils
.
isNotBlank
(
attachmentPath
))
{
try
{
// 创建附件部分
MimeBodyPart
attachmentPart
=
new
MimeBodyPart
();
// 创建文件数据源
DataSource
source
=
new
FileDataSource
(
attachmentPath
);
// 设置附件的数据处理器
DataSource
source
;
String
fileName
;
if
(
attachmentPath
.
startsWith
(
"http://"
)
||
attachmentPath
.
startsWith
(
"https://"
))
{
// 使用URLDataSource处理网络附件
URL
url
=
new
URL
(
attachmentPath
);
source
=
new
URLDataSource
(
url
);
fileName
=
getFileNameFromUrl
(
attachmentPath
);
}
else
{
// 处理本地文件附件
source
=
new
FileDataSource
(
attachmentPath
);
fileName
=
new
File
(
attachmentPath
).
getName
();
}
attachmentPart
.
setDataHandler
(
new
DataHandler
(
source
));
// 设置附件文件名(使用原文件名)
attachmentPart
.
setFileName
(
new
File
(
attachmentPath
).
getName
());
// 将附件部分添加到多部分容器中
attachmentPart
.
setFileName
(
fileName
);
multipart
.
addBodyPart
(
attachmentPart
);
log
.
info
(
"添加附件: {}"
,
attachmentPath
);
}
catch
(
Exception
e
)
{
log
.
error
(
"添加附件失败: {}"
,
attachmentPath
,
e
);
// 可以选择继续处理其他附件,或者抛出异常
throw
new
RuntimeException
(
"添加附件失败: "
+
attachmentPath
,
e
);
}
}
...
...
@@ -156,4 +162,19 @@ public class ApiEmailServiceImpl implements ApiEmailService {
}
}
// 辅助方法:从URL中提取文件名
private
String
getFileNameFromUrl
(
String
url
)
{
try
{
return
new
File
(
new
URL
(
url
).
getPath
()).
getName
();
}
catch
(
Exception
e
)
{
return
"attachment"
;
}
}
// 辅助方法:获取文件扩展名
private
String
getFileExtension
(
String
filename
)
{
int
lastDot
=
filename
.
lastIndexOf
(
'.'
);
return
(
lastDot
==
-
1
)
?
""
:
filename
.
substring
(
lastDot
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment