use of javax.mail.Message in project camel by apache.
the class MailSorterTest method testSortMessagesMulti.
@Test
public void testSortMessagesMulti() throws Exception {
Message[] expected = new Message[] { MESSAGES[0], MESSAGES[1], MESSAGES[2] };
// should be ignored since it is already the decider.
for (SortTerm term1 : POSSIBLE_TERMS) {
for (SortTerm term2 : POSSIBLE_TERMS) {
Message[] actual = MESSAGES.clone();
MailSorter.sortMessages(actual, new SortTerm[] { term1, SortTerm.REVERSE, term2 });
try {
assertArrayEquals(actual, expected);
} catch (AssertionError ex) {
throw new AssertionError(String.format("Terms: %s, %s", term1.toString(), term2.toString()), ex);
}
}
}
}
use of javax.mail.Message in project camel by apache.
the class MailMultipleRecipientsUsingHeadersTest method testMailMultipleRecipientUsingHeaders.
@Test
public void testMailMultipleRecipientUsingHeaders() throws Exception {
Mailbox.clearAll();
// START SNIPPET: e1
Map<String, Object> map = new HashMap<String, Object>();
map.put("To", new String[] { "davsclaus@apache.org", "janstey@apache.org" });
map.put("From", "jstrachan@apache.org");
map.put("Subject", "Camel rocks");
String body = "Hello Riders.\nYes it does.\n\nRegards James.";
template.sendBodyAndHeaders("smtp://davsclaus@apache.org", body, map);
// END SNIPPET: e1
Mailbox box = Mailbox.get("davsclaus@apache.org");
Message msg = box.get(0);
assertEquals("davsclaus@apache.org", msg.getRecipients(Message.RecipientType.TO)[0].toString());
assertEquals("janstey@apache.org", msg.getRecipients(Message.RecipientType.TO)[1].toString());
assertEquals("jstrachan@apache.org", msg.getFrom()[0].toString());
assertEquals("Camel rocks", msg.getSubject());
}
use of javax.mail.Message in project remusic by aa112901.
the class CommonUtils method sendTextMail.
/**
* 以文本格式发送邮件
* @param title 待发送的邮件的信息
*/
public static boolean sendTextMail(String title, String content) {
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, null);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.163.com", 25, "remusic_log@163.com", "remusiclog1");
Message mailMessage = new SMTPMessage(session);
Address from = new InternetAddress("remusic_log@163.com");
mailMessage.setFrom(from);
Address to = new InternetAddress("remusic_log@163.com");
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(title);
mailMessage.setSentDate(new Date());
mailMessage.setText(content);
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
use of javax.mail.Message in project jodd by oblac.
the class ReceiveMailSession method receive.
/**
* Receives all emails that matches given {@link EmailFilter filter}
* and set given flags. Both filter and flags to set are optional.
* If flags to set is not provided, it forces 'seen' flag to be unset.
*/
public ReceivedEmail[] receive(EmailFilter filter, Flags flagsToSet) {
if (folder == null) {
useDefaultFolder();
}
Message[] messages;
try {
if (filter == null) {
messages = folder.getMessages();
} else {
messages = folder.search(filter.getSearchTerm());
}
if (messages.length == 0) {
return null;
}
// process messages
ReceivedEmail[] emails = new ReceivedEmail[messages.length];
for (int i = 0; i < messages.length; i++) {
Message msg = messages[i];
// we need to parse message BEFORE flags are set!
emails[i] = new ReceivedEmail(msg);
if (flagsToSet != null) {
emails[i].setFlags(flagsToSet);
msg.setFlags(flagsToSet, true);
}
if (flagsToSet == null && !emails[i].isSeen()) {
msg.setFlag(Flags.Flag.SEEN, false);
}
}
return emails;
} catch (MessagingException msex) {
throw new MailException("Failed to fetch messages", msex);
}
}
use of javax.mail.Message in project jodd by oblac.
the class SendMailTest method testTextHtml.
@Test
public void testTextHtml() throws MessagingException, IOException {
Email email = Email.create().from("from@example.com").to("to@example.com").subject("sub").addText("Hello!").addHtml("<html><body><h1>Hey!</h1></body></html>");
Message message = createMessage(email);
assertEquals(1, message.getFrom().length);
assertEquals("from@example.com", message.getFrom()[0].toString());
assertEquals(1, message.getRecipients(Message.RecipientType.TO).length);
assertEquals("to@example.com", message.getRecipients(Message.RecipientType.TO)[0].toString());
assertEquals("sub", message.getSubject());
// wrapper
MimeMultipart multipart = (MimeMultipart) message.getContent();
assertEquals(1, multipart.getCount());
assertTrue(multipart.getContentType().contains("multipart/mixed"));
// inner content
MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0);
MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent();
assertEquals(2, mimeMultipart.getCount());
assertTrue(mimeMultipart.getContentType().contains("multipart/alternative"));
MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0);
assertEquals("Hello!", bodyPart.getContent());
assertTrue(bodyPart.getDataHandler().getContentType().contains("text/plain"));
bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(1);
assertEquals("<html><body><h1>Hey!</h1></body></html>", bodyPart.getContent());
assertTrue(bodyPart.getDataHandler().getContentType().contains("text/html"));
}
Aggregations