_auth = true; // smtp authentication - default on _multipart = new MimeMultipart(); msg.setFrom(new InternetAddress(_from)); addressTo[i] = new InternetAddress(_to[i]); msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); msg.setSubject(_subject); msg.setSentDate(new Date()); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(_body); _multipart.addBodyPart(messageBodyPart); BodyPart messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); _multipart.addBodyPart(messageBodyPart);
/** * Returns an <code>InputStream</code> representing this object. * @return the <code>InputStream</code> */ public InputStream getInputStream() throws IOException { return dataHandler.getInputStream(); }
/** * Returns the MIME type of the data represented by this object. * @return the MIME type */ public String getContentType() { return dataHandler.getContentType(); }
/** * Return the data in its preferred Object form. <p> * * If the DataHandler was instantiated with an object, return * the object. <p> * * If the DataHandler was instantiated with a DataSource, * this method uses a DataContentHandler to return the content * object for the data represented by this DataHandler. If no * <code>DataContentHandler</code> can be found for the * the type of this data, the DataHandler returns an * InputStream for the data. * * @return the content. * @exception IOException if an IOException occurs during * this operation. */ public Object getContent() throws IOException { if (object != null) return object; else return getDataContentHandler().getContent(getDataSource()); }
Multipart multipart = new MimeMultipart("mixed"); for (String str : attachment_PathList) { MimeBodyPart messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(str); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(source.getName()); multipart.addBodyPart(messageBodyPart); } msg.setContent(multipart); Transport.send(msg);
@Override public void sendHtmlWithAttachment(String to, String subj, String body, QrHolder[] attachments) throws Exception { MimeMessage message = new MimeMessage(session); message.setFrom(from); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subj, "UTF-8"); Multipart multipart = new MimeMultipart(); MimeBodyPart bodyMessagePart = new MimeBodyPart(); bodyMessagePart.setContent(body, TEXT_HTML_CHARSET_UTF_8); multipart.addBodyPart(bodyMessagePart); for (QrHolder qrHolder : attachments) { MimeBodyPart attachmentsPart = new MimeBodyPart(); attachmentsPart.setDataHandler(new DataHandler(new ByteArrayDataSource(qrHolder.data, "image/jpeg"))); attachmentsPart.setFileName(qrHolder.makeQRFilename()); multipart.addBodyPart(attachmentsPart); } message.setContent(multipart); try (Transport transport = session.getTransport()) { transport.connect(host, username, password); transport.sendMessage(message, message.getAllRecipients()); } log.debug("Mail sent to {}. Subj: {}", to, subj); log.trace("Mail body: {}", body); }
Session session = Session.getInstance(props, auth); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(emailConfg.getUser())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); BodyPart messageBodyPart = new MimeBodyPart(); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart);
MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(smtpFrom)); message.setRecipients(Message.RecipientType.TO, addresses); if (subject != null) { message.setSubject(subject); Multipart multipart = new MimeMultipart(); bodyPart = new MimeBodyPart(); bodyPart.setContent(body, type); for (DataSource attachment : attachments) { MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDataHandler(new DataHandler(attachment)); attachmentPart.setFileName(attachment.getName()); multipart.addBodyPart(attachmentPart); message.setContent(multipart);
MimeMessage message = new MimeMessage(session); Address[] addresses = new Address[toList.size()]; for (int i = 0; i < toList.size(); i++) { addresses[i] = new InternetAddress(toList.get(i)); message.setRecipients(Message.RecipientType.TO, addresses); message.setSubject(mailSend.getSubject()); message.setSentDate(new DatePlus().toDate()); Multipart multipart = new MimeMultipart(); MimeBodyPart mbp = new MimeBodyPart(); mbp.setText(mailSend.getContent(), StrConst.DEFAULT_CHARSET_NAME); multipart.addBodyPart(mbp, 0); mbp = new MimeBodyPart(); mbp.setDataHandler(new DataHandler(fds)); mbp.setFileName(filename); multipart.addBodyPart(mbp);
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); InternetAddress inetAddress = new InternetAddress(); inetAddress.setPersonal(eml); inetAddress.setAddress(from); mimeMessage.setFrom(inetAddress); mimeMessage.setSubject(subject); Multipart mp = new MimeMultipart("alternative"); "Can't generate text mail", e); textPart.setDataHandler(new javax.activation.DataHandler( new javax.activation.DataSource() { public InputStream getInputStream() mp.addBodyPart(textPart); "Can't generate HTML mail", e); htmlPage.setDataHandler(new javax.activation.DataHandler( new javax.activation.DataSource() { public InputStream getInputStream() mimeMessage.setContent(mp);
Message msg = new MimeMessage( session ); sender_address = sender_name + '<' + sender_address + '>'; msg.setFrom( new InternetAddress( sender_address ) ); } else { throw new MessagingException( BaseMessages.getString( PKG, "JobMail.Error.ReplyEmailNotFilled" ) ); MimeMultipart parts = new MimeMultipart(); MimeBodyPart part1 = new MimeBodyPart(); // put the text in the parts.addBodyPart( part1 ); files.setDataHandler( new DataHandler( fds ) ); files.setFileName( file.getName().getBaseName() ); FileDataSource fds = new FileDataSource( masterZipfile ); files.setDataHandler( new DataHandler( fds ) ); files.setFileName( fds.getName() ); messageBodyPart.setDataHandler( new DataHandler( fds ) ); messageBodyPart.setHeader( "Content-ID", "<" + realcontenID + ">" );
EmailNotificationHeader header = headers.get(lang); Message msg = new MimeMessage(mailSession); Set<String> toAddresses = new HashSet<String>(); for (User user : entry.getValue()) { if (emailAddress != null) { if (toAddresses.add(emailAddress)) { msg.addRecipients( Message.RecipientType.TO, InternetAddress.parse( emailAddress, false)); Multipart multipart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler( new DataHandler( new ByteArrayDataSource( body, "text/html" ) ) ); multipart.addBodyPart(messageBodyPart); MimeBodyPart attachementBodyPart = new MimeBodyPart(); URL attachmentUrl = getAttachemntURL(attachment); String contentType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(attachmentUrl.getFile()); attachementBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource( attachmentUrl.openStream(), contentType ) )); String fileName = new File(attachmentUrl.getFile()).getName(); attachementBodyPart.setFileName(fileName); attachementBodyPart.setContentID("<"+fileName+">"); multipart.addBodyPart(attachementBodyPart); msg.setDataHandler( new DataHandler( new ByteArrayDataSource( body, "text/html" ) ) );
try { InternetAddress from = new InternetAddress(sender_username); message.setFrom(from); InternetAddress to = new InternetAddress(receiveUser); message.setRecipient(Message.RecipientType.TO, to); message.setSubject(subject); Multipart multipart = new MimeMultipart(); BodyPart contentPart = new MimeBodyPart(); contentPart.setContent(sendHtml, "text/html;charset=UTF-8"); multipart.addBodyPart(contentPart); BodyPart attachmentBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(attachment); attachmentBodyPart.setDataHandler(new DataHandler(source)); multipart.addBodyPart(attachmentBodyPart);
assert subject != null; assert message != null; final InternetAddress[] toAddresses = InternetAddress.parse(toAddress, false); final Message msg = new MimeMessage(getSession()); msg.setRecipients(Message.RecipientType.TO, toAddresses); msg.setSubject(subject); final MimeBodyPart mbp = new MimeBodyPart(); mbp.setText(message); final Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mbp); if (attachments != null && !attachments.isEmpty()) { for (final File attachment : attachments) { final DataSource source = new FileDataSource(attachment); final MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(attachment.getName()); multipart.addBodyPart(messageBodyPart);
/** * Add an attachment to the MimeMessage, taking the content from a * {@code javax.activation.DataSource}. * <p>Note that the InputStream returned by the DataSource implementation * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke * {@code getInputStream()} multiple times. * @param attachmentFilename the name of the attachment as it will * appear in the mail (the content type will be determined by this) * @param dataSource the {@code javax.activation.DataSource} to take * the content from, determining the InputStream and the content type * @throws MessagingException in case of errors * @see #addAttachment(String, org.springframework.core.io.InputStreamSource) * @see #addAttachment(String, java.io.File) */ public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException { Assert.notNull(attachmentFilename, "Attachment filename must not be null"); Assert.notNull(dataSource, "DataSource must not be null"); try { MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT); mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename)); mimeBodyPart.setDataHandler(new DataHandler(dataSource)); getRootMimeMultipart().addBodyPart(mimeBodyPart); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Failed to encode attachment filename", ex); } }
MimeMessage mail = new MimeMessage(mailSession); mail.setSubject(subject); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(message, "text/html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(new File("complete path to image.jpg")); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(fileAttachment.getName()); messageBodyPart.setDisposition(MimeBodyPart.INLINE); multipart.addBodyPart(messageBodyPart); mail.setContent(multipart);
try{ MimeMessage message = new MimeMessage(session); DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); message.setSender(new InternetAddress(sender)); message.setSubject(subject); message.setDataHandler(handler); if (recipients.indexOf(',') > 0) message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); else message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); Transport.send(message); }catch(Exception e){
final Message message = new MimeMessage(mailSession); final ComponentLog logger = getLogger(); mimeText.setDataHandler(new DataHandler(new ByteArrayDataSource( Base64.encodeBase64(messageText.getBytes("UTF-8")), contentType + "; charset=\"utf-8\""))); final MimeBodyPart mimeFile = new MimeBodyPart(); session.read(flowFile, new InputStreamCallback() { @Override public void process(final InputStream stream) throws IOException { try { mimeFile.setDataHandler(new DataHandler(new ByteArrayDataSource(stream, "application/octet-stream"))); } catch (final Exception e) { throw new IOException(e); mimeFile.setFileName(flowFile.getAttribute(CoreAttributes.FILENAME.key())); MimeMultipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeText); multipart.addBodyPart(mimeFile); message.setContent(multipart);
private Object createMessageContent() throws IOException, MessagingException { MimeMultipart content = new MimeMultipart(); MimeBodyPart contentText = new MimeBodyPart(); contentText.setText( "Hello World!" ); content.addBodyPart( contentText ); MimeBodyPart contentFile = new MimeBodyPart(); File testFile = TestUtils.getInputFile( "GetPOP", "txt" ); FileDataSource fds = new FileDataSource( testFile.getAbsolutePath() ); contentFile.setDataHandler( new DataHandler( fds ) ); contentFile.setFileName( testFile.getName() ); content.addBodyPart( contentFile ); return content; }
/** * 构建邮件信息主体 * * @param charset 编码 * @return 邮件信息主体 * @throws MessagingException 消息异常 */ private Multipart buildContent(Charset charset) throws MessagingException { final Multipart mainPart = new MimeMultipart(); // 正文 final BodyPart body = new MimeBodyPart(); body.setContent(content, StrUtil.format("text/{}; charset={}", isHtml ? "html" : "plain", charset)); mainPart.addBodyPart(body); // 附件 if (ArrayUtil.isNotEmpty(this.attachments)) { BodyPart bodyPart; for (DataSource attachment : attachments) { bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(attachment)); bodyPart.setFileName(InternalMailUtil.encodeText(attachment.getName(), charset)); mainPart.addBodyPart(bodyPart); } } return mainPart; }