Search in sources :

Example 71 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project bitcampSCOpen2017 by ryuyj.

the class SimpleRegistrationNotifier2 method sendMailAttach.

public void sendMailAttach(Member member) {
    MimeMessage message = mailSender.createMimeMessage();
    try {
        // 메일에 파일 첨부를 위해서 MimeMessageHelper 클래스 이용,
        // 생성자 매개변수(메시지, 파일 첨부여부, 인코딩)
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        // 제목설정
        helper.setSubject("회원가입");
        // 내용설정
        String htmlContent = "<strong>안녕하세요</strong>, 반갑습니다.";
        helper.setText(htmlContent, true);
        // 보내는 사람 설정
        helper.setFrom("bit@gmail.com", "비트");
        // 받는사람 설정
        helper.setTo(new InternetAddress(member.getMember_id(), member.getMember_name(), "UTF-8"));
        // 첨부파일 설정
        DataSource dataSource = new FileDataSource("d:\\1300.jpg");
        helper.addAttachment(MimeUtility.encodeText("다스.jpg", "UTF-8", "B"), dataSource);
        mailSender.send(message);
    } catch (MessagingException | UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) FileDataSource(javax.activation.FileDataSource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Example 72 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project bitcampSCOpen2017 by ryuyj.

the class MimeMessageInlineTest method sendMail.

@Override
public void sendMail(Member member) {
    MimeMessage message = mailSender.createMimeMessage();
    try {
        MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");
        messageHelper.setSubject("[공지] 회원 가입 안내");
        String htmlContent = "<strong>안녕하세요</strong>, 반갑습니다." + "<img src=\"cid:logo.png\">";
        messageHelper.setText(htmlContent, true);
        messageHelper.setFrom("noreply@localhost.com", "로컬");
        messageHelper.setTo(new InternetAddress(member.getEmail(), member.getName(), "UTF-8"));
        DataSource dataSource = new FileDataSource("C:\\Users\\403room-23\\Desktop\\ddd.png");
        messageHelper.addInline("logo.png", dataSource);
        mailSender.send(message);
    } catch (MailException e) {
        e.printStackTrace();
        return;
    } catch (Throwable e) {
        e.printStackTrace();
        return;
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) FileDataSource(javax.activation.FileDataSource) MailException(org.springframework.mail.MailException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Example 73 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project spring-integration-samples by spring-projects.

the class MimeMessageParsingTest method testProcessingOfEmailAttachments.

/**
 * This test will create a Mime Message that contains an Attachment, send it
 * to an SMTP Server (Using Wiser) and retrieve and process the Mime Message.
 *
 * This test verifies that the parsing of the retrieved Mime Message is
 * successful and that the correct number of {@link EmailFragment}s is created.
 */
@Test
public void testProcessingOfEmailAttachments() {
    final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setPort(this.wiserPort);
    final MimeMessage message = mailSender.createMimeMessage();
    final String pictureName = "picture.png";
    final ByteArrayResource byteArrayResource = getFileData(pictureName);
    try {
        final MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("testfrom@springintegration.org");
        helper.setTo("testto@springintegration.org");
        helper.setSubject("Parsing of Attachments");
        helper.setText("Spring Integration Rocks!");
        helper.addAttachment(pictureName, byteArrayResource, "image/png");
    } catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(message);
    final List<WiserMessage> wiserMessages = wiser.getMessages();
    Assert.assertTrue(wiserMessages.size() == 1);
    boolean foundTextMessage = false;
    boolean foundPicture = false;
    for (WiserMessage wiserMessage : wiserMessages) {
        final List<EmailFragment> emailFragments = new ArrayList<EmailFragment>();
        try {
            final MimeMessage mailMessage = wiserMessage.getMimeMessage();
            EmailParserUtils.handleMessage(null, mailMessage, emailFragments);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving Mime Message.");
        }
        Assert.assertTrue(emailFragments.size() == 2);
        for (EmailFragment emailFragment : emailFragments) {
            if ("picture.png".equals(emailFragment.getFilename())) {
                foundPicture = true;
            }
            if ("message.txt".equals(emailFragment.getFilename())) {
                foundTextMessage = true;
            }
        }
        Assert.assertTrue(foundPicture);
        Assert.assertTrue(foundTextMessage);
    }
}
Also used : MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) WiserMessage(org.subethamail.wiser.WiserMessage) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailParseException(org.springframework.mail.MailParseException) EmailFragment(org.springframework.integration.samples.mailattachments.support.EmailFragment) Test(org.junit.Test)

Example 74 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project jhipster-sample-app-websocket by jhipster.

the class MailService method sendEmail.

@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
    log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", isMultipart, isHtml, to, subject, content);
    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, StandardCharsets.UTF_8.name());
        message.setTo(to);
        message.setFrom(jHipsterProperties.getMail().getFrom());
        message.setSubject(subject);
        message.setText(content, isHtml);
        javaMailSender.send(mimeMessage);
        log.debug("Sent email to User '{}'", to);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.warn("Email could not be sent to user '{}'", to, e);
        } else {
            log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
        }
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Async(org.springframework.scheduling.annotation.Async)

Example 75 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project jhipster-sample-app-hazelcast by jhipster.

the class MailService method sendEmail.

@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
    log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", isMultipart, isHtml, to, subject, content);
    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, StandardCharsets.UTF_8.name());
        message.setTo(to);
        message.setFrom(jHipsterProperties.getMail().getFrom());
        message.setSubject(subject);
        message.setText(content, isHtml);
        javaMailSender.send(mimeMessage);
        log.debug("Sent email to User '{}'", to);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.warn("Email could not be sent to user '{}'", to, e);
        } else {
            log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
        }
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Async(org.springframework.scheduling.annotation.Async)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)111 MimeMessage (javax.mail.internet.MimeMessage)103 MessagingException (javax.mail.MessagingException)47 Async (org.springframework.scheduling.annotation.Async)20 File (java.io.File)16 MailException (org.springframework.mail.MailException)16 FileSystemResource (org.springframework.core.io.FileSystemResource)12 Date (java.util.Date)11 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)11 InternetAddress (javax.mail.internet.InternetAddress)10 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)9 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)9 MailSendException (org.springframework.mail.MailSendException)7 IOException (java.io.IOException)6 Template (freemarker.template.Template)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Properties (java.util.Properties)5 DataSource (javax.activation.DataSource)5 Test (org.junit.Test)5