use of javax.mail.internet.MimeMultipart in project ddf by codice.
the class SmtpClientImplITCase method testSend.
@Test
public void testSend() throws IOException, MessagingException, ExecutionException, InterruptedException {
int port = findAvailablePort();
SimpleSmtpServer server = SimpleSmtpServer.start(port);
SmtpClientImpl emailService = new SmtpClientImpl();
emailService.setHostName(HOSTNAME);
emailService.setPortNumber(port);
Session session = emailService.createSession();
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(FROM_ADDR));
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_ADDR));
mimeMessage.setSubject(SUBJECT);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(BODY);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
mimeMessage.setContent(multipart);
emailService.send(mimeMessage).get();
server.stop();
assertThat(server.getReceivedEmailSize(), is(1));
Iterator emailIterator = server.getReceivedEmail();
SmtpMessage email = (SmtpMessage) emailIterator.next();
assertThat(email.getHeaderValue(SUBJECT_HEADER), is(SUBJECT));
assertThat(email.getHeaderValue(FROM_HEADER), containsString(FROM_ADDR));
assertThat(email.getHeaderValue(TO_HEADER), containsString(TO_ADDR));
assertThat(email.getBody(), containsString(BODY));
}
use of javax.mail.internet.MimeMultipart in project JMRI by JMRI.
the class MailMessage method prepare.
/**
* sets up needed parts for sending email message presumes any needed sets
* have been done first
*/
public void prepare() {
try {
Properties props = System.getProperties();
props.put("mail.transport.protocol", pProtocol);
props.put("mail.smtp.starttls.enable", pTls);
if (mailhost != null) {
props.put("mail.smtp.host", mailhost);
}
props.put("mail.smtp.auth", pAuth);
Authenticator auth = new SMTPAuthenticator();
// Get a Session object
session = Session.getInstance(props, auth);
if (log.isDebugEnabled()) {
session.setDebug(true);
}
// construct the message
msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
if (cc != null) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
}
if (bcc != null) {
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
}
msg.setSubject(subject);
// We need a multipart message to hold attachment.
mp = new MimeMultipart();
} catch (MessagingException e) {
log.warn("Exception in prepare", e);
}
}
use of javax.mail.internet.MimeMultipart in project webservices-axiom by apache.
the class XOPSample method getInlinedMessage.
public final InputStream getInlinedMessage() {
try {
MimeMultipart mp = getMultipart();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
Document rootPart = documentBuilderFactory.newDocumentBuilder().parse(mp.getBodyPart(0).getInputStream());
process(rootPart.getDocumentElement(), mp);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(rootPart), new StreamResult(baos));
return new ByteArrayInputStream(baos.toByteArray());
} catch (Exception ex) {
throw new Error(ex);
}
}
use of javax.mail.internet.MimeMultipart in project webservices-axiom by apache.
the class AbstractMultipartWriterTest method test.
private void test(String contentTransferEncoding) throws Exception {
Random random = new Random();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MultipartWriter mpw = factory.createMultipartWriter(baos, UIDGenerator.generateMimeBoundary());
byte[] content = new byte[8192];
random.nextBytes(content);
OutputStream partOutputStream = mpw.writePart("application/octet-stream", contentTransferEncoding, UIDGenerator.generateContentId());
partOutputStream.write(content);
partOutputStream.close();
mpw.complete();
MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(baos.toByteArray()));
assertEquals(1, mp.getCount());
MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(0);
assertEquals(contentTransferEncoding, bp.getHeader("Content-Transfer-Encoding")[0]);
baos.reset();
bp.getDataHandler().writeTo(baos);
assertTrue(Arrays.equals(content, baos.toByteArray()));
}
use of javax.mail.internet.MimeMultipart in project webservices-axiom by apache.
the class AttachmentsTest method testTurkishLocale.
private void testTurkishLocale(String contentIDHeaderName) throws Exception {
Locale locale = Locale.getDefault();
Locale.setDefault(new Locale("tr", "TR"));
try {
MimeMessage message = new MimeMessage((Session) null);
MimeMultipart mp = new MimeMultipart("related");
MimeBodyPart bp1 = new MimeBodyPart();
bp1.setText("<root/>", "utf-8", "xml");
bp1.addHeader("Content-Transfer-Encoding", "binary");
mp.addBodyPart(bp1);
MimeBodyPart bp2 = new MimeBodyPart();
byte[] content = new byte[8192];
new Random().nextBytes(content);
bp2.setDataHandler(new DataHandler("Test", "text/plain"));
bp2.addHeader("Content-Transfer-Encoding", "binary");
bp2.addHeader(contentIDHeaderName, "part@apache.org");
mp.addBodyPart(bp2);
message.setContent(mp);
message.saveChanges();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mp.writeTo(baos);
String contentType = message.getContentType();
InputStream in = new ByteArrayInputStream(baos.toByteArray());
Attachments attachments = new Attachments(in, contentType);
assertNotNull(attachments.getDataHandler("part@apache.org"));
} finally {
Locale.setDefault(locale);
}
}
Aggregations