use of javax.mail.internet.MimeMessage in project Openfire by igniterealtime.
the class SendMail method sendMessage.
public boolean sendMessage(String message, String host, String port, String username, String password) {
boolean ok = false;
String uidString = "";
try {
// Set the email properties necessary to send email
final Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.server", host);
if (ModelUtil.hasLength(port)) {
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", port);
}
Session sess;
if (ModelUtil.hasLength(password) && ModelUtil.hasLength(username)) {
sess = Session.getInstance(props, new MailAuthentication(username, password));
} else {
sess = Session.getDefaultInstance(props, null);
}
Message msg = new MimeMessage(sess);
StringTokenizer toST = new StringTokenizer(toField, ",");
if (toST.countTokens() > 1) {
InternetAddress[] address = new InternetAddress[toST.countTokens()];
int addrIndex = 0;
String addrString = "";
while (toST.hasMoreTokens()) {
addrString = toST.nextToken();
address[addrIndex] = (new InternetAddress(addrString));
addrIndex = addrIndex + 1;
}
msg.setRecipients(Message.RecipientType.TO, address);
} else {
InternetAddress[] address = { new InternetAddress(toField) };
msg.setRecipients(Message.RecipientType.TO, address);
}
InternetAddress from = new InternetAddress(myAddress);
msg.setFrom(from);
msg.setSubject(subjectField);
UID msgUID = new UID();
uidString = msgUID.toString();
msg.setHeader("X-Mailer", uidString);
msg.setSentDate(new Date());
MimeMultipart mp = new MimeMultipart();
// create body part for textarea
MimeBodyPart mbp1 = new MimeBodyPart();
if (getCustomerName() != null) {
messageText = "From: " + getCustomerName() + "\n" + messageText;
}
if (isHTML) {
mbp1.setContent(messageText, "text/html");
} else {
mbp1.setContent(messageText, "text/plain");
}
mp.addBodyPart(mbp1);
try {
if (!isHTML) {
msg.setContent(messageText, "text/plain");
} else {
msg.setContent(messageText, "text/html");
}
Transport.send(msg);
ok = true;
} catch (SendFailedException sfe) {
Log.warn("Could not connect to SMTP server.");
}
} catch (Exception eq) {
Log.warn("Could not connect to SMTP server.");
}
return ok;
}
use of javax.mail.internet.MimeMessage in project camel by apache.
the class MailConsumerUnsupportedCharsetTest method prepareMailbox.
private void prepareMailbox() throws Exception {
// connect to mailbox
Mailbox.clearAll();
JavaMailSender sender = new DefaultJavaMailSender();
Store store = sender.getSession().getStore("imap");
store.connect("localhost", 25, "jones", "secret");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
folder.expunge();
Message[] msg = new Message[1];
MimeMessage mime = new MimeMessage(sender.getSession());
mime.setContent("Bye World", "text/plain; charset=ThisIsNotAKnownCharset");
msg[0] = mime;
folder.appendMessages(msg);
folder.close(true);
}
use of javax.mail.internet.MimeMessage in project camel by apache.
the class MailDoNotDeleteIfProcessFailsTest method prepareMailbox.
private void prepareMailbox() throws Exception {
// connect to mailbox
Mailbox.clearAll();
JavaMailSender sender = new DefaultJavaMailSender();
Store store = sender.getSession().getStore("imap");
store.connect("localhost", 25, "claus", "secret");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
folder.expunge();
// inserts two new messages
Message[] msg = new Message[2];
msg[0] = new MimeMessage(sender.getSession());
msg[0].setText("Message 1");
msg[0].setHeader("Message-ID", "0");
msg[0].setFlag(Flags.Flag.SEEN, false);
msg[1] = new MimeMessage(sender.getSession());
msg[1].setText("Message 2");
msg[0].setHeader("Message-ID", "1");
msg[1].setFlag(Flags.Flag.SEEN, true);
folder.appendMessages(msg);
folder.close(true);
}
use of javax.mail.internet.MimeMessage in project camel by apache.
the class NestedMimeMessageConsumeTest method prepareMailbox.
private void prepareMailbox(String user) throws MessagingException {
// connect to mailbox
JavaMailSender sender = new DefaultJavaMailSender();
Store store = sender.getSession().getStore("pop3");
store.connect("localhost", 25, user, "secret");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
folder.expunge();
InputStream is = getClass().getResourceAsStream("/nested-multipart.elm");
Message hurzMsg = new MimeMessage(sender.getSession(), is);
Message[] messages = new Message[] { hurzMsg };
// insert one signed message
folder.appendMessages(messages);
folder.close(true);
}
use of javax.mail.internet.MimeMessage in project camel by apache.
the class SearchTermBuilderTest method testSearchTermSentLast24Hours.
public void testSearchTermSentLast24Hours() throws Exception {
SearchTermBuilder build = new SearchTermBuilder();
long offset = -1 * (24 * 60 * 60 * 1000L);
SearchTerm st = build.subject("Camel").sentNow(Comparison.GE, offset).build();
assertNotNull(st);
// create dummy message
Mailbox.clearAll();
JavaMailSender sender = new DefaultJavaMailSender();
MimeMessage msg = new MimeMessage(sender.getSession());
msg.setSubject("Yeah Camel rocks");
msg.setText("Apache Camel is a cool project. Have a fun ride.");
msg.setFrom(new InternetAddress("someone@somewhere.com"));
msg.setSentDate(new Date());
assertTrue("Should match message", st.match(msg));
MimeMessage msg2 = new MimeMessage(sender.getSession());
msg2.setSubject("Camel in Action");
msg2.setText("Hey great book");
msg2.setFrom(new InternetAddress("dude@apache.org"));
// mark it as sent 2 days ago
long twoDays = 2 * 24 * 60 * 60 * 1000L;
long time = new Date().getTime() - twoDays;
msg2.setSentDate(new Date(time));
assertFalse("Should not match message as its too old", st.match(msg2));
}
Aggregations