List of usage examples for javax.mail Multipart addBodyPart
public synchronized void addBodyPart(BodyPart part) throws MessagingException
From source file:MainClass.java
public static void main(String[] args) { if (args.length != 4) { System.out.println("usage: java msgmultisend <to> <from> <smtp> true|false"); return;/*from ww w . j a v a 2 s .c o m*/ } String to = args[0]; String from = args[1]; String host = args[2]; boolean debug = Boolean.valueOf(args[3]).booleanValue(); // create some properties and get the default Session Properties props = new Properties(); props.put("mail.smtp.host", host); Session session = Session.getInstance(props, null); session.setDebug(debug); try { // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = { new InternetAddress(to) }; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject("JavaMail APIs Multipart Test"); msg.setSentDate(new Date()); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(msgText1); // create and fill the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // Use setText(text, charset), to show it off ! mbp2.setText(msgText2, "us-ascii"); // create the Multipart and its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); // send the message Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); } } }
From source file:sendfile.java
public static void main(String[] args) { if (args.length != 5) { System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false"); System.exit(1);/*from w ww . ja v a 2 s. c om*/ } String to = args[0]; String from = args[1]; String host = args[2]; String filename = args[3]; boolean debug = Boolean.valueOf(args[4]).booleanValue(); String msgText1 = "Sending a file.\n"; String subject = "Sending a file"; // create some properties and get the default Session Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getInstance(props, null); session.setDebug(debug); try { // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = { new InternetAddress(to) }; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(msgText1); // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message FileDataSource fds = new FileDataSource(filename); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); // create the Multipart and add its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); // set the Date: header msg.setSentDate(new Date()); // send the message Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); } } }
From source file:sendfile.java
public static void main(String[] args) { if (args.length != 5) { System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false"); System.exit(1);//from www .ja va 2 s . c o m } String to = args[0]; String from = args[1]; String host = args[2]; String filename = args[3]; boolean debug = Boolean.valueOf(args[4]).booleanValue(); String msgText1 = "Sending a file.\n"; String subject = "Sending a file"; // create some properties and get the default Session Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getInstance(props, null); session.setDebug(debug); try { // create a message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); InternetAddress[] address = { new InternetAddress(to) }; msg.setRecipients(Message.RecipientType.TO, address); msg.setSubject(subject); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText(msgText1); // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message mbp2.attachFile(filename); /* * Use the following approach instead of the above line if * you want to control the MIME type of the attached file. * Normally you should never need to do this. * FileDataSource fds = new FileDataSource(filename) { public String getContentType() { return "application/octet-stream"; } }; mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); */ // create the Multipart and add its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); // set the Date: header msg.setSentDate(new Date()); /* * If you want to control the Content-Transfer-Encoding * of the attached file, do the following. Normally you * should never need to do this. * msg.saveChanges(); mbp2.setHeader("Content-Transfer-Encoding", "base64"); */ // send the message Transport.send(msg); } catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); } } catch (IOException ioex) { ioex.printStackTrace(); } }
From source file:org.apache.usergrid.apm.service.util.Mailer.java
public static void send(String recipeintEmail, String subject, String messageText) { /*/*from w w w . j a va2s . c o m*/ * 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:bo.com.offercruzmail.imp.FormadorMensajes.java
public static Multipart enviarEntidadNoExiste(String id) throws MessagingException { Multipart cuerpo = new MimeMultipart(); cuerpo.addBodyPart(FormadorMensajes.getBodyPartEnvuelto("El regitro con Id <b>" + id + "</b> no existe")); return cuerpo; }
From source file:bo.com.offercruzmail.imp.FormadorMensajes.java
public static Multipart enviarErrorInesperado() throws MessagingException { Multipart cuerpo = new MimeMultipart(); cuerpo.addBodyPart(FormadorMensajes .getBodyPartEnvuelto("Ha ocurrido un error inesperado, por favor intentelo nuevamente")); return cuerpo; }
From source file:bo.com.offercruzmail.imp.FormadorMensajes.java
public static Multipart enviarModificacionExitosa() throws MessagingException { Multipart cuerpo = new MimeMultipart(); cuerpo.addBodyPart(FormadorMensajes .getBodyPartEnvuelto(escapeHtml4("Se ha efectuado la actualizacin exitosamente"))); return cuerpo; }
From source file:bo.com.offercruzmail.imp.FormadorMensajes.java
public static Multipart getMensajeUsuarioNoRegistrado() throws MessagingException { Multipart multiPartes = new MimeMultipart(); multiPartes.addBodyPart(FormadorMensajes .getBodyPartEnvuelto(escapeHtml4("Lo siento no est registrado para poder usar este sistema"))); return multiPartes; }
From source file:com.medsavant.mailer.Mail.java
public synchronized static boolean sendEmail(String to, String subject, String text, File attachment) { try {/*from w w w .j a v a 2s. c o m*/ 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.caarray.util.EmailUtil.java
private static void addBodyPart(Multipart mp, String content, String contentType) throws MessagingException { BodyPart bp = new MimeBodyPart(); bp.setContent(content, contentType); mp.addBodyPart(bp); }