List of usage examples for javax.mail.internet MimeMessage addRecipient
public void addRecipient(RecipientType type, Address address) throws MessagingException
From source file:MailExample.java
public static void main(String args[]) throws Exception { if (args.length != 3) { System.err.println("Usage: java MailExample host from to"); System.exit(-1);// ww w . j a v a2 s. c o m } String host = args[0]; String from = args[1]; String to = args[2]; // Get system properties Properties props = System.getProperties(); // Setup mail server props.put("mail.smtp.host", host); // Get session Session session = Session.getDefaultInstance(props, null); // Define message MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("The Subject"); message.setText("The Message"); // Send message Transport.send(message); }
From source file:Main.java
/** * Create a MimeMessage using the parameters provided. * * @param to Email address of the receiver. * @param from Email address of the sender, the mailbox account. * @param subject Subject of the email./*ww w . ja va 2 s . c om*/ * @param bodyText Body text of the email. * @return MimeMessage to be used to send email. * @throws MessagingException */ public static MimeMessage createEmail(String to, String from, String subject, String bodyText) throws MessagingException { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage email = new MimeMessage(session); email.setFrom(new InternetAddress(from)); email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to)); email.setSubject(subject); email.setText(bodyText); return email; }
From source file:org.vosao.utils.EmailUtil.java
/** * Send email with html content and attachments. * @param htmlBody/* ww w . ja v a2 s . co m*/ * @param subject * @param fromAddress * @param fromText * @param toAddress * @return null if OK or error message. */ public static String sendEmail(final String htmlBody, final String subject, final String fromAddress, final String fromText, final String toAddress, final List<FileItem> files) { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); try { Multipart mp = new MimeMultipart(); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(htmlBody, "text/html"); htmlPart.setHeader("Content-type", "text/html; charset=UTF-8"); mp.addBodyPart(htmlPart); for (FileItem item : files) { MimeBodyPart attachment = new MimeBodyPart(); attachment.setFileName(item.getFilename()); String mimeType = MimeType.getContentTypeByExt(FolderUtil.getFileExt(item.getFilename())); DataSource ds = new ByteArrayDataSource(item.getData(), mimeType); attachment.setDataHandler(new DataHandler(ds)); mp.addBodyPart(attachment); } MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(fromAddress, fromText)); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress, toAddress)); msg.setSubject(subject, "UTF-8"); msg.setContent(mp); Transport.send(msg); return null; } catch (AddressException e) { return e.getMessage(); } catch (MessagingException e) { return e.getMessage(); } catch (UnsupportedEncodingException e) { return e.getMessage(); } }
From source file:org.vosao.utils.EmailUtil.java
/** * Send email with html content and attachments. * @param htmlBody//from ww w .j a v a2s. c o m * @param subject * @param fromAddress * @param fromText * @param toAddress * @return null if OK or error message. */ public static String sendEmail(final String htmlBody, final String subject, final String fromAddress, final String fromText, final String toAddress, final List<FileItem> files) { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); try { Multipart mp = new MimeMultipart(); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(htmlBody, "text/html"); htmlPart.setHeader("Content-type", "text/html; charset=UTF-8"); mp.addBodyPart(htmlPart); for (FileItem item : files) { MimeBodyPart attachment = new MimeBodyPart(); attachment.setFileName(item.getFilename()); String mimeType = MimeType.getContentTypeByExt(FolderUtil.getFileExt(item.getFilename())); if (mimeType.equals("text/plain")) { mimeType = MimeType.DEFAULT; } DataSource ds = new ByteArrayDataSource(item.getData(), mimeType); attachment.setDataHandler(new DataHandler(ds)); mp.addBodyPart(attachment); } MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(fromAddress, fromText)); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddress, toAddress)); msg.setSubject(subject, "UTF-8"); msg.setContent(mp); Transport.send(msg); return null; } catch (AddressException e) { return e.getMessage(); } catch (MessagingException e) { return e.getMessage(); } catch (UnsupportedEncodingException e) { return e.getMessage(); } }
From source file:gt.dakaik.common.Common.java
public static MimeMessage createEmail(String to, String subject, String bodyText) throws MessagingException { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage email = new MimeMessage(session); email.setFrom(new InternetAddress(sendMailFrom)); email.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to)); email.setSubject(subject);//from w w w .j a va 2 s .c o m email.setText(bodyText); return email; }
From source file:com.email.SendEmailNotification.java
/** * This sends a basic notification email that is just pure TEXT. Message is * sent from the section gathered/* w w w.j a v a2s .c om*/ * * @param eml DocketNotificationModel */ public static void sendNotificationEmail(DocketNotificationModel eml) { //Get Account SystemEmailModel account = null; for (SystemEmailModel acc : Global.getSystemEmailParams()) { if (acc.getSection().equals(eml.getSection())) { account = acc; break; } } if (account != null) { String FROMaddress = account.getEmailAddress(); String[] TOAddressess = ((eml.getSendTo() == null) ? "".split(";") : eml.getSendTo().split(";")); String subject = eml.getMessageSubject(); String body = eml.getMessageBody(); Authenticator auth = EmailAuthenticator.setEmailAuthenticator(account); Properties properties = EmailProperties.setEmailOutProperties(account); Session session = Session.getInstance(properties, auth); MimeMessage email = new MimeMessage(session); try { for (String To : TOAddressess) { if (EmailValidator.getInstance().isValid(To)) { email.addRecipient(Message.RecipientType.TO, new InternetAddress(To)); } } email.setFrom(new InternetAddress(FROMaddress)); email.setSubject(subject); email.setText(body); if (Global.isOkToSendEmail()) { Transport.send(email); } else { Audit.addAuditEntry("Notification Not Actually Sent: " + eml.getId() + " - " + subject); } DocketNotification.deleteEmailEntry(eml.getId()); } catch (AddressException ex) { ExceptionHandler.Handle(ex); } catch (MessagingException ex) { ExceptionHandler.Handle(ex); } } }
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 ww. j a va 2 s. 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.email.SendEmailCrashReport.java
/** * Sends crash email to predetermined list from the database. * * Also BCCs members of XLN team for notification of errors *//*from w w w . java 2 s. com*/ public static void sendCrashEmail() { //Get Account SystemEmailModel account = null; for (SystemEmailModel acc : Global.getSystemEmailParams()) { if (acc.getSection().equals("ERROR")) { account = acc; break; } } if (account != null) { String FROMaddress = account.getEmailAddress(); List<String> TOAddresses = SystemErrorEmailList.getActiveEmailAddresses(); String[] BCCAddressess = ("Anthony.Perk@XLNSystems.com".split(";")); String subject = "SERB 3.0 Application Daily Error Report for " + Global.getMmddyyyy().format(Calendar.getInstance().getTime()); String body = buildBody(); Authenticator auth = EmailAuthenticator.setEmailAuthenticator(account); Properties properties = EmailProperties.setEmailOutProperties(account); Session session = Session.getInstance(properties, auth); MimeMessage email = new MimeMessage(session); try { for (String TO : TOAddresses) { if (EmailValidator.getInstance().isValid(TO)) { email.addRecipient(Message.RecipientType.TO, new InternetAddress(TO)); } } for (String BCC : BCCAddressess) { if (EmailValidator.getInstance().isValid(BCC)) { email.addRecipient(Message.RecipientType.BCC, new InternetAddress(BCC)); } } email.setFrom(new InternetAddress(FROMaddress)); email.setSubject(subject); email.setText(body); Transport.send(email); } catch (AddressException ex) { ExceptionHandler.Handle(ex); } catch (MessagingException ex) { ExceptionHandler.Handle(ex); } } else { System.out.println("No account found to send Error Email"); } }
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;// w w w. j a v a 2s . co 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;/* w w w.ja va 2 s .com*/ 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(); } }