Example usage for org.springframework.mail.javamail JavaMailSenderImpl DEFAULT_PROTOCOL

List of usage examples for org.springframework.mail.javamail JavaMailSenderImpl DEFAULT_PROTOCOL

Introduction

In this page you can find the example usage for org.springframework.mail.javamail JavaMailSenderImpl DEFAULT_PROTOCOL.

Prototype

String DEFAULT_PROTOCOL

To view the source code for org.springframework.mail.javamail JavaMailSenderImpl DEFAULT_PROTOCOL.

Click Source Link

Document

The default protocol: 'smtp'.

Usage

From source file:com.consol.citrus.admin.converter.endpoint.MailClientConverter.java

@Override
public EndpointModel convert(MailClientModel model) {
    EndpointModel endpointModel = new EndpointModel(getEndpointType(), model.getId(),
            getSourceModelClass().getName());

    endpointModel.add(property("host", model, true));
    endpointModel.add(property("port", model, "25", true));
    endpointModel.add(property("protocol", model, JavaMailSenderImpl.DEFAULT_PROTOCOL));
    endpointModel.add(property("username", model));
    endpointModel.add(property("password", model));
    endpointModel.add(property("properties", model));
    endpointModel.add(property("messageConverter", model).optionKey(MessageConverter.class.getName()));

    endpointModel.add(property("actor", "TestActor", model));

    return endpointModel;
}

From source file:com.glaf.mail.config.JavaMailSenderConfiguration.java

@Bean(name = "javaMailSender")
public JavaMailSenderImpl buildJavaMailSender() {
    MailConfig cfg = new MailConfig();
    String filename = SystemProperties.getConfigRootPath() + Constants.MAIL_CONFIG;
    Properties properties = PropertiesUtils.loadFilePathResource(filename);
    cfg.setEncoding(properties.getProperty("mail.defaultEncoding", "GBK"));
    cfg.setHost(properties.getProperty("mail.host", "127.0.0.1"));
    cfg.setUsername(properties.getProperty("mail.username"));
    cfg.setPassword(properties.getProperty("mail.password"));
    if (StringUtils.equals(properties.getProperty("mail.auth"), "true")) {
        cfg.setAuth(true);//from  w w  w . j  a v a2  s  .c  om
    }

    int port = JavaMailSenderImpl.DEFAULT_PORT;
    if (properties.getProperty("mail.port") != null) {
        port = Integer.parseInt(properties.getProperty("mail.port"));
    }

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setJavaMailProperties(properties);
    sender.setHost(properties.getProperty("mail.host"));
    sender.setPort(port);
    sender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
    sender.setUsername(properties.getProperty("mail.username"));
    sender.setPassword(properties.getProperty("mail.password"));
    sender.setDefaultEncoding(properties.getProperty("mail.defaultEncoding", "GBK"));

    return sender;
}

From source file:com.springsource.insight.plugin.mail.MessageSendOperationCollectionAspectTest.java

private void testSendMessage(int port) {
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost(NetworkAddressUtil.LOOPBACK_ADDRESS);
    sender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
    sender.setPort(port);/* w  w  w. j av  a2 s  . com*/

    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("from@com.springsource.insight.plugin.mail");
    message.setTo("to@com.springsource.insight.plugin.mail");
    message.setCc("cc@com.springsource.insight.plugin.mail");
    message.setBcc("bcc@com.springsource.insight.plugin.mail");

    Date now = new Date(System.currentTimeMillis());
    message.setSentDate(now);
    message.setSubject(now.toString());
    message.setText("Test at " + now.toString());
    sender.send(message);

    Operation op = getLastEntered();
    assertNotNull("No operation extracted", op);
    assertEquals("Mismatched operation type", MailDefinitions.SEND_OPERATION, op.getType());
    assertEquals("Mismatched protocol", sender.getProtocol(),
            op.get(MailDefinitions.SEND_PROTOCOL, String.class));
    assertEquals("Mismatched host", sender.getHost(), op.get(MailDefinitions.SEND_HOST, String.class));
    if (port == -1) {
        assertEquals("Mismatched default port", 25, op.getInt(MailDefinitions.SEND_PORT, (-1)));
    } else {
        assertEquals("Mismatched send port", sender.getPort(), op.getInt(MailDefinitions.SEND_PORT, (-1)));
    }

    if (getAspect().collectExtraInformation()) {
        assertAddresses(op, MailDefinitions.SEND_SENDERS, 1);
        assertAddresses(op, MailDefinitions.SEND_RECIPS, 3);

        OperationMap details = op.get(MailDefinitions.SEND_DETAILS, OperationMap.class);
        assertNotNull("No details extracted", details);
        assertEquals("Mismatched subject", message.getSubject(),
                details.get(MailDefinitions.SEND_SUBJECT, String.class));
    }
}

