List of usage examples for javax.mail Authenticator Authenticator
Authenticator
From source file:com.smartitengineering.emailq.binder.guice.EmailModule.java
private void configureJavaMailSession() { Properties properties = new Properties(); properties.setProperty(JAVAMAIL_SMTP_HOST, smtpHost); properties.setProperty(JAVAMAIL_SMTP_PORT, String.valueOf(smtpPort)); if (sslEnabled) { properties.setProperty(JAVAMAIL_SMTP_SSL_SOCKET_FACTORY_CLASS, JAVAMAIL_SMTP_SSL_SOCKET_FACTORY_CLASS_VAL); properties.setProperty(JAVAMAIL_SMTP_SSL_SOCKET_FACTORY_PORT, String.valueOf(smtpPort)); } else if (tlsEnabled) { properties.setProperty(JAVAMAIL_SMTP_TLS, String.valueOf(tlsEnabled)); }//from w w w. jav a2 s. c om if (authEnabled) { properties.setProperty(JAVAMAIL_SMTP_USER, smtpUser); properties.setProperty(JAVAMAIL_SMTP_AUTH, String.valueOf(authEnabled)); bind(Session.class).toInstance(Session.getInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpUser, smtpPassword); } })); } else { bind(Session.class).toInstance(Session.getInstance(properties)); } }
From source file:com.basp.trabajo_al_minuto.model.business.BusinessUtils.java
/** * Se encarga de enviar el mensaje de correo electronico * *//* w w w .j ava2s . c o m*/ public static Boolean sendEmail(final EmailMessage emdto) throws BusinessException { try { Session session; Properties properties = System.getProperties(); properties.put("mail.host", emdto.getHost()); properties.put("mail.transport.protocol", "smtp"); properties.put("mail.smtp.port", emdto.getPort()); properties.put("mail.smtp.starttls.enable", emdto.getStarttls()); if (emdto.getUser() != null && emdto.getPassword() != null) { properties.put("mail.smtp.auth", "true"); session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(emdto.getUser(), emdto.getPassword()); } }); } else { properties.put("mail.smtp.auth", "false"); session = Session.getDefaultInstance(properties); } message = new MimeMessage(session); message.setFrom(new InternetAddress(emdto.getFrom(), emdto.getMask())); message.setSubject(emdto.getSubject()); for (String to : emdto.getToList()) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); } if (emdto.getCcList() != null) { for (String cc : emdto.getCcList()) { message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc)); } } if (emdto.getCcoList() != null) { for (String cco : emdto.getCcoList()) { message.addRecipient(Message.RecipientType.BCC, new InternetAddress(cco)); } } bodyPart = new MimeBodyPart(); bodyPart.setContent(emdto.getBodyMessage(), emdto.getMimeTypeMessage()); multipart = new MimeMultipart(); multipart.addBodyPart(bodyPart); if (emdto.getFilePaths() != null) { for (String filePath : emdto.getFilePaths()) { File file = new File(filePath); if (file.exists()) { dataSource = new FileDataSource(file); bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(dataSource)); bodyPart.setFileName(file.getName()); multipart.addBodyPart(bodyPart); } } } message.setContent(multipart); Transport.send(message); } catch (MessagingException | UnsupportedEncodingException ex) { Logger.getLogger(BusinessUtils.class.getName()).log(Level.SEVERE, "BusinessUtils.sendEmail Exception", ex); throw new BusinessException(ex); } return Boolean.TRUE; }
From source file:ftpclient.FTPManager.java
public boolean sendUserMail(String uploadedFileName) { if (settings != null) { try {// www. j a va 2s .c om String userEmail = db.getUserEmail(uid); Properties props = System.getProperties(); props.setProperty("mail.smtp.host", settings.getSenderEmailHost()); props.setProperty("mail.smtp.auth", "true"); // props.put("mail.smtp.starttls.enable", "true"); // props.put("mail.smtp.port", "587"); Session s = Session.getDefaultInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(settings.getSenderLogin(), new String(settings.getSenderPassword())); } }); MimeMessage msg = new MimeMessage(s); msg.setFrom(settings.getSenderEmail()); msg.addRecipients(Message.RecipientType.TO, userEmail); msg.setSubject("FTP action"); msg.setText("You have succesfully uploaded file " + uploadedFileName); Transport.send(msg); return true; } catch (MessagingException ex) { Logger.getLogger(FTPManager.class.getName()).log(Level.SEVERE, null, ex); return false; } } else { return false; } }
From source file:org.pentaho.platform.scheduler2.email.Emailer.java
public boolean setup() { try {// www.ja v a 2 s . c o m final IEmailService service = PentahoSystem.get(IEmailService.class, "IEmailService", PentahoSessionHolder.getSession()); props.put("mail.smtp.host", service.getEmailConfig().getSmtpHost()); props.put("mail.smtp.port", ObjectUtils.toString(service.getEmailConfig().getSmtpPort())); props.put("mail.transport.protocol", service.getEmailConfig().getSmtpProtocol()); props.put("mail.smtp.starttls.enable", ObjectUtils.toString(service.getEmailConfig().isUseStartTls())); props.put("mail.smtp.auth", ObjectUtils.toString(service.getEmailConfig().isAuthenticate())); props.put("mail.smtp.ssl", ObjectUtils.toString(service.getEmailConfig().isUseSsl())); props.put("mail.smtp.quitwait", ObjectUtils.toString(service.getEmailConfig().isSmtpQuitWait())); props.put("mail.from.default", service.getEmailConfig().getDefaultFrom()); String fromName = service.getEmailConfig().getFromName(); if (StringUtils.isEmpty(fromName)) { fromName = Messages.getInstance().getString("schedulerEmailFromName"); } props.put("mail.from.name", fromName); props.put("mail.debug", ObjectUtils.toString(service.getEmailConfig().isDebug())); if (service.getEmailConfig().isAuthenticate()) { props.put("mail.userid", service.getEmailConfig().getUserId()); props.put("mail.password", service.getEmailConfig().getPassword()); setAuthenticator(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(service.getEmailConfig().getUserId(), service.getEmailConfig().getPassword()); } }); } return true; } catch (Exception e) { logger.error("Email.ERROR_0013_CONFIG_FILE_INVALID", e); //$NON-NLS-1$ } return false; }
From source file:org.pentaho.platform.plugin.services.email.EmailService.java
/** * Tests the provided email configuration by sending a test email. This will just indicate that the server * configuration is correct and a test email was successfully sent. It does not test the destination address. * /*from w ww. j av a2 s . co m*/ * @param emailConfig * the email configuration to test * @throws Exception * indicates an error running the test (as in an invalid configuration) */ public String sendEmailTest(final IEmailConfiguration emailConfig) { final Properties emailProperties = new Properties(); emailProperties.setProperty("mail.smtp.host", emailConfig.getSmtpHost()); emailProperties.setProperty("mail.smtp.port", ObjectUtils.toString(emailConfig.getSmtpPort())); emailProperties.setProperty("mail.transport.protocol", emailConfig.getSmtpProtocol()); emailProperties.setProperty("mail.smtp.starttls.enable", ObjectUtils.toString(emailConfig.isUseStartTls())); emailProperties.setProperty("mail.smtp.auth", ObjectUtils.toString(emailConfig.isAuthenticate())); emailProperties.setProperty("mail.smtp.ssl", ObjectUtils.toString(emailConfig.isUseSsl())); emailProperties.setProperty("mail.debug", ObjectUtils.toString(emailConfig.isDebug())); Session session = null; if (emailConfig.isAuthenticate()) { Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(emailConfig.getUserId(), emailConfig.getPassword()); } }; session = Session.getInstance(emailProperties, authenticator); } else { session = Session.getInstance(emailProperties); } String sendEmailMessage = ""; try { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(emailConfig.getDefaultFrom(), emailConfig.getFromName())); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailConfig.getDefaultFrom())); msg.setSubject(messages.getString("EmailService.SUBJECT")); msg.setText(messages.getString("EmailService.MESSAGE")); msg.setHeader("X-Mailer", "smtpsend"); msg.setSentDate(new Date()); Transport.send(msg); sendEmailMessage = "EmailTester.SUCESS"; } catch (Exception e) { logger.error(messages.getString("EmailService.NOT_CONFIGURED"), e); sendEmailMessage = "EmailTester.FAIL"; } return sendEmailMessage; }
From source file:org.pentaho.platform.util.Emailer.java
public boolean setup() { try {/*from ww w . j a va 2s . c om*/ final IEmailService service = PentahoSystem.get(IEmailService.class, "IEmailService", PentahoSessionHolder.getSession()); props.put("mail.smtp.host", service.getEmailConfig().getSmtpHost()); props.put("mail.smtp.port", ObjectUtils.toString(service.getEmailConfig().getSmtpPort())); props.put("mail.transport.protocol", service.getEmailConfig().getSmtpProtocol()); props.put("mail.smtp.starttls.enable", ObjectUtils.toString(service.getEmailConfig().isUseStartTls())); props.put("mail.smtp.auth", ObjectUtils.toString(service.getEmailConfig().isAuthenticate())); props.put("mail.smtp.ssl", ObjectUtils.toString(service.getEmailConfig().isUseSsl())); props.put("mail.smtp.quitwait", ObjectUtils.toString(service.getEmailConfig().isSmtpQuitWait())); props.put("mail.from.default", service.getEmailConfig().getDefaultFrom()); String fromName = service.getEmailConfig().getFromName(); if (StringUtils.isEmpty(fromName)) { fromName = getEmailFromName(); } props.put("mail.from.name", fromName); props.put("mail.debug", ObjectUtils.toString(service.getEmailConfig().isDebug())); if (service.getEmailConfig().isAuthenticate()) { props.put("mail.userid", service.getEmailConfig().getUserId()); props.put("mail.password", service.getEmailConfig().getPassword()); setAuthenticator(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { String decrypted; try { Base64PasswordService ps = new Base64PasswordService(); String pass = service.getEmailConfig().getPassword(); if (pass.startsWith("ENC:")) { decrypted = ps.decrypt( service.getEmailConfig().getPassword().substring(4, pass.length())); } else { decrypted = ps.decrypt(service.getEmailConfig().getPassword()); } } catch (Exception e) { decrypted = service.getEmailConfig().getPassword(); } return new PasswordAuthentication(service.getEmailConfig().getUserId(), decrypted); } }); } return true; } catch (Exception e) { logger.error("Email.ERROR_0013_CONFIG_FILE_INVALID", e); //$NON-NLS-1$ } return false; }
From source file:org.wso2.esb.integration.common.utils.MailToTransportUtil.java
/** * Getting the connection to email using Mail API * * @return - Email message Store// w w w . j av a 2 s. co m * @throws ESBMailTransportIntegrationTestException - Is thrown if an error while connecting to email store */ private static Store getConnection() throws ESBMailTransportIntegrationTestException { Properties properties; Session session; properties = new Properties(); properties.setProperty("mail.host", "imap.gmail.com"); properties.setProperty("mail.port", "995"); properties.setProperty("mail.transport.protocol", "imaps"); session = Session.getInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(receiver + "@" + domain, String.valueOf(receiverPassword)); } }); try { Store store = session.getStore("imaps"); store.connect(); return store; } catch (MessagingException e) { log.error("Error when creating the email store ", e); throw new ESBMailTransportIntegrationTestException("Error when creating the email store ", e); } }
From source file:ee.cyber.licensing.service.MailService.java
public void sendExpirationNearingMail(License license) throws IOException, MessagingException { logger.info("1st ===> setup Mail Server Properties"); Properties mailServerProperties = getProperties(); final String email = mailServerProperties.getProperty("fromEmail"); final String password = mailServerProperties.getProperty("password"); final String host = mailServerProperties.getProperty("mail.smtp.host"); final String mailTo = mailServerProperties.getProperty("mailTo"); logger.info("2nd ===> create Authenticator object to pass in Session.getInstance argument"); Authenticator authentication = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(email, password); }//from w ww .j av a2s . co m }; logger.info("Mail Server Properties have been setup successfully"); logger.info("3rd ===> get Mail Session.."); Session getMailSession = Session.getInstance(mailServerProperties, authentication); logger.info("4th ===> generateAndSendEmail() starts"); MimeMessage mailMessage = new MimeMessage(getMailSession); mailMessage.addHeader("Content-type", "text/html; charset=UTF-8"); mailMessage.addHeader("format", "flowed"); mailMessage.addHeader("Content-Transfer-Encoding", "8bit"); mailMessage.setFrom(new InternetAddress(email, "Licensing service")); mailMessage.setSubject("License with id " + license.getId() + " is expiring"); mailMessage.setSentDate(new Date()); mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo)); String emailBody = "This is test<br><br> Regards, <br>Licensing team"; mailMessage.setContent(emailBody, "text/html"); logger.info("5th ===> Get Session"); sendMail(email, password, host, getMailSession, mailMessage); }
From source file:org.wso2.carbon.event.output.adapter.email.EmailEventAdapter.java
/** * Initialize the Email SMTP session and be ready to send emails. * * @throws ConnectionUnavailableException on error. *///from w ww.ja v a 2 s.c om @Override public void connect() throws ConnectionUnavailableException { if (session == null) { /** * Default SMTP properties for outgoing messages. */ String smtpFrom; String smtpHost; String smtpPort; /** * Default from username and password for outgoing messages. */ final String smtpUsername; final String smtpPassword; // initialize SMTP session. Properties props = new Properties(); props.putAll(globalProperties); //Verifying default SMTP properties of the SMTP server. smtpFrom = props.getProperty(MailConstants.MAIL_SMTP_FROM); smtpHost = props.getProperty(EmailEventAdapterConstants.MAIL_SMTP_HOST); smtpPort = props.getProperty(EmailEventAdapterConstants.MAIL_SMTP_PORT); if (smtpFrom == null) { String msg = "failed to connect to the mail server due to null smtpFrom value"; throw new ConnectionUnavailableException( "The adapter " + eventAdapterConfiguration.getName() + " " + msg); } if (smtpHost == null) { String msg = "failed to connect to the mail server due to null smtpHost value"; throw new ConnectionUnavailableException( "The adapter " + eventAdapterConfiguration.getName() + " " + msg); } if (smtpPort == null) { String msg = "failed to connect to the mail server due to null smtpPort value"; throw new ConnectionUnavailableException( "The adapter " + eventAdapterConfiguration.getName() + " " + msg); } try { smtpFromAddress = new InternetAddress(smtpFrom); } catch (AddressException e) { log.error("Error in retrieving smtp address : " + smtpFrom, e); String msg = "failed to connect to the mail server due to error in retrieving " + "smtp from address"; throw new ConnectionUnavailableException( "The adapter " + eventAdapterConfiguration.getName() + " " + msg, e); } //Retrieving username and password of SMTP server. smtpUsername = props.getProperty(MailConstants.MAIL_SMTP_USERNAME); smtpPassword = props.getProperty(MailConstants.MAIL_SMTP_PASSWORD); //initializing SMTP server to create session object. if (smtpUsername != null && smtpPassword != null && !smtpUsername.isEmpty() && !smtpPassword.isEmpty()) { session = Session.getInstance(props, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpUsername, smtpPassword); } }); } else { session = Session.getInstance(props); log.info("Connecting adapter " + eventAdapterConfiguration.getName() + "without user authentication for tenant " + tenantId); } } }
From source file:bioLockJ.module.agent.MailAgent.java
private Session getSession() { final Properties props = new Properties(); props.put(EMAIL_SMTP_AUTH, emailSmtpAuth); props.put(EMAIL_START_TLS_ENABLE, emailTls); props.put(EMAIL_HOST, emailHost);//from w w w .java 2 s .c om props.put(EMAIL_PORT, emailPort); final Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(emailFrom, decrypt(emailEncryptedPassword)); } }); return session; }