List of usage examples for javax.mail.internet MimeMultipart MimeMultipart
public MimeMultipart()
From source file:app.logica.gestores.GestorEmail.java
/** * Create a MimeMessage using the parameters provided. * * @param to//from w w w . ja v a2s. c o m * Email address of the receiver. * @param from * Email address of the sender, the mailbox account. * @param subject * Subject of the email. * @param bodyText * Body text of the email. * @param file * Path to the file to be attached. * @return MimeMessage to be used to send email. * @throws MessagingException */ private MimeMessage createEmailWithAttachment(String to, String from, String subject, String bodyText, File file) throws MessagingException, IOException { 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); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(bodyText, "text/plain"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); mimeBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(file); mimeBodyPart.setDataHandler(new DataHandler(source)); mimeBodyPart.setFileName(file.getName()); multipart.addBodyPart(mimeBodyPart); email.setContent(multipart); return email; }
From source file:no.kantega.publishing.modules.mailsender.MailSender.java
/** * Sends a mail message. The content must be provided as MimeBodyPart objects. * * @param from Sender's email address. * @param to Recipient's email address. * @param cc Email address for CC. * @param bcc Email address for BCC. * @param subject Subject text for the email. * @param bodyParts The body parts to insert into the message. * @throws SystemException if an unexpected error occurs. * @throws ConfigurationException if a configuration error occurs. *//*from w w w. ja v a 2 s.co m*/ public static void send(String from, String to, String cc, String bcc, String subject, MimeBodyPart[] bodyParts) throws ConfigurationException, SystemException { initEventLog(); try { Properties props = new Properties(); Configuration config = Aksess.getConfiguration(); String host = config.getString("mail.host"); if (host == null) { throw new ConfigurationException("mail.host"); } // I noen tilfeller nsker vi at all epost skal g til en testadresse String catchAllTo = config.getString("mail.catchall.to"); boolean catchallExists = catchAllTo != null && catchAllTo.contains("@"); if (catchallExists) { StringBuilder prefix = new StringBuilder(" (original recipient: " + to); if (cc != null) { prefix.append(", cc: ").append(cc); } if (bcc != null) { prefix.append(", bcc: ").append(bcc); } prefix.append(") "); subject = prefix + subject; to = catchAllTo; cc = null; bcc = null; } props.setProperty("mail.smtp.host", host); Session session = Session.getDefaultInstance(props); boolean debug = config.getBoolean("mail.debug", false); if (debug) { session.setDebug(true); } // Opprett message, sett attributter MimeMessage message = new MimeMessage(session); InternetAddress fromAddress = new InternetAddress(from); InternetAddress toAddress[] = InternetAddress.parse(to); message.setFrom(fromAddress); if (toAddress.length > 1) { message.setRecipients(Message.RecipientType.BCC, toAddress); } else { message.setRecipients(Message.RecipientType.TO, toAddress); } if (cc != null) { message.addRecipients(Message.RecipientType.CC, cc); } if (bcc != null) { message.addRecipients(Message.RecipientType.BCC, bcc); } message.setSubject(subject, "ISO-8859-1"); message.setSentDate(new Date()); Multipart mp = new MimeMultipart(); for (MimeBodyPart bodyPart : bodyParts) { mp.addBodyPart(bodyPart); } message.setContent(mp); // Send meldingen Transport.send(message); eventLog.log("System", null, Event.EMAIL_SENT, to + ":" + subject, null); // Logg sending log.info("Sending email to " + to + " with subject " + subject); } catch (MessagingException e) { String errormessage = "Subject: " + subject + " | Error: " + e.getMessage(); eventLog.log("System", null, Event.FAILED_EMAIL_SUBMISSION, errormessage + " | to: " + to, null); log.error("Error sending mail", e); throw new SystemException("Error sending email to : " + to + " with subject " + subject, e); } }
From source file:com.duroty.utils.mail.MessageUtilities.java
/** * DOCUMENT ME!//from ww w . j a v a 2s . c o m * * @param from DOCUMENT ME! * @param to DOCUMENT ME! * @param subjectPrefix DOCUMENT ME! * @param subjectSuffix DOCUMENT ME! * @param msgText DOCUMENT ME! * @param message DOCUMENT ME! * @param session DOCUMENT ME! * * @return DOCUMENT ME! * * @throws Exception DOCUMENT ME! * @throws IllegalArgumentException DOCUMENT ME! */ public static MimeMessage createNewMessage(Address from, Address[] to, String subjectPrefix, String subjectSuffix, String msgText, MimeMessage message, Session session) throws Exception { if (from == null) { throw new IllegalArgumentException("from addres cannot be null."); } if ((to == null) || (to.length <= 0)) { throw new IllegalArgumentException("to addres cannot be null."); } if (message == null) { throw new IllegalArgumentException("to message cannot be null."); } try { //Create the message MimeMessage newMessage = new MimeMessage(session); StringBuffer buffer = new StringBuffer(); if (subjectPrefix != null) { buffer.append(subjectPrefix); } if (message.getSubject() != null) { buffer.append(message.getSubject()); } if (subjectSuffix != null) { buffer.append(subjectSuffix); } if (buffer.length() > 0) { newMessage.setSubject(buffer.toString()); } newMessage.setFrom(from); newMessage.addRecipients(Message.RecipientType.TO, to); //Create your new message part BodyPart messageBodyPart1 = new MimeBodyPart(); messageBodyPart1.setText(msgText); //Create a multi-part to combine the parts Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart1); //Create and fill part for the forwarded content BodyPart messageBodyPart2 = new MimeBodyPart(); messageBodyPart2.setDataHandler(message.getDataHandler()); //Add part to multi part multipart.addBodyPart(messageBodyPart2); //Associate multi-part with message newMessage.setContent(multipart); newMessage.saveChanges(); return newMessage; } finally { } }
From source file:com.adaptris.mail.MailSenderImp.java
/** * Build the content of the MimeMessage from the constituent parts. *//*from www .j a v a 2s. co m*/ protected void buildContent() throws MailException { checkSession(); try { if (ArrayUtils.isEmpty(message.getFrom())) { message.setFrom(); } if (ArrayUtils.isEmpty(message.getAllRecipients())) { throw new MailException("Mail message has no recipients"); } message.setSentDate(new Date()); MimeMultipart body = new MimeMultipart(); addAttachmentsToMessage(body); addEmailBody(body); message.setContent(body); message.saveChanges(); } catch (MessagingException e) { throw new MailException(e); } }
From source file:org.pentaho.platform.scheduler2.email.Emailer.java
public boolean send() { String from = props.getProperty("mail.from.default"); String fromName = props.getProperty("mail.from.name"); String to = props.getProperty("to"); String cc = props.getProperty("cc"); String bcc = props.getProperty("bcc"); boolean authenticate = "true".equalsIgnoreCase(props.getProperty("mail.smtp.auth")); String subject = props.getProperty("subject"); String body = props.getProperty("body"); logger.info("Going to send an email to " + to + " from " + from + " with the subject '" + subject + "' and the body " + body); try {/*from w ww . j a v a2 s. c o m*/ // Get a Session object Session session; if (authenticate) { session = Session.getInstance(props, authenticator); } else { session = Session.getInstance(props); } // if debugging is not set in the email config file, then default to false if (!props.containsKey("mail.debug")) { //$NON-NLS-1$ session.setDebug(false); } // construct the message MimeMessage msg = new MimeMessage(session); Multipart multipart = new MimeMultipart(); if (from != null) { msg.setFrom(new InternetAddress(from, fromName)); } else { // There should be no way to get here logger.error("Email.ERROR_0012_FROM_NOT_DEFINED"); //$NON-NLS-1$ } if ((to != null) && (to.trim().length() > 0)) { msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); } if ((cc != null) && (cc.trim().length() > 0)) { msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false)); } if ((bcc != null) && (bcc.trim().length() > 0)) { msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false)); } if (subject != null) { msg.setSubject(subject, LocaleHelper.getSystemEncoding()); } if (attachment == null) { logger.error("Email.ERROR_0015_ATTACHMENT_FAILED"); //$NON-NLS-1$ return false; } ByteArrayDataSource dataSource = new ByteArrayDataSource(attachment, attachmentMimeType); if (body != null) { MimeBodyPart bodyMessagePart = new MimeBodyPart(); bodyMessagePart.setText(body, LocaleHelper.getSystemEncoding()); multipart.addBodyPart(bodyMessagePart); } // attach the file to the message MimeBodyPart attachmentBodyPart = new MimeBodyPart(); attachmentBodyPart.setDataHandler(new DataHandler(dataSource)); attachmentBodyPart.setFileName(MimeUtility.encodeText(attachmentName, "UTF-8", null)); multipart.addBodyPart(attachmentBodyPart); // add the Multipart to the message msg.setContent(multipart); msg.setHeader("X-Mailer", Emailer.MAILER); //$NON-NLS-1$ msg.setSentDate(new Date()); Transport.send(msg); return true; } catch (SendFailedException e) { logger.error("Email.ERROR_0011_SEND_FAILED -" + to, e); //$NON-NLS-1$ } catch (AuthenticationFailedException e) { logger.error("Email.ERROR_0014_AUTHENTICATION_FAILED - " + to, e); //$NON-NLS-1$ } catch (Throwable e) { logger.error("Email.ERROR_0011_SEND_FAILED - " + to, e); //$NON-NLS-1$ } return false; }
From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java
public void attach(BodyPart createNewAttach) { try {/*from ww w . j a v a2 s. c om*/ Object content = getMail().getMessage().getContent(); if (content instanceof Multipart) { Multipart multi = (Multipart) content; multi.addBodyPart(createNewAttach); } else { Multipart multiPart = new MimeMultipart(); multiPart.addBodyPart(createNewAttach); getMail().getMessage().setContent(multiPart); } getMail().getMessage().saveChanges(); } catch (Exception e) { log.error("Could not attach html for the email. EmailId: " + getMessagePlatformId() + " - " + e.getClass().getName() + " - " + e.getMessage()); throw new RuntimeException("Could not attach html for email " + getMessagePlatformId()); } }
From source file:io.mapzone.arena.share.app.EMailSharelet.java
private void sendEmail(final String toText, final String subjectText, final String messageText, final boolean withAttachment, final ImagePngContent image) throws Exception { MimeMessage msg = new MimeMessage(mailSession()); msg.addRecipients(RecipientType.TO, InternetAddress.parse(toText, false)); // TODO we need the FROM from the current user msg.addFrom(InternetAddress.parse("support@mapzone.io")); //ArenaConfigMBean.SMTP_USER ) ); msg.setReplyTo(InternetAddress.parse("DO_NOT_REPLY_TO_THIS_EMAIL@mapzone.io")); //ArenaConfigMBean.SMTP_USER ) ); msg.setSubject(subjectText, "utf-8"); if (withAttachment) { // add mime multiparts Multipart multipart = new MimeMultipart(); BodyPart part = new MimeBodyPart(); part.setText(messageText);/*from w w w .j a va 2s .com*/ multipart.addBodyPart(part); // Second part is attachment part = new MimeBodyPart(); part.setDataHandler(new DataHandler(new URLDataSource(new URL(image.imgResource)))); part.setFileName("preview.png"); part.setHeader("Content-ID", "preview"); multipart.addBodyPart(part); // // third part in HTML with embedded image // part = new MimeBodyPart(); // part.setContent( "<img src='cid:preview'>", "text/html" ); // multipart.addBodyPart( part ); msg.setContent(multipart); } else { msg.setText(messageText, "utf-8"); } msg.setSentDate(new Date()); Transport.send(msg); }
From source file:org.fogbowcloud.manager.core.plugins.util.CloudInitUserDataBuilder.java
private CloudInitUserDataBuilder(Charset charset) { super();//from w w w . j a v a 2 s. c o m userDataMimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties())); userDataMultipart = new MimeMultipart(); try { userDataMimeMessage.setContent(userDataMultipart); } catch (MessagingException e) { throw Throwables.propagate(e); } this.charset = Preconditions.checkNotNull(charset, "'charset' can NOT be null"); }
From source file:fr.xebia.cloud.cloudinit.CloudInitUserDataBuilder.java
private CloudInitUserDataBuilder(@Nonnull Charset charset) { super();// w w w .j av a2 s. c o m userDataMimeMessage = new MimeMessage(Session.getDefaultInstance(new Properties())); userDataMultipart = new MimeMultipart(); try { userDataMimeMessage.setContent(userDataMultipart); } catch (MessagingException e) { throw Throwables.propagate(e); } this.charset = Preconditions.checkNotNull(charset, "'charset' can NOT be null"); }
From source file:rescustomerservices.GmailQuickstart.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 . ja v a2 s .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 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; }