List of usage examples for javax.mail Session getInstance
public static Session getInstance(Properties props, Authenticator authenticator)
From source file:ru.codemine.ccms.mail.EmailService.java
public void sendSimpleMessage(String address, String subject, String content) { try {// ww w. j a v a 2 s.com Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.auth", "true"); props.put("mail.mime.charset", "UTF-8"); props.put("mail.smtp.ssl.enable", ssl); Session session = Session.getInstance(props, new EmailAuthenticator(username, password)); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(username, "")); message.setRecipient(Message.RecipientType.TO, new InternetAddress(address)); message.setSubject(subject); message.setText(content, "utf-8", "html"); Transport transport = session.getTransport("smtp"); transport.connect(); transport.sendMessage(message, message.getAllRecipients()); } catch (MessagingException | UnsupportedEncodingException ex) { log.error( "? ? email, : " + ex.getLocalizedMessage()); ex.printStackTrace(); } }
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 www.j ava 2 s.c o 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:com.reizes.shiva.net.mail.Mail.java
public Session getSessoin() { Properties prop = new Properties(); prop.put("mail.transport.protocol", "smtp"); prop.put("mail.smtp.host", host); prop.put("mail.smtp.port", port); prop.put("mail.debug", debug ? "true" : "false"); // SMTP ??//from w w w. ja v a 2s . c o m prop.put("mail.smtp.auth", auth ? "true" : "false"); if (auth) { MailAuthenticator auth = new MailAuthenticator(id, password); return Session.getInstance(prop, auth); } return Session.getInstance(prop); }
From source file:uk.ac.ox.it.ords.api.statistics.services.impl.SendMailTLS.java
private void sendMail() { if (sendEmails) { if (username == null) { log.error("Unable to send emails due to null user"); return; }/*from w w w. jav a2 s. com*/ Session session = Session.getInstance(props, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(props.get("mail.smtp.username").toString(), props.get("mail.smtp.password").toString()); } }); try { Message message = new MimeMessage(session); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); message.setSubject(subject); message.setText(messageText); message.setFrom(new InternetAddress("ords@it.ox.ac.uk")); Transport.send(message); if (log.isDebugEnabled()) { log.debug(String.format("Sent email to %s (name %s)", email, username)); log.debug("with content: " + messageText); } } catch (MessagingException e) { log.error("Unable to send email to " + email + " username " + username, e); } } }
From source file:com.commoncoupon.mail.EmailProcessor.java
/** * Retrieves a Mail session from Tomcat's Resource Factory (JNDI) *///from w w w .j a v a2 s. c om 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 www. jav a2 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; } }
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/*w ww . j a va 2 s . c om*/ 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.apache.axis2.transport.mail.MailClient.java
public MailClient(String user, String host, String password, boolean debug) { from = user + '@' + host; authentication = new PasswordAuthentication(user, password); Properties props = new Properties(); props.put("mail.user", user); props.put("mail.host", host); props.put("mail.debug", debug ? "true" : "false"); props.put("mail.store.protocol", "pop3"); props.put("mail.transport.protocol", "smtp"); session = Session.getInstance(props, this); }
From source file:ro.agrade.jira.qanda.listeners.DirectEmailMessageHandler.java
@Override protected void sendMail(String[] recipients, String subject, String message, String from) throws MessagingException { SMTPMailServer server = mailServerManager.getDefaultSMTPMailServer(); if (server == null) { LOG.debug("Email server is not configured. QandA is unable to send mails ..."); return;//from ww w . j ava2 s .c om } LOG.debug("Email message: initializing."); //Set the host smtp address Properties props = new Properties(); String proto = server.getMailProtocol().getProtocol(); props.put("mail.transport.protocol", proto); props.put("mail." + proto + ".host", server.getHostname()); props.put("mail." + proto + ".port", server.getPort()); String username = server.getUsername(); String password = server.getPassword(); Authenticator auth = null; if (username != null && password != null) { auth = new SMTPAuthenticator(username, password); props.put("mail." + proto + ".auth", "true"); } Session session; try { session = auth != null ? Session.getDefaultInstance(props, auth) : Session.getDefaultInstance(props); } catch (SecurityException ex) { LOG.warn("Could not get default session. Attempting to create a new one."); session = auth != null ? Session.getInstance(props, auth) : Session.getInstance(props); } // create a message MimeMessage msg = new MimeMessage(session); Multipart multipart = new MimeMultipart(); if (from == null) { from = server.getDefaultFrom(); } // set the from address if (from != null) { InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); } if (recipients != null && recipients.length > 0) { // set TO address(es) InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); } // Setting the Subject msg.setSubject(subject); // Setting text content MimeBodyPart contentPart = new MimeBodyPart(); contentPart.setContent(message, "text/html; charset=utf-8"); multipart.addBodyPart(contentPart); msg.setContent(multipart); Transport.send(msg); LOG.debug("Email message sent successfully."); }
From source file:com.hs.mail.web.controller.FetchAccountFormController.java
private Store connect(String userName, String password) throws MessagingException { Session session = Session.getInstance(System.getProperties(), null); Store store = session.getStore("imap"); store.connect("localhost", userName, password); return store; }