Search in sources :

Example 16 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project ngtesting-platform by aaronchen2k.

the class SystemConfig method javaMailSender.

@Bean
public JavaMailSender javaMailSender() {
    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setHost(mailHost);
    javaMailSender.setPort(mailPort);
    javaMailSender.setUsername(mailUserName);
    javaMailSender.setPassword(mailPassword);
    Properties p = new Properties();
    p.setProperty("mail.smtp.auth", "true");
    p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    javaMailSender.setJavaMailProperties(p);
    return javaMailSender;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties) Bean(org.springframework.context.annotation.Bean)

Example 17 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project flytecnologia-api by jullierme.

the class FlyMailConfig method mailSender.

@Bean
public JavaMailSender mailSender() {
    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost(env.getProperty("spring.mail.host"));
    mailSender.setPort(env.getProperty("spring.mail.port", Integer.class));
    mailSender.setUsername(env.getProperty("spring.mail.username"));
    mailSender.setPassword(env.getProperty("spring.mail.password"));
    mailSender.setProtocol(env.getProperty("spring.mail.protocol"));
    Properties props = mailSender.getJavaMailProperties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.connectiontimeout", 10000);
    props.put("mail.default-encoding", "UTF-8");
    return mailSender;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties) Bean(org.springframework.context.annotation.Bean)

Example 18 with JavaMailSenderImpl

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

the class MimeMessageParsingTest method testProcessingOfNestedEmailAttachments.

/**
 * This test will create a Mime Message that in return contains another
 * mime message. The nested mime message contains an attachment.
 *
 * The root message consist of both HTML and Text message.
 */
@Test
public void testProcessingOfNestedEmailAttachments() {
    final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setPort(this.wiserPort);
    final MimeMessage rootMessage = mailSender.createMimeMessage();
    try {
        final MimeMessageHelper messageHelper = new MimeMessageHelper(rootMessage, true);
        messageHelper.setFrom("testfrom@springintegration.org");
        messageHelper.setTo("testto@springintegration.org");
        messageHelper.setSubject("Parsing of Attachments");
        messageHelper.setText("Spring Integration Rocks!", "Spring Integration <b>Rocks</b>!");
        final String pictureName = "picture.png";
        final ByteArrayResource byteArrayResource = getFileData(pictureName);
        messageHelper.addInline("picture12345", byteArrayResource, "image/png");
    } catch (MessagingException e) {
        throw new MailParseException(e);
    }
    mailSender.send(rootMessage);
    final List<WiserMessage> wiserMessages = wiser.getMessages();
    Assert.assertTrue(wiserMessages.size() == 1);
    boolean foundTextMessage = false;
    boolean foundPicture = false;
    boolean foundHtmlMessage = false;
    for (WiserMessage wiserMessage : wiserMessages) {
        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() == 3);
        for (EmailFragment emailFragment : emailFragments) {
            if ("<picture12345>".equals(emailFragment.getFilename())) {
                foundPicture = true;
            }
            if ("message.txt".equals(emailFragment.getFilename())) {
                foundTextMessage = true;
            }
            if ("message.html".equals(emailFragment.getFilename())) {
                foundHtmlMessage = true;
            }
        }
        Assert.assertTrue(foundPicture);
        Assert.assertTrue(foundTextMessage);
        Assert.assertTrue(foundHtmlMessage);
    }
}
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 19 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project neubbs by nuitcoder.

the class SendEmailUtil method send.

/**
 * 发送(邮件)
 *      - 构建邮件请求
 *      - 构建参数
 *      - 构建连接
 *      - 构建消息
 *          - 设置发件人 + 昵称
 *          - 设置主题 + 内容
 *          - 设置收件人
 *          - 保存更改
 *      - 发送邮件
 *
 * @param sendNickname 发件人昵称
 * @param receiveEmail 接收邮箱
 * @param sendSubject 发送主题
 * @param sendEmailContent 发送内容
 */
public static void send(String sendNickname, String receiveEmail, String sendSubject, String sendEmailContent) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setUsername(FROM_USERNAME);
    sender.setPassword(FROM_AUTHORIZATION_CODE);
    sender.setHost(SetConst.TO_HOST);
    sender.setProtocol(SetConst.TO_SMTP);
    sender.setPort(Integer.parseInt(SetConst.TO_SMTP_SSL_PROT));
    Properties properties = new Properties();
    properties.setProperty(SetConst.TO_AUTH, SetConst.TO_AUTH_TRUE);
    properties.setProperty(SetConst.TO_MAIL_SMTP_SOCKETFACTORY_CLASS, SetConst.TO_JAVAX_NET_SSL_SSLSOCKETFACTORY);
    properties.setProperty(SetConst.TO_MAIL_SMTP_SOCKETFACTORY_PORT, SetConst.TO_SMTP_SSL_PROT);
    sender.setJavaMailProperties(properties);
    Session mailSession = Session.getDefaultInstance(properties, new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(FROM_USERNAME, FROM_AUTHORIZATION_CODE);
        }
    });
    MimeMessage message = new MimeMessage(mailSession);
    try {
        message.setFrom(new InternetAddress(FROM_USERNAME, sendNickname));
        message.setSubject(sendSubject, SetConst.FROM_SUBJECT_ENCODING);
        message.setContent(sendEmailContent, SetConst.FROM_CONTENT_TYPE);
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveEmail));
        message.saveChanges();
    } catch (UnsupportedEncodingException | MessagingException uee) {
        uee.printStackTrace();
    }
    sender.send(message);
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Properties(java.util.Properties) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 20 with JavaMailSenderImpl

use of org.springframework.mail.javamail.JavaMailSenderImpl in project thingsboard by thingsboard.

the class MailPlugin method initMailSender.

private void initMailSender(MailPluginConfiguration configuration) {
    JavaMailSenderImpl mail = new JavaMailSenderImpl();
    mail.setHost(configuration.getHost());
    mail.setPort(configuration.getPort());
    mail.setUsername(configuration.getUsername());
    mail.setPassword(configuration.getPassword());
    if (configuration.getOtherProperties() != null) {
        Properties mailProperties = new Properties();
        configuration.getOtherProperties().forEach(p -> mailProperties.put(p.getKey(), p.getValue()));
        mail.setJavaMailProperties(mailProperties);
    }
    mailSender = mail;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) Properties(java.util.Properties)

Aggregations

JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)56 Properties (java.util.Properties)25 Bean (org.springframework.context.annotation.Bean)14 MimeMessage (javax.mail.internet.MimeMessage)11 MessagingException (javax.mail.MessagingException)10 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)10 Test (org.junit.Test)9 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)6 Date (java.util.Date)4 IOException (java.io.IOException)3 ByteArrayResource (org.springframework.core.io.ByteArrayResource)3 MailException (org.springframework.mail.MailException)3 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)3 GreenMail (com.icegreen.greenmail.util.GreenMail)2 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2