Commit 70e2e2bc authored by 赵建伟's avatar 赵建伟

update codes

parent efc66085
......@@ -79,11 +79,11 @@
</component>
<component name="RunAnythingCache">
<option name="myCommands">
<command value="mvn clean install -DskipTests" />
<command value="mvn clean" />
<command value="mvn clean install -DskipTests" />
</option>
</component>
<component name="RunManager" selected="Application.Test">
<component name="RunManager" selected="JUnit.EmailServiceTest.testSendAttachmentsMail">
<configuration name="ExcelServiceImpl" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="com.gmei.data.gateway.server.service.impl.ExcelServiceImpl" />
<module name="service-server" />
......@@ -176,10 +176,10 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.EmailServiceTest.testSendAttachmentsMail" />
<item itemvalue="Application.Test" />
<item itemvalue="JUnit.ExcelServiceTest.testGenExcel" />
<item itemvalue="Application.ExcelServiceImpl" />
<item itemvalue="JUnit.EmailServiceTest.testSendAttachmentsMail" />
<item itemvalue="Spring Boot.ServiceServerApplication" />
</list>
</recent_temporary>
......@@ -215,45 +215,60 @@
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state x="773" y="303" key="#com.intellij.ide.util.MemberChooser/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1593835959317" />
<state width="1878" height="313" key="GridCell.Tab.0.bottom" timestamp="1594454800942">
<state width="1878" height="693" key="GridCell.Tab.0.bottom" timestamp="1594459289785">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state width="1878" height="313" key="GridCell.Tab.0.bottom/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594454800942" />
<state width="1878" height="693" key="GridCell.Tab.0.bottom/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594459289785" />
<state width="1878" height="243" key="GridCell.Tab.0.bottom/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520783" />
<state width="1878" height="313" key="GridCell.Tab.0.center" timestamp="1594454800941">
<state width="1878" height="693" key="GridCell.Tab.0.center" timestamp="1594459289783">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state width="1878" height="313" key="GridCell.Tab.0.center/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594454800941" />
<state width="1878" height="693" key="GridCell.Tab.0.center/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594459289783" />
<state width="1878" height="243" key="GridCell.Tab.0.center/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520781" />
<state width="1878" height="313" key="GridCell.Tab.0.left" timestamp="1594454800940">
<state width="1878" height="693" key="GridCell.Tab.0.left" timestamp="1594459289782">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state width="1878" height="313" key="GridCell.Tab.0.left/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594454800940" />
<state width="1878" height="693" key="GridCell.Tab.0.left/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594459289782" />
<state width="1878" height="243" key="GridCell.Tab.0.left/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520781" />
<state width="1878" height="313" key="GridCell.Tab.0.right" timestamp="1594454800942">
<state width="1878" height="693" key="GridCell.Tab.0.right" timestamp="1594459289784">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state width="1878" height="313" key="GridCell.Tab.0.right/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594454800942" />
<state width="1878" height="693" key="GridCell.Tab.0.right/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594459289784" />
<state width="1878" height="243" key="GridCell.Tab.0.right/1752.1200.1920.1151/0.22.1920.1178/1920.120.1920.1080@1752.1200.1920.1151" timestamp="1590742520782" />
<state width="1878" height="386" key="GridCell.Tab.1.bottom" timestamp="1591671347527">
<state width="1878" height="386" key="GridCell.Tab.1.bottom" timestamp="1594459250977">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state width="1878" height="386" key="GridCell.Tab.1.bottom/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1591671347527" />
<state width="1878" height="386" key="GridCell.Tab.1.center" timestamp="1591671347526">
<state width="1878" height="386" key="GridCell.Tab.1.bottom/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594459250977" />
<state width="1878" height="386" key="GridCell.Tab.1.center" timestamp="1594459250975">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state width="1878" height="386" key="GridCell.Tab.1.center/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1591671347526" />
<state width="1878" height="386" key="GridCell.Tab.1.left" timestamp="1591671347525">
<state width="1878" height="386" key="GridCell.Tab.1.center/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594459250975" />
<state width="1878" height="386" key="GridCell.Tab.1.left" timestamp="1594459250975">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state width="1878" height="386" key="GridCell.Tab.1.left/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1591671347525" />
<state width="1878" height="386" key="GridCell.Tab.1.right" timestamp="1591671347526">
<state width="1878" height="386" key="GridCell.Tab.1.left/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594459250975" />
<state width="1878" height="386" key="GridCell.Tab.1.right" timestamp="1594459250976">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state width="1878" height="386" key="GridCell.Tab.1.right/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1591671347526" />
<state x="625" y="273" width="670" height="675" key="run.anything.popup" timestamp="1594454130020">
<state width="1878" height="386" key="GridCell.Tab.1.right/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594459250976" />
<state x="626" y="343" width="667" height="486" key="find.popup" timestamp="1594455372378">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state x="625" y="273" width="670" height="675" key="run.anything.popup/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594454130020" />
<state x="626" y="343" width="667" height="486" key="find.popup/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594455372378" />
<state x="625" y="273" width="670" height="675" key="run.anything.popup" timestamp="1594459250950">
<screen x="0" y="22" width="1920" height="1129" />
</state>
<state x="625" y="273" width="670" height="675" key="run.anything.popup/0.22.1920.1129/-981.-1200.1920.1200/939.-1080.1920.1080@0.22.1920.1129" timestamp="1594459250950" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/service-server/src/main/java/com/gmei/data/gateway/server/service/impl/EmailServiceImpl.java</url>
<line>125</line>
<option name="timeStamp" value="9" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>
\ No newline at end of file
......@@ -10,19 +10,18 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSendException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Component
......@@ -43,12 +42,45 @@ public class EmailServiceImpl implements EmailService {
private JavaMailSender mailSender;
private static final Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);
private void sendAttachMail(String[] toUsers,String[] ccUsers,String name) throws Exception{
private List<String> getInvalidAddresses(Throwable throwable) {
List<String> rsList = new ArrayList<String>();
if (throwable == null) {
return rsList;
}
if (throwable instanceof MailSendException) {
Exception[] exceptions = ((MailSendException) throwable).getMessageExceptions();
for (Exception exception : exceptions) {
if (exception instanceof SendFailedException) {
rsList = getStringAddress(((SendFailedException) exception).getInvalidAddresses());
}
}
}
if (throwable instanceof SendFailedException) {
rsList = getStringAddress(((SendFailedException) throwable).getInvalidAddresses());
}
return rsList;
}
private List<String> getStringAddress(Address[] addresses) {
List<String> invalid = new ArrayList<String>();
for (Address a : addresses) {
String address = ((InternetAddress) a).getAddress();
if (address != null && StringUtils.isNotEmpty(address)) {
invalid.add(address);
}
}
return invalid;
}
@Override
public int sendAttachmentsMail(String to,String cc,String name){
String[] toUsers = to.split(",");
String[] ccUsers = cc.split(",");
try {
String subject = "";
String properPath = properBasePath + "pm/" + name + "/en-cn.properties";
logger.info("邮件主题属性文件目录为:{}",properPath);
String value = ProperUtils.getValue(properPath , name);
//String value = ProperUtils.getValue(properFilePath, name);
if(null != value){
subject = value;
}else{
......@@ -69,49 +101,30 @@ public class EmailServiceImpl implements EmailService {
String fileNameSrc = name + "-" + DateUtils.getYesterdayDateSimpleStr() + ".xlsx";
FileSystemResource file = new FileSystemResource(new File(filePath + fileNameSrc));
helper.addAttachment(fileName, file);
//helper.addAttachment(MimeUtility.encodeWord(file.getFilename(),"utf-8","B"), file);
mailSender.send(message);
}
@Override
public int sendAttachmentsMail(String to,String cc,String name){
String[] toUsers = to.split(",");
String[] ccUsers = cc.split(",");
try {
sendAttachMail(toUsers,ccUsers,name);
logger.info("带附件的邮件已经发送。");
} catch (SendFailedException sendFailedException) {
Set<String> failMails = new HashSet<String>();
for(Address address: sendFailedException.getInvalidAddresses()){
failMails.add(address.toString().trim());
}
List<String> validToMails = new ArrayList<String>();
for(String toMail : toUsers){
if(StringUtils.isBlank(toMail)||failMails.contains(toMail.trim())){
logger.warn("剔除无效邮箱地址:{}",toMail);
}catch(Throwable e){
String validToUsers = "";
List<String> invalid = getInvalidAddresses(e);
for(String toUser : toUsers){
if(invalid.contains(toUser)){
logger.warn("剔除无效接收人邮箱地址:{}",toUser);
continue;
}
validToMails.add(toMail);
validToUsers = validToUsers + toUser + ",";
}
List<String> validCcMails = new ArrayList<String>();
for(String ccMail : ccUsers){
if(StringUtils.isBlank(ccMail)||failMails.contains(ccMail.trim())){
logger.warn("剔除无效邮箱地址:{}",ccMail);
String validCcUsers = "";
for(String ccUser : ccUsers){
if(invalid.contains(ccUser)){
logger.warn("剔除无效抄送人邮箱地址:{}",ccUser);
continue;
}
validCcMails.add(ccMail);
validCcUsers = validCcUsers + ccUser + ",";
}
try{
sendAttachMail((String[])validToMails.toArray(),(String[])validCcMails.toArray(),name);
}catch (Exception e){
e.printStackTrace();
logger.error(e.getMessage());
return Constants.FAIL;
if (invalid.size() > 0) {
validToUsers = validToUsers.substring(0, validToUsers.length() - 1);
validCcUsers = validCcUsers.substring(0, validCcUsers.length() - 1);
sendAttachmentsMail(validToUsers,validCcUsers,name);
}
}catch (Exception e){
e.printStackTrace();
logger.error(e.getMessage());
return Constants.FAIL;
}
return Constants.SUCCESS;
}
......
......@@ -156,7 +156,7 @@ public class ExcelServiceImpl implements ExcelService {
}
}else{
cell.setCellValue("");
logger.error("the value is null!");
logger.warn("the value is null!");
}
}
......
......@@ -21,7 +21,7 @@ public class EmailServiceTest {
// @Test
// public void testSendAttachmentsMail() {
// emailService.sendAttachmentsMail("zhaojianwei@igengmei.com","jianweizhao@yeah.net","demo");
// emailService.sendAttachmentsMail("zhaojianwei@igengmei.com,zjw@igengmei.com","jianweizhao@yeah.net,aa@bb.com","demo");
// }
//
// @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment