반응형
eml 파일의 헤더부분 subject, messgeid를 변경하여 원하는 숫자만큼 복사하는 프로세스
마임이 없을 경우 라이브러리를 참조하도록 구성해야한다
package com.crinity.webapps.mail.web;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
/**
* @For test : Eml file copy
* @author JeongEuiJin
*
*/
@Controller
@RequestMapping("/mail")
public class test_file_copy {
public static void main(String[] args) {
// 파일 경로 수정
String dir = "F:/testfile/";
// 파일 이름 수정 (확장자 제외)
String orgFileName = "일반첨부한메일";
// 복사할 파일 갯수
int copysize = 501;
// 확장자 입력 (eml 파일 복사를 위한 코드이므로 eml고정)
String ext = ".eml";
Path source = Paths.get(dir + orgFileName + ext);
InputStream in = null;
for(int i=1; i < copysize+1; i++) {
MimeMessage orgmime = null;
try {
orgmime = setMimeMessageFromEml(in);
} catch (MessagingException e1) {
e1.printStackTrace();
}
try {
Path targetDir = Paths.get(dir + "snu_copy_test/" + orgFileName + "_" + Integer.toString(i) + ext);
if (!Files.exists(targetDir.getParent())) {
Files.createDirectories(targetDir.getParent());
}
String contents = new String(Files.readAllBytes(source), Charset.defaultCharset());
String orgSubject = null;
String encOrgSubject = null;
try {
encOrgSubject = orgmime.getHeader("Subject", null);
orgSubject = orgmime.getSubject();
} catch (MessagingException e) {
e.printStackTrace();
}
String newSubject = orgSubject +"_"+i;
String encNewSubject = MimeUtility.encodeText(newSubject, "UTF-8", "B");
String delchar = encOrgSubject.replace("=?UTF-8?B?", "");
int ei = delchar.lastIndexOf("?=");
encOrgSubject = delchar.substring(0,ei);
String delchar2 = encNewSubject.replace("=?UTF-8?B?", "");
int ei2 = delchar2.lastIndexOf("?=");
encNewSubject = delchar2.substring(0,ei2);
String newcon = contents.replaceAll(encOrgSubject, encNewSubject);
// msgid 변경
try {
String orgmsgid = orgmime.getMessageID().split("@")[0];
String newmsgid = orgmsgid + "_imapTest_" + i;
newcon = newcon.replaceAll(orgmsgid, newmsgid);
String orgfrom = orgmime.getFrom()[0].toString().split("@")[0];
String orgfrom2 = orgfrom.split("<")[1];
String newfrom = orgfrom2+"_imapTest_"+i;
newcon = newcon.replaceAll(orgfrom2, newfrom);
String orgname = orgfrom.split("<")[0];
String newname = orgname+"_imapTest_"+i;
newcon = newcon.replaceAll(orgname, newname);
Files.write(Files.createFile(targetDir), newcon.getBytes());
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("copy succ : "+targetDir);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private static MimeMessage setMimeMessageFromEml(InputStream source) throws MessagingException {
Properties sessionProperties = System.getProperties();
sessionProperties.put("mail.host", "smtp.dummydomain.com");
sessionProperties.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(sessionProperties, null);
MimeMessage message = new MimeMessage(mailSession, source);
return message;
}
}
반응형
'프로그래밍 노트 > Spring, Java, Kotlin' 카테고리의 다른 글
thymeleaf 와 tailwind 로 자바 서블릿 프로젝트 구성하기 (0) | 2023.11.15 |
---|---|
Springboot에서 messages.properties로 다국어 사용하는 방법 (reload까지) (2) | 2022.08.17 |
SpringBoot 2.6에서 swagger 3.0 사용 (11) | 2021.12.15 |
JAVA로 API 개발할 때 DTO파일 읽어서 필요하거나 필요 없는 필드 확인하기 (0) | 2021.08.03 |
java AES 인코딩, 디코딩 예제 (0) | 2020.11.06 |