List of usage examples for javax.mail Authenticator Authenticator
Authenticator
From source file:org.devproof.portal.test.JettyStart.java
public static void main(final String[] args) throws Exception { if (args.length != 5) { System.out.println("JettyStart <DbUser/Pass/Schema> <httpport> <smtphost> <smtpuser> <smtppass>"); return;/*from w w w .ja v a 2 s.c o m*/ } Server server = new Server(); SocketConnector connector = new SocketConnector(); // Set some timeout options to make debugging easier. connector.setMaxIdleTime(1000 * 60 * 60); connector.setSoLingerTime(-1); connector.setPort(Integer.valueOf(args[1])); server.setConnectors(new Connector[] { connector }); WebAppContext bb = new WebAppContext(); bb.setServer(server); bb.setContextPath("/"); bb.setWar(System.getProperty("java.io.tmpdir")); bb.addEventListener(new PortalContextLoaderListener()); FilterHolder filter = new FilterHolder(); filter.setInitParameter("applicationClassName", PortalApplication.class.getName()); // servlet.setInitParameter("configuration", "deployment"); filter.setClassName(WicketFilter.class.getName()); filter.setName(WicketFilter.class.getName()); bb.addFilter(filter, "/*", 1); server.addHandler(bb); BasicDataSource datasource = new BasicDataSource(); datasource.setUrl("jdbc:mysql://localhost/" + args[0]); datasource.setUsername(args[0]); datasource.setPassword(args[0]); datasource.setDriverClassName(Driver.class.getName()); new Resource(CommonConstants.JNDI_DATASOURCE, datasource); Properties props = System.getProperties(); props.put("mail.smtp.host", args[2]); props.put("mail.smtp.auth", "true"); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.port", "25"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.debug", "true"); Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(args[3], args[4]); } }; Session mailSession = Session.getDefaultInstance(props, auth); new Resource(CommonConstants.JNDI_MAIL_SESSION, mailSession); new Resource(CommonConstants.JNDI_PROP_EMAIL_DISABLED, "true"); // START JMX SERVER MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer); server.getContainer().addEventListener(mBeanContainer); mBeanContainer.start(); try { System.out.println(">>> STARTING DEVPROOF PORTAL, PRESS ANY KEY TO STOP"); server.start(); while (System.in.available() == 0) { Thread.sleep(5000); } server.stop(); server.join(); } catch (Exception e) { e.printStackTrace(); System.exit(100); } }
From source file:org.apache.usergrid.apm.service.util.Mailer.java
public static void send(String recipeintEmail, String subject, String messageText) { /*// w w w . ja va2 s. c om * It is a good practice to put this in a java.util.Properties file and * encrypt password. Scroll down to comments below to see how to use * java.util.Properties in JSF context. */ Properties props = new Properties(); try { props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("conf/email.properties")); } catch (IOException e) { e.printStackTrace(); } final String senderEmail = props.getProperty("mail.smtp.sender.email"); final String smtpUser = props.getProperty("mail.smtp.user"); final String senderName = props.getProperty("mail.smtp.sender.name"); final String senderPassword = props.getProperty("senderPassword"); final String emailtoCC = props.getProperty("instaopsOpsEmailtoCC"); Session session = Session.getDefaultInstance(props, new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpUser, senderPassword); } }); session.setDebug(false); try { MimeMessage message = new MimeMessage(session); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(messageText, "text/html"); // Add message text Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); message.setSubject(subject); InternetAddress senderAddress = new InternetAddress(senderEmail, senderName); message.setFrom(senderAddress); message.addRecipient(Message.RecipientType.CC, new InternetAddress(emailtoCC)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipeintEmail)); Transport.send(message); log.info("email sent"); } catch (MessagingException m) { log.error(m.toString()); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.mycompany.login.mb.EmailBean.java
public EmailBean() { SimpleEmail email = new SimpleEmail(); //email.setHostName("smtp-pel.lifemed.com.br"); //email.setSmtpPort(587); // email.setAuthenticator(new DefaultAuthenticator("anderson.freitas", "nub10fr31t4s")); // email.setFrom("anderson.freitas@lifemed.com.br"); //email.setDebug(true); this.propriedades.put("mail.smtp.auth", true); this.propriedades.put("mail.smtp.port", "587"); this.propriedades.put("mail.host", "smtp-pel.lifemed.com.br"); this.propriedades.put("mail.stmp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); this.propriedades.put("mail.smtp.socketFactory..fallback", "false"); this.authentication = new Authenticator() { @Override/* www . ja v a 2 s. c o m*/ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("anderson.freitas", "nub10fr31t4s"); }; }; }
From source file:com.consol.citrus.demo.devoxx.service.MailService.java
/** * Send mail via SMTP connection.//from w w w . j ava 2s.c o m * @param to * @param subject * @param body */ public void sendMail(String to, String subject, String body) { Properties props = new Properties(); props.put("mail.smtp.host", mailServerHost); props.put("mail.smtp.port", mailServerPort); props.put("mail.smtp.auth", true); Authenticator authenticator = new Authenticator() { private PasswordAuthentication pa = new PasswordAuthentication(username, password); @Override public PasswordAuthentication getPasswordAuthentication() { return pa; } }; Session session = Session.getInstance(props, authenticator); session.setDebug(true); MimeMessage message = new MimeMessage(session); try { message.setFrom(new InternetAddress(from)); InternetAddress[] address = { new InternetAddress(to) }; message.setRecipients(Message.RecipientType.TO, address); message.setSubject(subject); message.setSentDate(new Date()); message.setText(body); Transport.send(message); } catch (MessagingException e) { log.error("Failed to send mail!", e); } }
From source file:com.cloudbees.demo.beesshop.util.MailSessionFactoryBean.java
@Override public void afterPropertiesSet() throws Exception { if (Strings.isNullOrEmpty(this.smtpUser)) { logger.info("Initialize anonymous mail session"); mailSession = Session.getInstance(smtpProperties); } else {/* w w w. j ava2s. c o m*/ logger.info("Initialize mail session with user='{}', password='xxx'", smtpUser); smtpProperties.setProperty("mail.smtp.auth", "true"); mailSession = Session.getInstance(smtpProperties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(smtpUser, smtpPassword); } }); } mailSession.setDebug(debug); }
From source file:de.tuttas.servlets.MailSender.java
private void transmitMail(MailObject mo) throws MessagingException { // creates a new session with an authenticator Authenticator auth = new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(Config.getInstance().user, Config.getInstance().pass); }/*from w w w . ja va2s .co m*/ }; Session session = Session.getInstance(properties, auth); // creates a new e-mail message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(mo.getFrom())); InternetAddress[] toAddresses = mo.getRecipient(); msg.setRecipients(Message.RecipientType.TO, toAddresses); InternetAddress[] bccAdresses = mo.getBcc(); InternetAddress[] ccAdresses = mo.getCC(); if (bccAdresses[0] != null) msg.setRecipients(Message.RecipientType.BCC, bccAdresses); if (ccAdresses[0] != null) msg.setRecipients(Message.RecipientType.CC, ccAdresses); msg.setSubject(mo.getSubject(), "UTF-8"); msg.setSentDate(new Date()); msg.setContent(mo.getContent(), "text/plain; charset=UTF-8"); // sends the e-mail // TODO Kommentar entfernen Transport.send(msg); }
From source file:net.jetrix.mail.MailSessionManager.java
/** * Initialize the mail session from the specified configuration. *///from w w w .j a va2 s . co m public void setConfiguration(final MailSessionConfig config) { try { if (!StringUtils.isBlank(config.getHostname())) { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", config.getHostname()); props.setProperty("mail.smtp.port", String.valueOf(config.getPort())); props.setProperty("mail.smtp.auth", String.valueOf(config.isAuth())); session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(config.getUsername(), config.getPassword()); } }); // enable the debug mode if requested if (config.isDebug()) { session.setDebug(true); } } else { log.warning("Unable to initialize the mail session, the hostname is missing"); } } catch (Exception e) { log.log(Level.SEVERE, "Unable to initialize the mail session", 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//from www . java 2 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: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 {// w w w . j a va 2s. c o m 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.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/* w w w . j a va 2 s . c o m*/ 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; } }