List of usage examples for javax.mail.internet MimeBodyPart setContent
@Override public void setContent(Object o, String type) throws MessagingException
From source file:com.cisco.dbds.utils.report.CustomReport.java
/** * Sendmail./*from w w w . j a va 2s. com*/ * * @param msg the msg * @throws AddressException the address exception * @throws IOException Signals that an I/O exception has occurred. */ public static void sendmail(String msg) throws AddressException, IOException { //Properties CustomReport_CONFIG = new Properties(); //FileInputStream fn = new FileInputStream(System.getProperty("user.dir")+ "/src/it/resources/config.properties"); //CustomReport_CONFIG.load(fn); String from = "automationreportmailer@cisco.com"; // String from = "sitaut@cisco.com"; //String host = System.getProperty("MAIL.SMTP.HOST"); String host = "outbound.cisco.com"; // Mail to details Properties properties = System.getProperties(); properties.setProperty("mail.smtp.host", host); // String ecsip = CustomReport_CONFIG.getProperty("ecsip"); javax.mail.Session session = javax.mail.Session.getDefaultInstance(properties); // compose the message try { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from // )); , "Automation Report Mailer")); //message.addRecipients(Message.RecipientType.TO, System.getProperty("MAIL.TO")); //message.setSubject(System.getProperty("mail.subject")); message.addRecipients(Message.RecipientType.TO, "maparame@cisco.com"); message.setSubject("VCS consle report"); Multipart mp = new MimeMultipart(); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(msg, "text/html"); mp.addBodyPart(htmlPart); message.setContent(mp); Transport.send(message); System.out.println(msg); System.out.println("message sent successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } catch (Exception e) { System.out.println("\tException in sending mail to configured mailers list"); } }
From source file:org.latticesoft.util.resource.MessageUtil.java
/** * Sends the email.//w w w. j a v a2s .c o m * @param info the EmailInfo containing the message and other details * @param p the properties to set in the environment when instantiating the session * @param auth the authenticator */ public static void sendMail(EmailInfo info, Properties p, Authenticator auth) { try { if (p == null) { if (log.isErrorEnabled()) { log.error("Null properties!"); } return; } Session session = Session.getInstance(p, auth); session.setDebug(true); if (log.isInfoEnabled()) { log.info(p); log.info(session); } MimeMessage mimeMessage = new MimeMessage(session); if (log.isInfoEnabled()) { log.info(mimeMessage); log.info(info.getFromAddress()); } mimeMessage.setFrom(info.getFromAddress()); mimeMessage.setSentDate(new Date()); List l = info.getToList(); if (l != null) { for (int i = 0; i < l.size(); i++) { String addr = (String) l.get(i); if (log.isInfoEnabled()) { log.info(addr); } mimeMessage.addRecipients(Message.RecipientType.TO, addr); } } l = info.getCcList(); if (l != null) { for (int i = 0; i < l.size(); i++) { String addr = (String) l.get(i); mimeMessage.addRecipients(Message.RecipientType.CC, addr); } } l = info.getBccList(); if (l != null) { for (int i = 0; i < l.size(); i++) { String addr = (String) l.get(i); mimeMessage.addRecipients(Message.RecipientType.BCC, addr); } } if (info.getAttachment().size() == 0) { if (info.getCharSet() != null) { mimeMessage.setSubject(info.getSubject(), info.getCharSet()); mimeMessage.setText(info.getContent(), info.getCharSet()); } else { mimeMessage.setSubject(info.getSubject()); mimeMessage.setText(info.getContent()); } mimeMessage.setContent(info.getContent(), info.getContentType()); } else { if (info.getCharSet() != null) { mimeMessage.setSubject(info.getSubject(), info.getCharSet()); } else { mimeMessage.setSubject(info.getSubject()); } Multipart mp = new MimeMultipart(); MimeBodyPart body = new MimeBodyPart(); if (info.getCharSet() != null) { body.setText(info.getContent(), info.getCharSet()); body.setContent(info.getContent(), info.getContentType()); } else { body.setText(info.getContent()); body.setContent(info.getContent(), info.getContentType()); } mp.addBodyPart(body); for (int i = 0; i < info.getAttachment().size(); i++) { String filename = (String) info.getAttachment().get(i); MimeBodyPart attachment = new MimeBodyPart(); FileDataSource fds = new FileDataSource(filename); attachment.setDataHandler(new DataHandler(fds)); attachment.setFileName(MimeUtility.encodeWord(fds.getName())); mp.addBodyPart(attachment); } mimeMessage.setContent(mp); } Transport.send(mimeMessage); } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Error in sending email", e); } } }
From source file:mitm.common.mail.BodyPartUtils.java
/** * Creates a MimeBodyPart with the provided message attached as a RFC822 attachment. *//*from ww w . j a va 2 s . c o m*/ public static MimeBodyPart toRFC822(MimeMessage message, String filename) throws MessagingException { MimeBodyPart bodyPart = new MimeBodyPart(); bodyPart.setContent(message, "message/rfc822"); /* somehow the content-Type header is not set so we need to set it ourselves */ bodyPart.setHeader("Content-Type", "message/rfc822"); bodyPart.setDisposition(Part.INLINE); bodyPart.setFileName(filename); return bodyPart; }
From source file:mitm.common.mail.BodyPartUtils.java
public static MimeBodyPart makeContentBodyPart(MimePart sourcePart, HeaderMatcher matcher) throws MessagingException, IOException { MimeBodyPart newBodyPart = new MimeBodyPart(); newBodyPart.setContent(sourcePart.getContent(), sourcePart.getContentType()); HeaderUtils.copyHeaders(sourcePart, newBodyPart, matcher); return newBodyPart; }
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);/*w w w . j a v a2 s . com*/ 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:javamailclient.GmailAPI.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.//from w w w . j av a 2s.c o m * @param bodyText Body text of the email. * @param fileDir Path to the directory containing attachment. * @param filename Name of file to be attached. * @return MimeMessage to be used to send email. * @throws MessagingException */ public static MimeMessage createEmailWithAttachment(String to, String from, String subject, String bodyText, String fileDir, String filename) throws MessagingException, IOException { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage email = new MimeMessage(session); InternetAddress tAddress = new InternetAddress(to); InternetAddress fAddress = new InternetAddress(from); email.setFrom(fAddress); email.addRecipient(javax.mail.Message.RecipientType.TO, tAddress); email.setSubject(subject); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(bodyText, "text/plain"); mimeBodyPart.setHeader("Content-Type", "text/plain; charset=\"UTF-8\""); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); mimeBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(fileDir + filename); mimeBodyPart.setDataHandler(new DataHandler(source)); mimeBodyPart.setFileName(filename); String contentType = Files.probeContentType(FileSystems.getDefault().getPath(fileDir, filename)); mimeBodyPart.setHeader("Content-Type", contentType + "; name=\"" + filename + "\""); mimeBodyPart.setHeader("Content-Transfer-Encoding", "base64"); multipart.addBodyPart(mimeBodyPart); email.setContent(multipart); return email; }
From source file:no.kantega.publishing.modules.mailsender.MailSender.java
/** * Helper method to create a MimeBodyPart from a string. * * @param content The string to insert into the MimeBodyPart. * @return The resulting MimeBodyPart./* w w w .ja va 2s . c o m*/ * @throws SystemException if the MimeBodyPart can't be created. */ public static MimeBodyPart createMimeBodyPartFromStringMessage(String content) throws SystemException { try { MimeBodyPart bp = new MimeBodyPart(); if (content.toLowerCase().contains("<html>")) { log.debug("Set content as text/html"); bp.setContent(content, "text/html; charset=iso-8859-1"); } else { log.debug("Set content as text/plain"); bp.setText(content, "ISO-8859-1"); bp.setHeader("Content-Transfer-Encoding", "quoted-printable"); } return bp; } catch (MessagingException e) { throw new SystemException("Feil ved generering av MimeBodyPart fra string", e); } }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * /*from w w w . j a va 2s . c o m*/ * @param subject the subject of the new mail * @param htmlMessage the content of the mail as html * @param alternativeTextMessage the content of the mail as text * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from, String to, String cc, String bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && !cc.equals("")) msg.setRecipients(Message.RecipientType.CC, cc); if (bcc != null && !bcc.equals("")) msg.setRecipients(Message.RecipientType.BCC, bcc); msg.addRecipients(Message.RecipientType.TO, to); msg.setSubject(subject, "ISO8859_1"); msg.setSentDate(new Date()); MimeMultipart multiPart = new MimeMultipart("alternative"); MimeBodyPart htmlPart = new MimeBodyPart(); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(alternativeTextMessage, "ISO8859_1"); textPart.setHeader("MIME-Version", "1.0"); //textPart.setHeader("Content-Type", textPart.getContentType()); textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\""); htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\""); htmlPart.setHeader("MIME-Version", "1.0"); htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\""); //htmlPart.setHeader("Content-Type", htmlPart.getContentType()); multiPart.addBodyPart(textPart); multiPart.addBodyPart(htmlPart); multiPart.setSubType("alternative"); msg.setContent(multiPart); msg.setHeader("MIME-Version", "1.0"); msg.setHeader("Content-Type", multiPart.getContentType()); Transport.send(msg); } catch (Exception e) { log.error("Error sending html-mail: " + e.getLocalizedMessage()); } }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * /*from w ww.j av a2 s .c o m*/ * @param subject the subject of the new mail * @param htmlMessage the content of the mail as html * @param alternativeTextMessage the content of the mail as text * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from, String[] to, String[] cc, String[] bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && cc.length != 0) { for (int i = cc.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.CC, cc[i]); } } if (bcc != null && bcc.length != 0) { for (int i = bcc.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.BCC, bcc[i]); } } if (to != null && to.length != 0) { for (int i = to.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.TO, to[i]); } } msg.setSubject(subject, "ISO8859_1"); msg.setSentDate(new Date()); MimeMultipart multiPart = new MimeMultipart("alternative"); MimeBodyPart htmlPart = new MimeBodyPart(); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(alternativeTextMessage, "ISO8859_1"); textPart.setHeader("MIME-Version", "1.0"); //textPart.setHeader("Content-Type", textPart.getContentType()); textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\""); htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\""); htmlPart.setHeader("MIME-Version", "1.0"); htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\""); //htmlPart.setHeader("Content-Type", htmlPart.getContentType()); multiPart.addBodyPart(textPart); multiPart.addBodyPart(htmlPart); multiPart.setSubType("alternative"); msg.setContent(multiPart); msg.setHeader("MIME-Version", "1.0"); msg.setHeader("Content-Type", multiPart.getContentType()); Transport.send(msg); } catch (Exception e) { log.error("Error sending html-mail: " + e.getLocalizedMessage()); } }
From source file:org.apache.hupa.server.handler.AbstractSendMessageHandler.java
/** * Fill the body of a message already created. * The result message depends on the information given. * /*from w ww. j a va 2 s.co m*/ * @param message * @param text * @param html * @param parts * @return The composed message * @throws MessagingException * @throws IOException */ @SuppressWarnings("rawtypes") public static Message composeMessage(Message message, String text, String html, List parts) throws MessagingException, IOException { MimeBodyPart txtPart = null; MimeBodyPart htmlPart = null; MimeMultipart mimeMultipart = null; if (text == null && html == null) { text = ""; } if (text != null) { txtPart = new MimeBodyPart(); txtPart.setContent(text, "text/plain"); } if (html != null) { htmlPart = new MimeBodyPart(); htmlPart.setContent(html, "text/html"); } if (html != null && text != null) { mimeMultipart = new MimeMultipart(); mimeMultipart.setSubType("alternative"); mimeMultipart.addBodyPart(txtPart); mimeMultipart.addBodyPart(htmlPart); } if (parts == null || parts.isEmpty()) { if (mimeMultipart != null) { message.setContent(mimeMultipart); } else if (html != null) { message.setText(html); message.setHeader("Content-type", "text/html"); } else if (text != null) { message.setText(text); } } else { MimeBodyPart bodyPart = new MimeBodyPart(); if (mimeMultipart != null) { bodyPart.setContent(mimeMultipart); } else if (html != null) { bodyPart.setText(html); bodyPart.setHeader("Content-type", "text/html"); } else if (text != null) { bodyPart.setText(text); } Multipart multipart = new MimeMultipart(); multipart.addBodyPart(bodyPart); for (Object attachment : parts) { if (attachment instanceof FileItem) { multipart.addBodyPart(MessageUtils.fileitemToBodypart((FileItem) attachment)); } else { multipart.addBodyPart((BodyPart) attachment); } } message.setContent(multipart); } message.saveChanges(); return message; }