use of org.exoplatform.services.mail.Attachment in project kernel by exoplatform.
the class TestMailService method testSendMessage.
public void testSendMessage() throws Exception {
Message message = new Message();
message.setFrom(generateRandomEmailSender());
message.setTo(generateRandomEmailRecipient());
message.setCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
message.setBCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
message.setSubject(MAIL_SUBJECT);
message.setBody(MAIL_CONTENTS);
message.setMimeType(TEXT_HTML);
Attachment attachment = new Attachment();
attachment.setInputStream(new ByteArrayInputStream(ATTACHMENT.getBytes()));
attachment.setMimeType(TEXT_PLAIN);
message.addAttachment(attachment);
assertEquals("SMTP server should be now empty", 0, mailServer.getMessages().size());
assertFalse(isEmailMessageSent(MAIL_SUBJECT));
service.sendMessage(message);
Thread.sleep(ONE_SECOND);
assertEquals("SMTP server should have one message", 5, mailServer.getMessages().size());
assertTrue(isEmailMessageSent(MAIL_SUBJECT));
}
use of org.exoplatform.services.mail.Attachment in project kernel by exoplatform.
the class TestMailService method testSendMessageAsynchExceptionCause.
/**
* Here we test asynchronous email sending of {@link Message}.
* We check if we can get real cause of exception, if that occurs during
* message sending process.
*/
public void testSendMessageAsynchExceptionCause() throws Exception {
Attachment attachment = new Attachment();
attachment.setInputStream(new ByteArrayInputStream(ATTACHMENT.getBytes()));
attachment.setMimeType(TEXT_PLAIN);
Message message = new Message();
message.setFrom("!@#$%^&*()");
message.setTo(generateRandomEmailRecipient());
message.setCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
message.setBCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
message.setSubject(MAIL_SUBJECT);
message.setBody(MAIL_CONTENTS);
message.setMimeType(TEXT_HTML);
message.addAttachment(attachment);
Future<Boolean> future = service.sendMessageInFuture(message);
try {
future.get();
fail();
} catch (ExecutionException ee) {
assertEquals("We tried to send mail with malformed sender field," + " so we expect an AdressException to be the real cause of ExecutionException", ee.getCause().getClass(), AddressException.class);
}
}
use of org.exoplatform.services.mail.Attachment in project kernel by exoplatform.
the class MailServiceImpl method sendMessage.
/**
* {@inheritDoc}
*/
public void sendMessage(Message message) throws Exception {
MimeMessage mimeMessage = new MimeMessage(getMailSession());
String FROM = message.getFrom();
String TO = message.getTo();
String CC = message.getCC();
String BCC = message.getBCC();
String subject = message.getSubject();
String mimeType = message.getMimeType();
String body = message.getBody();
List<Attachment> attachment = message.getAttachment();
// set From to the message
if (FROM != null && !FROM.equals("")) {
InternetAddress sentFrom = new InternetAddress(FROM);
if (sentFrom.getPersonal() != null) {
sentFrom.setPersonal(sentFrom.getPersonal(), "UTF-8");
}
mimeMessage.setFrom(sentFrom);
}
// set To to the message
InternetAddress[] sendTo = new InternetAddress[getArrs(TO).length];
for (int i = 0; i < getArrs(TO).length; i++) {
sendTo[i] = new InternetAddress(getArrs(TO)[i]);
if (sendTo[i].getPersonal() != null) {
sendTo[i].setPersonal(sendTo[i].getPersonal(), "UTF-8");
}
}
mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, sendTo);
// set CC to the message
if ((getArrs(CC) != null) && (getArrs(CC).length > 0)) {
InternetAddress[] copyTo = new InternetAddress[getArrs(CC).length];
for (int i = 0; i < getArrs(CC).length; i++) {
copyTo[i] = new InternetAddress(getArrs(CC)[i]);
if (copyTo[i].getPersonal() != null) {
copyTo[i].setPersonal(copyTo[i].getPersonal(), "UTF-8");
}
}
mimeMessage.setRecipients(javax.mail.Message.RecipientType.CC, copyTo);
}
// set BCC to the message
if ((getArrs(BCC) != null) && (getArrs(BCC).length > 0)) {
InternetAddress[] bccTo = new InternetAddress[getArrs(BCC).length];
for (int i = 0; i < getArrs(BCC).length; i++) {
bccTo[i] = new InternetAddress(getArrs(BCC)[i]);
if (bccTo[i].getPersonal() != null) {
bccTo[i].setPersonal(bccTo[i].getPersonal(), "UTF-8");
}
}
mimeMessage.setRecipients(javax.mail.Message.RecipientType.BCC, bccTo);
}
// set Subject to the message
mimeMessage.setSubject(subject);
mimeMessage.setSubject(message.getSubject(), "UTF-8");
mimeMessage.setSentDate(new Date());
MimeMultipart multipPartRoot = new MimeMultipart("mixed");
MimeMultipart multipPartContent = new MimeMultipart("alternative");
if (attachment != null && attachment.size() != 0) {
MimeBodyPart contentPartRoot = new MimeBodyPart();
if (mimeType != null && mimeType.indexOf("text/plain") > -1)
contentPartRoot.setContent(body, "text/plain; charset=utf-8");
else
contentPartRoot.setContent(body, "text/html; charset=utf-8");
MimeBodyPart mimeBodyPart1 = new MimeBodyPart();
mimeBodyPart1.setContent(body, mimeType);
multipPartContent.addBodyPart(mimeBodyPart1);
multipPartRoot.addBodyPart(contentPartRoot);
for (Attachment att : attachment) {
InputStream is = att.getInputStream();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(is, att.getMimeType());
mimeBodyPart.setDataHandler(new DataHandler(byteArrayDataSource));
mimeBodyPart.setDisposition(Part.ATTACHMENT);
if (att.getName() != null)
mimeBodyPart.setFileName(MimeUtility.encodeText(att.getName(), "utf-8", null));
multipPartRoot.addBodyPart(mimeBodyPart);
}
mimeMessage.setContent(multipPartRoot);
} else {
if (mimeType != null && mimeType.indexOf("text/plain") > -1)
mimeMessage.setContent(body, "text/plain; charset=utf-8");
else
mimeMessage.setContent(body, "text/html; charset=utf-8");
}
sendMessage(mimeMessage);
}
use of org.exoplatform.services.mail.Attachment in project kernel by exoplatform.
the class TestMailService method testSendMessageInFuture.
/**
* Here we test asynchronous email sending of {@link Message}.
* We check concurrent execution of {@link FutureTask}
*/
public void testSendMessageInFuture() throws Exception {
Message message;
Attachment attachment = new Attachment();
attachment.setInputStream(new ByteArrayInputStream(ATTACHMENT.getBytes()));
attachment.setMimeType(TEXT_PLAIN);
@SuppressWarnings("unchecked") Future<Boolean>[] futures = new Future[THREAD_NUMBER];
assertEquals("SMTP server should be now empty", 0, mailServer.getMessages().size());
for (int i = 0; i < THREAD_NUMBER; i++) {
assertFalse(isEmailMessageSent(MAIL_SUBJECT + i));
message = new Message();
message.setFrom(generateRandomEmailSender());
message.setTo(generateRandomEmailRecipient());
message.setCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
message.setBCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
message.setSubject(MAIL_SUBJECT + i);
message.setBody(MAIL_CONTENTS + i);
message.setMimeType(TEXT_HTML);
message.addAttachment(attachment);
futures[i] = service.sendMessageInFuture(message);
}
for (int i = 0; i < THREAD_NUMBER; i++) {
assertTrue(futures[i].get());
assertTrue(isEmailMessageSent(MAIL_SUBJECT + i));
}
// we assume that one thread sends one email
assertEquals("SMTP server should have" + (5 * THREAD_NUMBER) + " message (asynchronously sent)", 5 * THREAD_NUMBER, mailServer.getMessages().size());
}
Aggregations