From source file:com.glaf.mail.business.MailSendThread.java

public void run() {
    logger.debug("---------------send mail----------------------------");
    if (mailItem != null && MailTools.isMailAddress(mailItem.getMailTo())) {
        /**//from w  ww  .  j a va  2s. c o  m
         * ?5????
         */
        if (mailItem.getRetryTimes() > 5) {
            return;
        }
        /**
         * ????????
         */
        if (mailItem.getSendStatus() == 1) {
            return;
        }
        /**
         * ????
         */
        if (mailItem.getLastModified() < (System.currentTimeMillis() - DateUtils.DAY * 30)) {
            return;
        }

        MailAccount mailAccount = null;
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        List<MailAccount> accounts = task.getAccounts();
        if (accounts.size() > 1) {
            java.util.Random r = new java.util.Random();
            mailAccount = accounts.get(Math.abs(r.nextInt(accounts.size())));
        } else {
            mailAccount = accounts.get(0);
        }

        logger.debug("send account:" + mailAccount);

        String content = task.getContent();

        if (StringUtils.isEmpty(task.getCallbackUrl())) {
            String serviceUrl = SystemConfig.getServiceUrl();
            String callbackUrl = serviceUrl + "/website/mail/receive/view";
            task.setCallbackUrl(callbackUrl);
        }

        if (StringUtils.isNotEmpty(task.getCallbackUrl())) {
            MxMailHelper mailHelper = new MxMailHelper();
            String messageId = "{taskId:\"" + task.getId() + "\",itemId:\"" + mailItem.getId() + "\"}";
            messageId = RequestUtils.encodeString(messageId);
            String url = task.getCallbackUrl() + "?messageId=" + messageId;
            content = mailHelper.embedCallbackScript(content, url);
        }

        MailMessage mailMessage = new MailMessage();
        mailMessage.setFrom(mailAccount.getMailAddress());
        mailMessage.setTo(mailItem.getMailTo());
        mailMessage.setContent(content);
        mailMessage.setSubject(task.getSubject());
        mailMessage.setSaveMessage(false);

        javaMailSender.setHost(mailAccount.getSmtpServer());
        javaMailSender.setPort(mailAccount.getSendPort() > 0 ? mailAccount.getSendPort() : 25);
        javaMailSender.setProtocol(JavaMailSenderImpl.DEFAULT_PROTOCOL);
        javaMailSender.setUsername(mailAccount.getUsername());
        if (StringUtils.isNotEmpty(mailAccount.getPassword())) {
            String pwd = RequestUtils.decodeString(mailAccount.getPassword());
            javaMailSender.setPassword(pwd);
            // logger.debug("send account pwd:"+pwd);
        }
        javaMailSender.setDefaultEncoding("GBK");

        try {
            mailItem.setSendDate(new Date());
            mailItem.setRetryTimes(mailItem.getRetryTimes() + 1);
            MailSender mailSender = ContextFactory.getBean("mailSender");
            mailSender.send(javaMailSender, mailMessage);
            mailItem.setSendStatus(1);
        } catch (Throwable ex) {
            mailItem.setSendStatus(-1);
            if (LogUtils.isDebug()) {
                logger.debug(ex);
                ex.printStackTrace();
            }
            if (ex instanceof javax.mail.internet.ParseException) {
                mailItem.setSendStatus(-10);
            } else if (ex instanceof javax.mail.AuthenticationFailedException) {
                mailItem.setSendStatus(-20);
            } else if (ex instanceof javax.mail.internet.AddressException) {
                mailItem.setSendStatus(-30);
            } else if (ex instanceof javax.mail.SendFailedException) {
                mailItem.setSendStatus(-40);
            } else if (ex instanceof java.net.UnknownHostException) {
                mailItem.setSendStatus(-50);
            } else if (ex instanceof java.net.SocketException) {
                mailItem.setSendStatus(-60);
            } else if (ex instanceof java.io.IOException) {
                mailItem.setSendStatus(-70);
            } else if (ex instanceof java.net.ConnectException) {
                mailItem.setSendStatus(-80);
            } else if (ex instanceof javax.mail.MessagingException) {
                mailItem.setSendStatus(-90);
                if (ex.getMessage().indexOf("response: 554") != -1) {
                    mailItem.setSendStatus(-99);
                }
            }
        } finally {
            mailDataFacede.updateMail(task.getId(), mailItem);
        }
    }
}