====== JavaMailSender ======
* java email mail sender
===== 첨부파일 =====
* [[https://www.mkyong.com/spring/spring-sending-e-mail-with-attachment/|Spring – Sending e-mail with attachment]]
public class MailMail
{
private JavaMailSender mailSender;
private SimpleMailMessage simpleMailMessage;
public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
}
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMail(String dear, String content) {
MimeMessage message = mailSender.createMimeMessage();
try{
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(simpleMailMessage.getFrom());
helper.setTo(simpleMailMessage.getTo());
helper.setSubject(simpleMailMessage.getSubject());
helper.setText(String.format(
simpleMailMessage.getText(), dear, content));
FileSystemResource file = new FileSystemResource("C:\\log.txt");
helper.addAttachment(file.getFilename(), file);
}catch (MessagingException e) {
throw new MailParseException(e);
}
mailSender.send(message);
}
}
* 이미 오픈된 ''InputStream''은 ''ByteArrayDataSource'' 사용
ByteArrayDataSource attachment = new ByteArrayDataSource(message.getPayload(), "application/octet-stream");
* ''Passed-in Resource contains an open stream: invalid argument. JavaMail requires an InputStreamSource that creates a fresh stream for every call.'' 오류 발생
* JavaMailSender는 InputStream 을 두 번 연다. 한 번은 인코딩을 확인하러, 한 번은 확인한 인코딩으로 인코드 하려고. 따라서 열린 Stream이면 안된다.