use of javax.mail.internet.InternetAddress in project hudson-2.x by hudson.
the class BaseMailSender method getMail.
/**
* Returns prepared email.
*
* @return prepared email.
* @throws MessagingException exception if any.
*/
protected MimeMessage getMail() throws MessagingException {
MimeMessage msg = new HudsonMimeMessage(Mailer.descriptor().createSession());
msg.setContent("", "text/plain");
msg.setFrom(new InternetAddress(Mailer.descriptor().getAdminAddress()));
msg.setSentDate(new Date());
Set<InternetAddress> rcp = new LinkedHashSet<InternetAddress>();
StringTokenizer tokens = new StringTokenizer(recipients);
while (tokens.hasMoreTokens()) {
String address = tokens.nextToken();
try {
rcp.add(new InternetAddress(address));
} catch (AddressException ignore) {
// ignore bad address, but try to send to other addresses
}
}
msg.setRecipients(Message.RecipientType.TO, rcp.toArray(new InternetAddress[rcp.size()]));
msg.setSubject(new StringBuilder().append(getSubjectPrefix()).append(" ").append(getSubject()).toString(), charset);
msg.setText(new StringBuilder().append(getText()).append(getTextFooter()).toString(), charset);
return msg;
}
use of javax.mail.internet.InternetAddress in project ninja by ninjaframework.
the class CommonsMailHelperImplTest method doConvertAdressesToInternetAddressList.
/**
* Convenience method as commonsmail does not use generics...
*
* @param list
* @return
*/
private List<InternetAddress> doConvertAdressesToInternetAddressList(List<?> list) {
List<InternetAddress> returnList = new ArrayList<InternetAddress>();
for (Object object : list) {
InternetAddress internetAddress = (InternetAddress) object;
returnList.add(internetAddress);
}
return returnList;
}
use of javax.mail.internet.InternetAddress in project ninja by ninjaframework.
the class CommonsmailHelperImpl method createValidEmailFromString.
@Override
public Tuple<String, String> createValidEmailFromString(String email) throws AddressException {
InternetAddress internetAddress = new InternetAddress(email);
Tuple<String, String> tuple = new Tuple<String, String>(internetAddress.getAddress(), internetAddress.getPersonal());
return tuple;
}
use of javax.mail.internet.InternetAddress in project jodd by oblac.
the class SendMailSession method createMessage.
// ---------------------------------------------------------------- adapter
/**
* Creates new JavaX message from {@link Email email}.
*/
protected MimeMessage createMessage(Email email, Session session) throws MessagingException {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(email.getFrom().toInternetAddress());
// to
int totalTo = email.getTo().length;
InternetAddress[] address = new InternetAddress[totalTo];
for (int i = 0; i < totalTo; i++) {
address[i] = email.getTo()[i].toInternetAddress();
}
msg.setRecipients(Message.RecipientType.TO, address);
// replyTo
if (email.getReplyTo() != null) {
int totalReplyTo = email.getReplyTo().length;
address = new InternetAddress[totalReplyTo];
for (int i = 0; i < totalReplyTo; i++) {
address[i] = email.getReplyTo()[i].toInternetAddress();
}
msg.setReplyTo(address);
}
// cc
if (email.getCc() != null) {
int totalCc = email.getCc().length;
address = new InternetAddress[totalCc];
for (int i = 0; i < totalCc; i++) {
address[i] = email.getCc()[i].toInternetAddress();
}
msg.setRecipients(Message.RecipientType.CC, address);
}
// bcc
if (email.getBcc() != null) {
int totalBcc = email.getBcc().length;
address = new InternetAddress[totalBcc];
for (int i = 0; i < totalBcc; i++) {
address[i] = email.getBcc()[i].toInternetAddress();
}
msg.setRecipients(Message.RecipientType.BCC, address);
}
if (email.getSubjectEncoding() != null) {
msg.setSubject(email.getSubject(), email.getSubjectEncoding());
} else {
msg.setSubject(email.getSubject());
}
Date date = email.getSentDate();
if (date == null) {
date = new Date();
}
msg.setSentDate(date);
// headers
Map<String, String> headers = email.getAllHeaders();
if (headers != null) {
for (Map.Entry<String, String> stringStringEntry : headers.entrySet()) {
String value = stringStringEntry.getValue();
msg.setHeader(stringStringEntry.getKey(), value);
}
}
// message data and attachments
final List<EmailMessage> messages = email.getAllMessages();
final List<EmailAttachment> attachments = email.getAttachments() == null ? null : new ArrayList<>(email.getAttachments());
final int totalMessages = messages.size();
if ((attachments == null) && (totalMessages == 1)) {
// special case: no attachments and just one content
EmailMessage emailMessage = messages.get(0);
msg.setContent(emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding());
} else {
Multipart multipart = new MimeMultipart();
Multipart msgMultipart = multipart;
if (totalMessages > 1) {
MimeBodyPart bodyPart = new MimeBodyPart();
msgMultipart = new MimeMultipart(ALTERNATIVE);
bodyPart.setContent(msgMultipart);
multipart.addBodyPart(bodyPart);
}
for (EmailMessage emailMessage : messages) {
// detect embedded attachments
List<EmailAttachment> embeddedAttachments = filterEmbeddedAttachments(attachments, emailMessage);
MimeBodyPart bodyPart = new MimeBodyPart();
if (embeddedAttachments == null) {
// no embedded attachments, just add message
bodyPart.setContent(emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding());
} else {
// embedded attachments detected, join them as related
MimeMultipart relatedMultipart = new MimeMultipart(RELATED);
MimeBodyPart messageData = new MimeBodyPart();
messageData.setContent(emailMessage.getContent(), emailMessage.getMimeType() + CHARSET + emailMessage.getEncoding());
relatedMultipart.addBodyPart(messageData);
for (EmailAttachment att : embeddedAttachments) {
MimeBodyPart attBodyPart = createAttachmentBodyPart(att);
relatedMultipart.addBodyPart(attBodyPart);
}
bodyPart.setContent(relatedMultipart);
}
msgMultipart.addBodyPart(bodyPart);
}
if (attachments != null) {
// attach remaining attachments
for (EmailAttachment att : attachments) {
MimeBodyPart attBodyPart = createAttachmentBodyPart(att);
multipart.addBodyPart(attBodyPart);
}
}
msg.setContent(multipart);
}
return msg;
}
use of javax.mail.internet.InternetAddress in project jodd by oblac.
the class RFC2822AddressParser method pullFromGroups.
// ---------------------------------------------------------------- utilities
private InternetAddress pullFromGroups(Matcher m) {
InternetAddress currentInternetAddress;
String[] parts = _calcMatcherParts(m);
if (parts[1] == null || parts[2] == null) {
return null;
}
// functions use pullFromGroups; some call getMatcherParts directly.
try {
//currentInternetAddress = new InternetAddress(parts[0] + " <" + parts[1] + "@" +
// parts[2]+ ">", true);
// so it parses it OK, but since javamail doesn't extract too well
// we make sure that the consistent parts
// are correct
currentInternetAddress = new InternetAddress();
currentInternetAddress.setPersonal(parts[0]);
currentInternetAddress.setAddress(parts[1] + "@" + parts[2]);
} catch (UnsupportedEncodingException uee) {
currentInternetAddress = null;
}
return currentInternetAddress;
}
Aggregations