List of usage examples for javax.mail.internet MimeMessage setFrom
public void setFrom(String address) throws MessagingException
From source file:AmazonSESSample.java
private static RawMessage getRawMessage() throws MessagingException, IOException { // JavaMail representation of the message Session s = Session.getInstance(new Properties(), null); s.setDebug(true);//ww w. j av a2 s . c o m MimeMessage msg = new MimeMessage(s); // Sender and recipient msg.setFrom(new InternetAddress("aravind@gofastpay.com")); InternetAddress[] address = { new InternetAddress("aravind@gofastpay.com") }; msg.setRecipients(javax.mail.Message.RecipientType.TO, address); msg.setSentDate(new Date()); // Subject msg.setSubject(SUBJECT); // Add a MIME part to the message //MimeMultipart mp = new MimeMultipart(); Multipart mp = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); //mimeBodyPart.setText(BODY); //BodyPart part = new MimeBodyPart(); //String myText = BODY; //part.setContent(URLEncoder.encode(myText, "US-ASCII"), "text/html"); //part.setText(BODY); //mp.addBodyPart(part); //msg.setContent(mp); mimeBodyPart.setContent(BODY, "text/html"); mp.addBodyPart(mimeBodyPart); msg.setContent(mp); // Print the raw email content on the console //PrintStream out = System.out; ByteArrayOutputStream out = new ByteArrayOutputStream(); msg.writeTo(out); //String rawString = out.toString(); //byte[] bytes = IOUtils.toByteArray(msg.getInputStream()); //ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length); //ByteBuffer byteBuffer = ByteBuffer.wrap(Base64.getEncoder().encode(rawString.getBytes())); //byteBuffer.put(bytes); //byteBuffer.put(Base64.getEncoder().encode(bytes)); RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(out.toByteArray())); return rawMessage; }
From source file:eu.forgestore.ws.util.EmailUtil.java
public static void SendRegistrationActivationEmail(String email, String messageBody) { Properties props = new Properties(); // Session session = Session.getDefaultInstance(props, null); props.setProperty("mail.transport.protocol", "smtp"); if ((FStoreRepository.getPropertyByName("mailhost").getValue() != null) && (!FStoreRepository.getPropertyByName("mailhost").getValue().isEmpty())) props.setProperty("mail.host", FStoreRepository.getPropertyByName("mailhost").getValue()); if ((FStoreRepository.getPropertyByName("mailuser").getValue() != null) && (!FStoreRepository.getPropertyByName("mailuser").getValue().isEmpty())) props.setProperty("mail.user", FStoreRepository.getPropertyByName("mailuser").getValue()); if ((FStoreRepository.getPropertyByName("mailpassword").getValue() != null) && (!FStoreRepository.getPropertyByName("mailpassword").getValue().isEmpty())) props.setProperty("mail.password", FStoreRepository.getPropertyByName("mailpassword").getValue()); String adminemail = FStoreRepository.getPropertyByName("adminEmail").getValue(); String subj = FStoreRepository.getPropertyByName("activationEmailSubject").getValue(); logger.info("adminemail = " + adminemail); logger.info("subj = " + subj); Session mailSession = Session.getDefaultInstance(props, null); Transport transport;//from ww w .j a va 2s .c o m try { transport = mailSession.getTransport(); MimeMessage msg = new MimeMessage(mailSession); msg.setSentDate(new Date()); msg.setFrom(new InternetAddress(adminemail, adminemail)); msg.setSubject(subj); msg.setContent(messageBody, "text/html; charset=ISO-8859-1"); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email)); transport.connect(); transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO)); transport.close(); } catch (NoSuchProviderException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:gr.upatras.ece.nam.baker.util.EmailUtil.java
public static void SendRegistrationActivationEmail(String email, String messageBody) { Properties props = new Properties(); // Session session = Session.getDefaultInstance(props, null); props.setProperty("mail.transport.protocol", "smtp"); if ((BakerRepository.getPropertyByName("mailhost").getValue() != null) && (!BakerRepository.getPropertyByName("mailhost").getValue().isEmpty())) props.setProperty("mail.host", BakerRepository.getPropertyByName("mailhost").getValue()); if ((BakerRepository.getPropertyByName("mailuser").getValue() != null) && (!BakerRepository.getPropertyByName("mailuser").getValue().isEmpty())) props.setProperty("mail.user", BakerRepository.getPropertyByName("mailuser").getValue()); if ((BakerRepository.getPropertyByName("mailpassword").getValue() != null) && (!BakerRepository.getPropertyByName("mailpassword").getValue().isEmpty())) props.setProperty("mail.password", BakerRepository.getPropertyByName("mailpassword").getValue()); String adminemail = BakerRepository.getPropertyByName("adminEmail").getValue(); String subj = BakerRepository.getPropertyByName("activationEmailSubject").getValue(); logger.info("adminemail = " + adminemail); logger.info("subj = " + subj); Session mailSession = Session.getDefaultInstance(props, null); Transport transport;/*from w w w . j a v a 2 s . c om*/ try { transport = mailSession.getTransport(); MimeMessage msg = new MimeMessage(mailSession); msg.setSentDate(new Date()); msg.setFrom(new InternetAddress(adminemail, adminemail)); msg.setSubject(subj); msg.setContent(messageBody, "text/html; charset=ISO-8859-1"); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email)); transport.connect(); transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO)); transport.close(); } catch (NoSuchProviderException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:portal.api.util.EmailUtil.java
public static void SendRegistrationActivationEmail(String email, String messageBody, String subj) { Properties props = new Properties(); // Session session = Session.getDefaultInstance(props, null); props.setProperty("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true"); if ((PortalRepository.getPropertyByName("mailhost").getValue() != null) && (!PortalRepository.getPropertyByName("mailhost").getValue().isEmpty())) props.setProperty("mail.host", PortalRepository.getPropertyByName("mailhost").getValue()); if ((PortalRepository.getPropertyByName("mailuser").getValue() != null) && (!PortalRepository.getPropertyByName("mailuser").getValue().isEmpty())) props.setProperty("mail.user", PortalRepository.getPropertyByName("mailuser").getValue()); if ((PortalRepository.getPropertyByName("mailpassword").getValue() != null) && (!PortalRepository.getPropertyByName("mailpassword").getValue().isEmpty())) props.setProperty("mail.password", PortalRepository.getPropertyByName("mailpassword").getValue()); String adminemail = PortalRepository.getPropertyByName("adminEmail").getValue(); final String username = PortalRepository.getPropertyByName("mailuser").getValue(); final String password = PortalRepository.getPropertyByName("mailpassword").getValue(); logger.info("adminemail = " + adminemail); logger.info("subj = " + subj); Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); }/*from ww w .j a v a 2 s .c o m*/ }); Transport transport; try { transport = mailSession.getTransport(); MimeMessage msg = new MimeMessage(mailSession); msg.setSentDate(new Date()); msg.setFrom(new InternetAddress(adminemail, adminemail)); msg.setSubject(subj); msg.setContent(messageBody, "text/html; charset=ISO-8859-1"); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email)); msg.addRecipient(Message.RecipientType.CC, new InternetAddress(adminemail, adminemail)); transport.connect(); Address[] recips = (Address[]) ArrayUtils.addAll(msg.getRecipients(Message.RecipientType.TO), msg.getRecipients(Message.RecipientType.CC)); transport.sendMessage(msg, recips); transport.close(); } catch (NoSuchProviderException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.xmlactions.email.EMailSend.java
public static void sendEMail(String fromAddress, String toAddress, String host, String userName, String password, String subject, String msg) throws AddressException, MessagingException { log.debug(String.format(//from w w w . ja va 2s . c o m "sendEMail(from:%s, to:%s, host:%s, userName:%s, password:%s)\nsubject:{" + subject + "\n}\nmessage:{" + msg + "\n}", fromAddress, toAddress, host, userName, toPassword(password), subject, msg)); Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session; if (!StringUtils.isEmpty(password)) { props.put("mail.smtp.auth", "true"); //EMailAuthenticator auth = new EMailAuthenticator(userName + "+" + host, password); EMailAuthenticator auth = new EMailAuthenticator(userName, password); session = Session.getInstance(props, auth); } else { session = Session.getInstance(props); } // Define message MimeMessage message = new MimeMessage(session); // message.setFrom(new InternetAddress("email_addresses@riostl.com")); message.setFrom(new InternetAddress(fromAddress)); message.addRecipient(RecipientType.TO, new InternetAddress(toAddress)); message.setSubject(subject); message.setText(msg); // Send message if (StringUtils.isEmpty(password)) { Transport.send(message); } else { Provider provider = session.getProvider("smtp"); Transport transport = session.getTransport(provider); // Send message transport.connect(); transport.sendMessage(message, new Address[] { new InternetAddress(toAddress) }); transport.close(); } }
From source file:com.fiveamsolutions.nci.commons.util.MailUtils.java
/** * Send an email./*from w ww .j a va2 s . c om*/ * @param u the recipient of the message * @param title the subject of the message * @param html the html content of the message * @param plainText the plain text content of the message * @throws MessagingException on error. */ public static void sendEmail(AbstractUser u, String title, String html, String plainText) throws MessagingException { if (!isMailEnabled()) { LOG.info("sending email to " + u.getEmail() + " with title " + title); LOG.info("plain text: " + plainText); LOG.info("html: " + html); return; } MimeMessage msg = new MimeMessage(getMailSession()); msg.setFrom(new InternetAddress(getFromAddress())); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(u.getEmail())); msg.setSubject(title); Multipart mp = new MimeMultipart("alternative"); BodyPart bp = new MimeBodyPart(); bp.setContent(html, "text/html"); mp.addBodyPart(bp); bp = new MimeBodyPart(); bp.setContent(plainText, "text/plain"); mp.addBodyPart(bp); msg.setContent(mp); Transport.send(msg); }
From source file:com.medsavant.mailer.Mail.java
public synchronized static boolean sendEmail(String to, String subject, String text, File attachment) { try {/*from w ww . j a va 2s .c om*/ if (src == null || pw == null || host == null || port == -1) { return false; } if (to.isEmpty()) { return false; } LOG.info("Sending email to " + to + " with subject " + subject); // create some properties and get the default Session Properties props = new Properties(); props.put("mail.smtp.user", src); props.put("mail.smtp.host", host); props.put("mail.smtp.port", port); props.put("mail.smtp.starttls.enable", starttls); props.put("mail.smtp.auth", auth); props.put("mail.smtp.socketFactory.port", port); props.put("mail.smtp.socketFactory.class", socketFactoryClass); props.put("mail.smtp.socketFactory.fallback", fallback); Session session = Session.getInstance(props, null); // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(src, srcName)); InternetAddress[] address = InternetAddress.parse(to); msg.setRecipients(Message.RecipientType.BCC, address); msg.setSubject(subject); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setContent(text, "text/html"); // create the Multipart and add its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); if (attachment != null) { // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message FileDataSource fds = new FileDataSource(attachment); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); mp.addBodyPart(mbp2); } // add the Multipart to the message msg.setContent(mp); // set the Date: header msg.setSentDate(new Date()); // send the message Transport transport = session.getTransport("smtp"); transport.connect(host, src, pw); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); LOG.info("Mail sent"); return true; } catch (Exception ex) { ex.printStackTrace(); LOG.error(ex); return false; } }
From source file:gov.nih.nci.protexpress.ui.actions.registration.EmailUtil.java
private static MimeMessage constructMessage(List<String> mailRecipients, String from, String mailSubject) throws MessagingException { Validate.notEmpty(mailRecipients, "No email recipients are specified"); if (StringUtils.isEmpty(mailSubject)) { LOG.info("No email subject specified"); }//from w w w. j a v a 2s . c o m List<Address> addresses = new ArrayList<Address>(); for (String recipient : mailRecipients) { addresses.add(new InternetAddress(recipient)); } MimeMessage message = new MimeMessage(mailSession); message.setRecipients(Message.RecipientType.TO, addresses.toArray(new Address[addresses.size()])); message.setFrom(new InternetAddress(from)); message.setSubject(mailSubject); return message; }
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/* w w w . j a v a2s . 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:gov.nih.nci.caarray.util.EmailUtil.java
private static MimeMessage constructMessage(List<String> mailRecipients, String from, String mailSubject) throws MessagingException { Validate.notEmpty(mailRecipients, "No email recipients are specified"); if (StringUtils.isEmpty(mailSubject)) { LOG.info("No email subject specified"); }//from ww w. j a v a 2 s . c o m List<Address> addresses = new ArrayList<Address>(); for (String recipient : mailRecipients) { addresses.add(new InternetAddress(recipient)); } MimeMessage message = new MimeMessage(MAILSESSION); message.setRecipients(Message.RecipientType.TO, addresses.toArray(new Address[addresses.size()])); message.setFrom(new InternetAddress(from)); message.setSubject(mailSubject); return message; }