use of javax.mail.Message in project Lucee by lucee.
the class MailClient method deleteMails.
/**
* delete all message in ibox that match given criteria
* @param messageNumbers
* @param uIds
* @throws MessagingException
* @throws IOException
*/
public void deleteMails(String[] as, String[] as1) throws MessagingException, IOException {
Folder folder;
Message[] amessage;
folder = _store.getFolder("INBOX");
folder.open(2);
Map<String, Message> map = getMessages(null, folder, as1, as, startrow, maxrows, false);
Iterator<String> iterator = map.keySet().iterator();
amessage = new Message[map.size()];
int i = 0;
while (iterator.hasNext()) {
amessage[i++] = map.get(iterator.next());
}
try {
folder.setFlags(amessage, new Flags(javax.mail.Flags.Flag.DELETED), true);
} finally {
folder.close(true);
}
}
use of javax.mail.Message in project blogwt by billy1380.
the class EmailHelper method sendEmail.
public static boolean sendEmail(String to, String name, String subject, String body, boolean isHtml, Map<String, byte[]> attachments) {
boolean sent = false;
Property email = PropertyServiceProvider.provide().getNamedProperty(PropertyHelper.OUTGOING_EMAIL);
if (!PropertyHelper.isEmpty(email) && !PropertyHelper.NONE_VALUE.equals(email.value)) {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
Message msg = new MimeMessage(session);
InternetAddress address = new InternetAddress(email.value, PropertyServiceProvider.provide().getNamedProperty(PropertyHelper.TITLE).value);
msg.setFrom(address);
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, name));
msg.setSubject(subject);
if (attachments == null || attachments.size() == 0) {
if (isHtml) {
msg.setContent(body, "text/html");
} else {
msg.setText(body);
}
} else {
Multipart mp = new MimeMultipart();
MimeBodyPart content = new MimeBodyPart();
if (isHtml) {
content.setContent(body, "text/html");
} else {
content.setText(body);
}
mp.addBodyPart(content);
for (String key : attachments.keySet()) {
MimeBodyPart attachment = new MimeBodyPart();
InputStream attachmentDataStream = new ByteArrayInputStream(attachments.get(key));
attachment.setFileName(key);
attachment.setContent(attachmentDataStream, "application/pdf");
mp.addBodyPart(attachment);
}
msg.setContent(mp);
}
Transport.send(msg);
if (LOG.isLoggable(Level.INFO)) {
LOG.info("Email sent successfully.");
}
sent = true;
} catch (MessagingException | UnsupportedEncodingException e) {
LOG.log(Level.WARNING, "Error sending email [" + content(to, name, subject, body) + "]", e);
}
} else {
if (LOG.isLoggable(Level.INFO)) {
LOG.info("Property [" + PropertyHelper.OUTGOING_EMAIL + "] not configured.");
}
}
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Email [" + content(to, name, subject, body) + "]");
}
return sent;
}
use of javax.mail.Message in project suite by stupidsing.
the class SmtpSslGmail method send.
public void send(String to, String subject, String body) {
Constants.bindSecrets("gmail .0 .1").map((username, enc) -> {
String password = decode(System.getenv("USER").toCharArray(), enc);
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", SSLSocketFactory.class.getName());
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
String sender = username + "@gmail.com";
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to != null ? to : sender));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
} catch (MessagingException e) {
Fail.t(e);
}
return true;
});
}
use of javax.mail.Message in project wso2-axis2-transports by wso2.
the class MailRequestResponseClient method waitForReply.
private Message waitForReply(String msgId) throws Exception {
Thread.yield();
Thread.sleep(100);
Message reply = null;
boolean replyNotFound = true;
int retryCount = 50;
while (replyNotFound) {
log.debug("Checking for response ... with MessageID : " + msgId);
reply = getMessage(msgId);
if (reply != null) {
replyNotFound = false;
} else {
if (retryCount-- > 0) {
Thread.sleep(100);
} else {
break;
}
}
}
return reply;
}
use of javax.mail.Message in project gocd by gocd.
the class Pop3MailClient method matchMessages.
private String matchMessages(MessageMatcher messageContains) throws MessagingException, IOException {
Folder folder = null;
try {
folder = getInboxFolder();
final Set<String> contents = new HashSet<>();
Message[] messagesInBox = folder.getMessages();
List<Message> messages = new ArrayList<>();
for (Message message : messagesInBox) {
if (messageContains.matches(message)) {
messages.add(message);
}
}
if (messages == null || messages.size() == 0) {
throw new RuntimeException("No message found matching :" + messageContains + ", the actual messages are " + contents);
}
if (messages.size() > 1) {
throw new RuntimeException("Multiple message matched content");
}
return (String) messages.get(0).getContent();
} finally {
if (folder != null && folder.isOpen()) {
try {
folder.close(true);
} catch (MessagingException e) {
}
}
}
}
Aggregations