use of javax.mail.Address in project zm-mailbox by Zimbra.
the class MimeTest method semiColonAddressSeparator.
@Test
public void semiColonAddressSeparator() throws Exception {
StringBuilder to = new StringBuilder("To: ");
int count = 4;
for (int i = 1; i < count + 1; i++) {
to.append("user").append(count).append("@example.com").append(";");
}
to.setLength(to.length() - 1);
to.append("\r\n");
String content = "From: user1@example.com\r\n" + to.toString() + "Subject: test\r\n" + "Content-Type: test/plain\r\n\r\n" + "test message";
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
Address[] recipients = mm.getAllRecipients();
Assert.assertEquals(count, recipients.length);
}
use of javax.mail.Address in project zm-mailbox by Zimbra.
the class AutoProvision method sendNotifMessage.
protected void sendNotifMessage(Account acct, String password) throws ServiceException {
String subject = fillTemplate(acct, domain.getAutoProvNotificationSubject());
String body = fillTemplate(acct, domain.getAutoProvNotificationBody());
String from = domain.getAutoProvNotificationFromAddress();
if (from == null) {
// TODO: should we use a seperate boolean control?
return;
}
String toAddr = acct.getName();
try {
SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
InternetAddress addr = null;
try {
addr = new JavaMailInternetAddress(from);
} catch (AddressException e) {
// log and try the next one
ZimbraLog.autoprov.warn("invalid address in " + Provisioning.A_zimbraAutoProvNotificationFromAddress, e);
}
Address fromAddr = addr;
Address replyToAddr = addr;
// From
out.setFrom(fromAddr);
// Reply-To
out.setReplyTo(new Address[] { replyToAddr });
// To
out.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(toAddr));
// Date
out.setSentDate(new Date());
// Subject
Locale locale = acct.getLocale();
out.setSubject(subject);
// NOTIFY=NEVER
out.setNotifyOptions(SMTPMessage.NOTIFY_NEVER);
// body
MimeMultipart mmp = new ZMimeMultipart("alternative");
// TEXT part (add me first!)
String text = body;
MimeBodyPart textPart = new ZMimeBodyPart();
textPart.setText(text, MimeConstants.P_CHARSET_UTF8);
mmp.addBodyPart(textPart);
// HTML part
StringBuilder html = new StringBuilder();
html.append("<h4>\n");
html.append("<p>" + body + "</p>\n");
html.append("</h4>\n");
html.append("\n");
MimeBodyPart htmlPart = new ZMimeBodyPart();
htmlPart.setDataHandler(new DataHandler(new HtmlPartDataSource(html.toString())));
mmp.addBodyPart(htmlPart);
out.setContent(mmp);
// send it
Transport.send(out);
// log
Address[] rcpts = out.getRecipients(javax.mail.Message.RecipientType.TO);
StringBuilder rcptAddr = new StringBuilder();
for (Address a : rcpts) rcptAddr.append(a.toString());
ZimbraLog.autoprov.info("auto provision notification sent rcpt='" + rcptAddr + "' Message-ID=" + out.getMessageID());
} catch (MessagingException e) {
ZimbraLog.autoprov.warn("send auto provision notification failed rcpt='" + toAddr + "'", e);
}
}
use of javax.mail.Address in project ats-framework by Axway.
the class MimePackage method getRecipients.
/**
* Get the recipients of the specified type
*
* @param recipientType
* the type of recipient - to, cc or bcc
* @return array with recipients, emtpy array of no recipients of this type
* are present
* @throws PackageException
*/
@PublicAtsApi
public String[] getRecipients(RecipientType recipientType) throws PackageException {
try {
Address[] recipientAddresses = message.getRecipients(recipientType.toJavamailType());
// return an empty string if no recipients are present
if (recipientAddresses == null) {
return new String[] {};
}
String[] recipients = new String[recipientAddresses.length];
for (int i = 0; i < recipientAddresses.length; i++) {
recipients[i] = recipientAddresses[i].toString();
}
return recipients;
} catch (MessagingException me) {
throw new PackageException(me);
}
}
use of javax.mail.Address in project zm-mailbox by Zimbra.
the class FilterUtil method redirect.
public static void redirect(OperationContext octxt, Mailbox sourceMbox, MimeMessage msg, String destinationAddress) throws ServiceException {
MimeMessage outgoingMsg;
try {
if (!isMailLoop(sourceMbox, msg, new String[] { HEADER_FORWARDED })) {
outgoingMsg = new Mime.FixedMimeMessage(msg);
Mime.recursiveRepairTransferEncoding(outgoingMsg);
outgoingMsg.addHeader(HEADER_FORWARDED, sourceMbox.getAccount().getName());
outgoingMsg.saveChanges();
} else {
String error = String.format("Detected a mail loop for message %s.", Mime.getMessageID(msg));
throw ServiceException.FAILURE(error, null);
}
} catch (MessagingException e) {
try {
outgoingMsg = createRedirectMsgOnError(msg);
ZimbraLog.filter.info("Message format error detected. Wrapper class in use. %s", e.toString());
} catch (MessagingException again) {
throw ServiceException.FAILURE("Message format error detected. Workaround failed.", again);
}
} catch (IOException e) {
try {
outgoingMsg = createRedirectMsgOnError(msg);
ZimbraLog.filter.info("Message format error detected. Wrapper class in use. %s", e.toString());
} catch (MessagingException me) {
throw ServiceException.FAILURE("Message format error detected. Workaround failed.", me);
}
}
MailSender sender = sourceMbox.getMailSender().setSaveToSent(false).setRedirectMode(true).setSkipHeaderUpdate(true);
try {
if (Provisioning.getInstance().getLocalServer().isMailRedirectSetEnvelopeSender()) {
if (isDeliveryStatusNotification(msg) && LC.filter_null_env_sender_for_dsn_redirect.booleanValue()) {
sender.setEnvelopeFrom("<>");
sender.setDsnNotifyOptions(MailSender.DsnNotifyOption.NEVER);
} else {
// Set envelope sender to the account name (bug 31309).
Account account = sourceMbox.getAccount();
sender.setEnvelopeFrom(account.getName());
}
} else {
Address from = ArrayUtil.getFirstElement(outgoingMsg.getFrom());
if (from != null) {
String address = ((InternetAddress) from).getAddress();
sender.setEnvelopeFrom(address);
}
}
sender.setRecipients(destinationAddress);
sender.sendMimeMessage(octxt, sourceMbox, outgoingMsg);
} catch (MessagingException e) {
ZimbraLog.filter.warn("Envelope sender will be set to the default value.", e);
}
}
use of javax.mail.Address in project zm-mailbox by Zimbra.
the class CalendarRequest method notifyCalendarItem.
// Notify attendees following an update to the series of a recurring appointment. Only the
// added attendees are notified if notifyAllAttendees is false. If it is true all attendees
// for each invite are notified. (Some invites may have more attendees than others.)
protected static void notifyCalendarItem(ZimbraSoapContext zsc, OperationContext octxt, Account acct, Mailbox mbox, CalendarItem calItem, boolean notifyAllAttendees, List<ZAttendee> addedAttendees, boolean ignorePastExceptions, MailSendQueue sendQueue) throws ServiceException {
boolean onBehalfOf = isOnBehalfOfRequest(zsc);
Account authAcct = getAuthenticatedAccount(zsc);
boolean hidePrivate = !calItem.isPublic() && !calItem.allowPrivateAccess(authAcct, zsc.isUsingAdminPrivileges());
Address from = AccountUtil.getFriendlyEmailAddress(acct);
Address sender = null;
if (onBehalfOf)
sender = AccountUtil.getFriendlyEmailAddress(authAcct);
List<Address> addedRcpts = CalendarMailSender.toListFromAttendees(addedAttendees);
long now = octxt != null ? octxt.getTimestamp() : System.currentTimeMillis();
mbox.lock.lock();
try {
// Refresh the cal item so we see the latest blob, whose path may have been changed
// earlier in the current request.
calItem = mbox.getCalendarItemById(octxt, calItem.getId());
Invite[] invites = calItem.getInvites();
// Get exception instances. These will be included in the series update email.
List<Invite> exceptions = new ArrayList<Invite>();
for (Invite inv : invites) {
if (inv.hasRecurId()) {
exceptions.add(inv);
}
}
// Send the update invites.
boolean didExceptions = false;
for (Invite inv : invites) {
if (ignorePastExceptions && inv.hasRecurId() && !inviteIsAfterTime(inv, now)) {
continue;
}
// Make the new iCalendar part to send.
ZVCalendar cal = inv.newToICalendar(!hidePrivate);
// For series invite, append the exception instances.
if (inv.isRecurrence() && !didExceptions) {
// Find the VEVENT/VTODO for the series.
ZComponent seriesComp = null;
for (Iterator<ZComponent> compIter = cal.getComponentIterator(); compIter.hasNext(); ) {
ZComponent comp = compIter.next();
ICalTok compName = comp.getTok();
if (ICalTok.VEVENT.equals(compName) || ICalTok.VTODO.equals(compName)) {
if (comp.getProperty(ICalTok.RRULE) != null) {
seriesComp = comp;
break;
}
}
}
for (Invite except : exceptions) {
if (except.isCancel() && seriesComp != null) {
// Cancels are added as EXDATEs in the series VEVENT/VTODO.
RecurId rid = except.getRecurId();
if (rid != null && rid.getDt() != null) {
ZProperty exdate = rid.getDt().toProperty(ICalTok.EXDATE, false);
seriesComp.addProperty(exdate);
}
} else {
// Exception instances are added as additional VEVENTs/VTODOs.
ZComponent exceptComp = except.newToVComponent(false, !hidePrivate);
cal.addComponent(exceptComp);
}
}
didExceptions = true;
}
// Compose email using the existing MimeMessage as template and send it.
MimeMessage mmInv = calItem.getSubpartMessage(inv.getMailItemId());
List<Address> rcpts;
if (notifyAllAttendees) {
rcpts = CalendarMailSender.toListFromAttendees(inv.getAttendees());
} else {
rcpts = addedRcpts;
}
if (rcpts != null && !rcpts.isEmpty()) {
MimeMessage mmModify = CalendarMailSender.createCalendarMessage(authAcct, from, sender, rcpts, mmInv, inv, cal, true);
CalSendData csd = new CalSendData();
csd.mMm = mmModify;
csd.mOrigId = new ItemId(mbox, inv.getMailItemId());
MailSendQueueEntry entry = new MailSendQueueEntry(octxt, mbox, csd, null);
sendQueue.add(entry);
}
}
} finally {
mbox.lock.release();
}
}
Aggregations