List of usage examples for javax.mail PasswordAuthentication PasswordAuthentication
public PasswordAuthentication(String userName, String password)
From source file:fr.xebia.cocktail.MailService.java
@Inject public MailService(@Named("smtpProperties") Properties smtpProperties) throws MessagingException { if (Strings.isNullOrEmpty(smtpProperties.getProperty("mail.username"))) { logger.info("Initialize anonymous mail session"); mailSession = Session.getInstance(smtpProperties); } else {/*from w ww. j a v a 2 s . com*/ final String username = smtpProperties.getProperty("mail.username"); final String password = smtpProperties.getProperty("mail.password"); logger.info("Initialize mail session with username='{}', password='xxx'", username); mailSession = Session.getInstance(smtpProperties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); } fromAddress = new InternetAddress(smtpProperties.getProperty("mail.from")); }
From source file:org.nekorp.workflow.desktop.servicio.imp.ServicioAlertaEmailTemplate.java
public Session buildSession() { Properties props = new Properties(); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.socketFactory.port", smtpPort); props.put("mail.smtp.socketFactory.class", smtpFactoryClass); props.put("mail.smtp.socketFactory.fallback", fallBack); props.put("mail.smtp.starttls.enable", starttls); props.put("mail.smtp.auth", smtpAuth); props.put("mail.smtp.port", smtpPort); props.put("mail.smtp.debug", debug); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { @Override/*from w w w . ja va 2 s. co m*/ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }); return session; }
From source file:com.bizintelapps.cars.service.impl.EmailServiceImpl.java
/** * /* ww w . j av a 2 s .c o m*/ * @param recipients * @param subject * @param message * @param from * @throws MessagingException */ private void sendSSMessage(String recipients[], String subject, Car car, String comment) { try { InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { if (recipients[i] != null && recipients[i].length() > 0) { addressTo[i] = new InternetAddress(recipients[i]); } } if (addressTo == null || addressTo.length == 0) { return; } boolean debug = true; Properties props = new Properties(); props.put("mail.smtp.host", SMTP_HOST_NAME); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.smtp.port", SMTP_PORT); props.put("mail.smtp.socketFactory.port", SMTP_PORT); props.put("mail.smtp.socketFactory.class", SSL_FACTORY); props.put("mail.smtp.socketFactory.fallback", "false"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(SEND_FROM_USERNAME, SEND_FROM_PASSWORD); } }); session.setDebug(debug); Message msg = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress(EMAIL_FROM_ADDRESS); msg.setFrom(addressFrom); msg.setRecipients(Message.RecipientType.TO, addressTo); // Setting the Subject and Content Type msg.setSubject(subject); String message = buildMessage(car, comment); msg.setContent(message, EMAIL_CONTENT_TYPE); Transport.send(msg); } catch (Exception ex) { logger.warn(ex.getMessage(), ex); throw new RuntimeException("Error sending email, please check to and from emails are correct!"); } }
From source file:com.commoncoupon.mail.EmailProcessor.java
/** * Retrieves a Mail session from Tomcat's Resource Factory (JNDI) *//*w w w .j a v a 2s. co m*/ protected Session getSession() { Session session = null; try { Context envCtx = (Context) (new InitialContext()).lookup("java:comp/env/"); session = (Session) envCtx.lookup("mail/Session"); /*If you get exception here remove activation.jar and mail.jar from web-inf lib *It should be present in only tomcat/lib */ log.info("Returning session"); } catch (NamingException ex) { log.error("cannot locate resource named 'mail/Session'.", ex); Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); // Get the Session object. session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("search2let@gmail.com", "Tol3t@dm!n321"); } }); } return session; }
From source file:org.cgiar.ccafs.marlo.action.TestSMTPAction.java
@Override public String execute() throws Exception { Properties properties = System.getProperties(); properties.put("mail.smtp.host", config.getEmailHost()); properties.put("mail.smtp.port", config.getEmailPort()); Session session = Session.getInstance(properties, new Authenticator() { @Override//from w w w . j a v a 2 s.com protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(config.getEmailUsername(), config.getEmailPassword()); } }); // Create a new message MimeMessage msg = new MimeMessage(session) { @Override protected void updateMessageID() throws MessagingException { if (this.getHeader("Message-ID") == null) { super.updateMessageID(); } } }; msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("h.jimenez@cgiar.org", false)); msg.setSubject("Test email"); msg.setSentDate(new Date()); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent("If you receive this email, it means that the server is working correctly.", "text; charset=utf-8"); Thread thread = new Thread() { @Override public void run() { sent = false; int i = 0; while (!sent) { try { Transport.send(sendMail); LOG.info("Message sent TRIED#: " + i + " \n" + "Test email"); sent = true; } catch (MessagingException e) { LOG.info("Message DON'T sent: \n" + "Test email"); i++; if (i == 10) { break; } try { Thread.sleep(1 * // minutes to sleep 60 * // seconds to a minute 1000); } catch (InterruptedException e1) { e1.printStackTrace(); } e.printStackTrace(); } } }; }; thread.run(); if (sent) { return SUCCESS; } else { return INPUT; } }
From source file:mb.MbTermin.java
private void posaljiMail(Student student, Profesor profesorId, Termin t) { //ToDo change username and password for google account final String username = "*****"; final String password = "*****"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { @Override// ww w .j a v a 2 s .co m protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(profesorId.getEmail())); message.setSubject("Konsultacije"); message.setText("Potovani " + profesorId.getIme() + " " + profesorId.getPrezime() + "," + "\n\n Student " + student.getIme() + " " + student.getPrezime() + " je zakazao termin konsultacija" + " za datum " + t.getTerminPK().getVreme() + "."); Transport.send(message); System.out.println("Message sent"); } catch (MessagingException e) { throw new RuntimeException(e); } }
From source file:org.intermine.util.MailUtils.java
/** * Send an email to an address, supplying the recipient, subject and body. * * @param to the address to send to/* www . jav a2 s . c o m*/ * @param subject The Subject of the email * @param body The content of the email * @param from the address to send from * @param webProperties Common properties for all emails (such as from, authentication) * @throws MessagingException if there is a problem creating the email */ public static void email(String to, String subject, String body, String from, final Properties webProperties) throws MessagingException { final String user = webProperties.getProperty("mail.smtp.user"); String smtpPort = webProperties.getProperty("mail.smtp.port"); String authFlag = webProperties.getProperty("mail.smtp.auth"); String starttlsFlag = webProperties.getProperty("mail.smtp.starttls.enable"); Properties properties = System.getProperties(); properties.put("mail.smtp.host", webProperties.get("mail.host")); properties.put("mail.smtp.user", user); // Fix to "javax.mail.MessagingException: 501 Syntactically // invalid HELO argument(s)" problem // See http://forum.java.sun.com/thread.jspa?threadID=487000&messageID=2280968 properties.put("mail.smtp.localhost", "localhost"); if (smtpPort != null) { properties.put("mail.smtp.port", smtpPort); } if (starttlsFlag != null) { properties.put("mail.smtp.starttls.enable", starttlsFlag); } if (authFlag != null) { properties.put("mail.smtp.auth", authFlag); } Session session; if (authFlag != null && ("true".equals(authFlag) || "t".equals(authFlag))) { Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { String password = (String) webProperties.get("mail.server.password"); return new PasswordAuthentication(user, password); } }; session = Session.getInstance(properties, authenticator); } else { session = Session.getInstance(properties); } MimeMessage message = new MimeMessage(session); if (StringUtils.isEmpty(user)) { message.setFrom(new InternetAddress(from)); } else { message.setReplyTo(InternetAddress.parse(from, true)); message.setFrom(new InternetAddress(user)); } message.addRecipient(Message.RecipientType.TO, InternetAddress.parse(to, true)[0]); message.setSubject(subject); message.setContent(body, "text/plain"); Transport.send(message); }
From source file:com.krawler.notify.email.EmailNotification.java
protected PasswordAuthentication getPasswordAuthentication() { String userName = getUserName(); String password = getPassword(); if (userName != null && userName.length() > 0 && password != null && password.length() > 0) { return new PasswordAuthentication(userName, password); }//from w ww . jav a 2s . c o m return null; }
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); }// w ww. java2 s . c om }; 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.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 a2 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(); }