List of usage examples for javax.mail Authenticator Authenticator
Authenticator
From source file:ee.cyber.licensing.service.MailService.java
public void generateAndSendMail(MailBody mailbody, int licenseId, int fileId) throws MessagingException, IOException, SQLException { License license = licenseRepository.findById(licenseId); List<Contact> contacts = contactRepository.findAll(license.getCustomer()); List<String> receivers = getReceivers(mailbody, contacts); 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"); 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 w w .j av a2 s . c o 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, "License dude")); //mailMessage.setReplyTo(InternetAddress.parse(email, false)); mailMessage.setSubject(mailbody.getSubject()); //String emailBody = body + "<br><br> Regards, <br>Cybernetica team"; mailMessage.setSentDate(new Date()); for (String receiver : receivers) { mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver)); } if (fileId != 0) { MailAttachment file = fileRepository.findById(fileId); if (file != null) { String fileName = file.getFileName(); byte[] fileData = file.getData_b(); if (fileName != null) { // Create a multipart message for attachment Multipart multipart = new MimeMultipart(); // Body part BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(mailbody.getBody(), "text/html"); multipart.addBodyPart(messageBodyPart); //Attachment part messageBodyPart = new MimeBodyPart(); ByteArrayDataSource source = new ByteArrayDataSource(fileData, "application/octet-stream"); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(fileName); multipart.addBodyPart(messageBodyPart); mailMessage.setContent(multipart); } } } else { mailMessage.setContent(mailbody.getBody(), "text/html"); } logger.info("5th ===> Get Session"); sendMail(email, password, host, getMailSession, mailMessage); }
From source file:org.lf.yydp.service.film.BuyTicketService.java
/** * :?//from ww w. j a v a 2 s .c o m * @param receiver */ public void sendEmail(String receiver) { Properties p = null; InputStream inputSteam = null; String senter = null; String Subject = null; String Content = null; try { p = new Properties(); inputSteam = BuyTicketService.class.getClassLoader().getResourceAsStream("mail.properties"); p.load(inputSteam); final String uname = p.getProperty("mail.uname"); final String license = p.getProperty("mail.license"); senter = p.getProperty("mail.senter"); Subject = p.getProperty("mail.subject"); Content = p.getProperty("mail.content"); Authenticator authenticator = new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(uname, license); } }; Session session = Session.getInstance(p, authenticator); MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(senter)); msg.setRecipient(RecipientType.TO, new InternetAddress(receiver)); msg.setSubject(Subject); msg.setContent(Content, "text/html;charset=utf-8"); Transport.send(msg); } catch (Exception e) { e.printStackTrace(); } finally { try { if (inputSteam != null) { inputSteam.close(); } } catch (Exception e2) { e2.printStackTrace(); } } }
From source file:org.unitime.commons.JavaMailWrapper.java
public JavaMailWrapper() { Properties p = ApplicationProperties.getProperties(); if (p.getProperty("mail.smtp.host") == null && p.getProperty("tmtbl.smtp.host") != null) p.setProperty("mail.smtp.host", p.getProperty("tmtbl.smtp.host")); final String user = ApplicationProperty.EmailSmtpUser.value(); final String password = ApplicationProperty.EmailSmtpPassword.value(); Authenticator a = null;//from w w w .j av a 2 s . co m if (user != null && password != null) { p.setProperty("mail.smtp.user", user); p.setProperty("mail.smtp.auth", "true"); a = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }; } iMailSession = javax.mail.Session.getDefaultInstance(p, a); iMail = new MimeMessage(iMailSession); iBody = new MimeMultipart(); }
From source file:com.pinterest.deployservice.email.SMTPMailManagerImpl.java
private Authenticator getAuthenticator() { if (!StringUtils.isEmpty(userName)) { return new Authenticator() { @Override//from w w w . j av a 2 s . c o m protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }; } else { return null; } }
From source file:com.gazbert.bxbot.core.mail.EmailAlerter.java
public void sendMessage(String subject, String msgContent) { if (sendEmailAlertsEnabled) { final Session session = Session.getInstance(smtpProps, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpConfig.getAccountUsername(), smtpConfig.getAccountPassword()); }//from w w w.ja v a 2 s. co m }); try { final Message message = new MimeMessage(session); message.setFrom(new InternetAddress(smtpConfig.getFromAddress())); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(smtpConfig.getToAddress())); message.setSubject(subject); message.setText(msgContent); LOG.info(() -> "About to send following Email Alert with message content: " + msgContent); Transport.send(message); } catch (MessagingException e) { // not much we can do here, especially if the alert was critical - the bot is shutting down; just log it. LOG.error("Failed to send Email Alert. Details: " + e.getMessage(), e); } } else { LOG.warn("Email Alerts are disabled. Not sending the following message: Subject: " + subject + " Content: " + msgContent); } }
From source file:org.openmrs.module.reporting.report.processor.EmailReportProcessor.java
/** * Returns the email session// ww w .j av a2 s . c om */ public Session getSession() { if (session == null) { AdministrationService as = Context.getAdministrationService(); Properties p = new Properties(); p.put("mail.transport.protocol", as.getGlobalProperty("mail.transport_protocol", "smtp")); p.put("mail.smtp.host", as.getGlobalProperty("mail.smtp_host", "localhost")); p.put("mail.smtp.port", as.getGlobalProperty("mail.smtp_port", "25")); // mail.smtp_port p.put("mail.smtp.auth", as.getGlobalProperty("mail.smtp_auth", "false")); // mail.smtp_auth p.put("mail.debug", as.getGlobalProperty("mail.debug", "false")); p.put("mail.from", as.getGlobalProperty("mail.from", "")); final String user = as.getGlobalProperty("mail.user", ""); final String password = as.getGlobalProperty("mail.password", ""); if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password)) { session = Session.getInstance(p, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }); } else { session = Session.getInstance(p); } } return session; }
From source file:pmp.springmail.TestDrive.java
void sendEmailViaPlainMail(String text) { Authenticator authenticator = new Authenticator() { @Override/*from w ww . j ava 2s . co m*/ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USER, PASS); } }; Session session = Session.getInstance(props, authenticator); Message message = new MimeMessage(session); try { message.setFrom(new InternetAddress(FROM)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); message.setSubject("Plain JavaMail Test"); message.setText(DATE_FORMAT.format(new Date()) + " " + text); Transport.send(message); } catch (Exception e) { System.err.println(e.getClass().getSimpleName() + " " + e.getMessage()); } }
From source file:org.eclipse.ecr.automation.core.mail.Mailer.java
/** * Create a mailer using a session that lookup for the session in JNDI * under the given session name. If the JNDI binding doesn't exists use the * given properties to cinfiugure the session. * * @param sessionName/*from ww w .ja v a 2 s . com*/ * @param config */ public Mailer(String sessionName, Properties config) { this.config = config; this.sessionName = sessionName; final String user = config.getProperty("mail.smtp.user"); final String pass = config.getProperty("mail.smtp.password"); if (user != null && pass != null) { setAuthenticator(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, pass); } }); } }
From source file:org.unitime.commons.Email.java
public Email() { Properties p = ApplicationProperties.getProperties(); if (p.getProperty("mail.smtp.host") == null && p.getProperty("tmtbl.smtp.host") != null) p.setProperty("mail.smtp.host", p.getProperty("tmtbl.smtp.host")); final String user = ApplicationProperties.getProperty("mail.smtp.user", ApplicationProperties .getProperty("unitime.email.user", ApplicationProperties.getProperty("tmtbl.mail.user"))); final String password = ApplicationProperties.getProperty("mail.smtp.password", ApplicationProperties .getProperty("unitime.email.password", ApplicationProperties.getProperty("tmtbl.mail.pwd"))); Authenticator a = null;/* w w w.j a v a 2s .c o m*/ if (user != null && password != null) { p.setProperty("mail.smtp.user", user); p.setProperty("mail.smtp.auth", "true"); a = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }; } iMailSession = javax.mail.Session.getDefaultInstance(p, a); iMail = new MimeMessage(iMailSession); iBody = new MimeMultipart(); }
From source file:org.awknet.commons.mail.Mail.java
public void send() throws AddressException, MessagingException, FileNotFoundException, IOException { int count = recipientsTo.size() + recipientsCc.size() + recipientsBcc.size(); if (count == 0) return;/* w w w .jav a2 s . c o m*/ deleteDuplicates(); Properties javaMailProperties = new Properties(); if (fileName.equals("") || fileName == null) fileName = DEFAULT_PROPERTIES_FILE; javaMailProperties.load(getClass().getResourceAsStream(fileName)); final String mailUsername = javaMailProperties.getProperty("mail.autentication.username"); final String mailPassword = javaMailProperties.getProperty("mail.autentication.password"); final String mailFrom = javaMailProperties.getProperty("mail.autentication.mail_from"); Session session = Session.getInstance(javaMailProperties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(mailUsername, mailPassword); } }); final MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(mailFrom)); msg.setRecipients(Message.RecipientType.TO, getToRecipientsArray()); msg.setRecipients(Message.RecipientType.CC, getCcRecipientsArray()); msg.setRecipients(Message.RecipientType.BCC, getBccRecipientsArray()); msg.setSentDate(new Date()); msg.setSubject(mailSubject); msg.setText(mailText, "UTF-8", "html"); // msg.setText(mailText); //OLD WAY new Thread(new Runnable() { public void run() { try { Transport.send(msg); Logger.getLogger(Mail.class.getName()).log(Level.INFO, "email was sent successfully!"); } catch (MessagingException ex) { Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, "Cant send email!", ex); } } }).start(); }