use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.
the class SendShareNotificationTest method init.
@BeforeClass
public static void init() throws Exception {
MailboxTestUtil.initServer();
Provisioning prov = Provisioning.getInstance();
Map<String, Object> attrs = Maps.newHashMap();
prov.createDomain("zimbra.com", attrs);
attrs = Maps.newHashMap();
prov.createAccount("test@zimbra.com", "secret", attrs);
attrs = Maps.newHashMap();
attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
prov.createAccount("test2@zimbra.com", "secret", attrs);
attrs = Maps.newHashMap();
attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
prov.createAccount("test3@zimbra.com", "secret", attrs);
// this MailboxManager does everything except actually send mail
MailboxManager.setInstance(new MailboxManager() {
@Override
protected Mailbox instantiateMailbox(MailboxData data) {
return new Mailbox(data) {
@Override
public MailSender getMailSender() {
return new MailSender() {
@Override
protected Collection<Address> sendMessage(Mailbox mbox, MimeMessage mm, Collection<RollbackData> rollbacks) throws SafeMessagingException, IOException {
try {
return Arrays.asList(getRecipients(mm));
} catch (Exception e) {
return Collections.emptyList();
}
}
};
}
};
}
});
L10nUtil.setMsgClassLoader("conf/msgs");
}
use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.
the class GetMsgTest method setUp.
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
MailboxTestUtil.initServer();
MailboxTestUtil.clearData();
Provisioning prov = Provisioning.getInstance();
Map<String, Object> attrs = Maps.newHashMap();
prov.createDomain("zimbra.com", attrs);
attrs = Maps.newHashMap();
attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
prov.createAccount("test@zimbra.com", "secret", attrs);
attrs = Maps.newHashMap();
attrs.put(Provisioning.A_zimbraId, UUID.randomUUID().toString());
prov.createAccount("test2@zimbra.com", "secret", attrs);
// this MailboxManager does everything except actually send mail
MailboxManager.setInstance(new MailboxManager() {
@Override
protected Mailbox instantiateMailbox(MailboxData data) {
return new Mailbox(data) {
@Override
public MailSender getMailSender() {
return new MailSender() {
@Override
protected Collection<Address> sendMessage(Mailbox mbox, MimeMessage mm, Collection<RollbackData> rollbacks) {
try {
return Arrays.asList(getRecipients(mm));
} catch (Exception e) {
return Collections.emptyList();
}
}
};
}
};
}
});
}
use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.
the class SendMsg method doSendMessage.
public static ItemId doSendMessage(OperationContext oc, Mailbox mbox, MimeMessage mm, List<Upload> uploads, ItemId origMsgId, String replyType, String identityId, String dataSourceId, boolean noSaveToSent, boolean needCalendarSentByFixup, boolean isCalendarForward) throws ServiceException {
boolean isCalendarMessage = false;
ItemId id;
OutlookICalendarFixupMimeVisitor mv = null;
if (needCalendarSentByFixup || isCalendarForward) {
mv = new OutlookICalendarFixupMimeVisitor(mbox.getAccount(), mbox).needFixup(needCalendarSentByFixup).setIsCalendarForward(isCalendarForward);
try {
mv.accept(mm);
} catch (MessagingException e) {
throw ServiceException.PARSE_ERROR("Error while fixing up SendMsg for SENT-BY", e);
}
isCalendarMessage = mv.isCalendarMessage();
}
MailSender sender;
if (dataSourceId == null) {
sender = isCalendarMessage ? CalendarMailSender.getCalendarMailSender(mbox) : mbox.getMailSender();
if (noSaveToSent) {
id = sender.sendMimeMessage(oc, mbox, false, mm, uploads, origMsgId, replyType, null, false, processor);
} else {
id = sender.sendMimeMessage(oc, mbox, mm, uploads, origMsgId, replyType, identityId, false, processor);
}
} else {
com.zimbra.cs.account.DataSource dataSource = mbox.getAccount().getDataSourceById(dataSourceId);
if (dataSource == null) {
throw ServiceException.INVALID_REQUEST("No data source with id " + dataSourceId, null);
}
if (!dataSource.isSmtpEnabled()) {
throw ServiceException.INVALID_REQUEST("Data source SMTP is not enabled", null);
}
sender = mbox.getDataSourceMailSender(dataSource, isCalendarMessage);
id = sender.sendDataSourceMimeMessage(oc, mbox, mm, uploads, origMsgId, replyType);
}
// Send Calendar Forward Invitation notification if applicable
try {
if (isCalendarMessage && isCalendarForward && mv.getOriginalInvite() != null) {
ZOrganizer org = mv.getOriginalInvite().getOrganizer();
if (org != null) {
String orgAddress = org.getAddress();
Account orgAccount = Provisioning.getInstance().getAccountByName(orgAddress);
if (orgAccount != null && !orgAccount.getId().equals(mbox.getAccount().getId())) {
MimeMessage notifyMimeMsg = CalendarMailSender.createForwardNotifyMessage(mbox.getAccount(), orgAccount, orgAddress, mm.getAllRecipients(), mv.getOriginalInvite());
CalendarMailSender.sendPartial(oc, mbox, notifyMimeMsg, null, null, null, null, false, true);
}
}
}
} catch (Exception e) {
ZimbraLog.soap.warn("Ignoring error while sending Calendar Invitation Forward Notification", e);
}
return id;
}
use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.
the class FilterUtil method notify.
public static void notify(OperationContext octxt, Mailbox mailbox, ParsedMessage parsedMessage, String emailAddr, String subjectTemplate, String bodyTemplate, int maxBodyBytes, List<String> origHeaders) throws MessagingException, ServiceException {
MimeMessage mimeMessage = parsedMessage.getMimeMessage();
if (isMailLoop(mailbox, mimeMessage, new String[] { HEADER_FORWARDED })) {
String error = String.format("Detected a mail loop for message %s.", Mime.getMessageID(mimeMessage));
throw ServiceException.FAILURE(error, null);
}
Account account = mailbox.getAccount();
MimeMessage notification = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
// add the forwarded header account names to detect the mail loop between accounts
for (String headerFwdAccountName : Mime.getHeaders(mimeMessage, HEADER_FORWARDED)) {
notification.addHeader(HEADER_FORWARDED, headerFwdAccountName);
}
notification.addHeader(HEADER_FORWARDED, account.getName());
MailSender mailSender = mailbox.getMailSender().setSaveToSent(false);
//Map<String, String> vars = getVarsMap(mailbox, parsedMessage, mimeMessage);
if (origHeaders == null || origHeaders.isEmpty()) {
// no headers need to be copied from the original message
notification.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(emailAddr));
notification.setSentDate(new Date());
if (!StringUtil.isNullOrEmpty(subjectTemplate)) {
notification.setSubject(subjectTemplate, getCharset(account, subjectTemplate));
}
} else {
if (origHeaders.size() == 1 && "*".equals(origHeaders.get(0))) {
// all headers need to be copied from the original message
Enumeration enumeration = mimeMessage.getAllHeaders();
while (enumeration.hasMoreElements()) {
Header header = (Header) enumeration.nextElement();
if (StringUtil.equal(header.getName(), HEADER_FORWARDED)) {
continue;
}
if (StringUtil.equal(header.getName(), HEADER_CONTENT_TYPE) || StringUtil.equal(header.getName(), HEADER_CONTENT_DISPOSITION)) {
// Zimbra Mime parser will add the correct Content Type if absent
continue;
}
notification.addHeader(header.getName(), header.getValue());
}
} else {
// some headers need to be copied from the original message
Set<String> headersToCopy = Sets.newHashSet(origHeaders);
boolean copySubject = false;
for (String header : headersToCopy) {
if ("Subject".equalsIgnoreCase(header)) {
copySubject = true;
}
if (StringUtil.equal(header, HEADER_FORWARDED)) {
continue;
}
String[] hdrVals = mimeMessage.getHeader(header);
if (hdrVals == null) {
continue;
}
for (String hdrVal : hdrVals) {
notification.addHeader(header, hdrVal);
}
}
if (!copySubject && !StringUtil.isNullOrEmpty(subjectTemplate)) {
notification.setSubject(subjectTemplate, getCharset(account, subjectTemplate));
}
}
mailSender.setRedirectMode(true);
mailSender.setRecipients(emailAddr);
}
String body = StringUtil.truncateIfRequired(bodyTemplate, maxBodyBytes);
notification.setText(body, getCharset(account, body));
notification.saveChanges();
if (isDeliveryStatusNotification(mimeMessage)) {
mailSender.setEnvelopeFrom("<>");
} else {
mailSender.setEnvelopeFrom(account.getName());
}
mailSender.setDsnNotifyOptions(MailSender.DsnNotifyOption.NEVER);
mailSender.sendMimeMessage(octxt, mailbox, notification);
}
use of com.zimbra.cs.mailbox.MailSender in project zm-mailbox by Zimbra.
the class CalItemSmsReminderTask method sendReminder.
@Override
protected void sendReminder(CalendarItem calItem, Invite invite) throws Exception {
Account account = calItem.getAccount();
Locale locale = account.getLocale();
TimeZone tz = Util.getAccountTimeZone(account);
MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
String to = account.getAttr(Provisioning.A_zimbraCalendarReminderDeviceEmail);
if (to == null) {
ZimbraLog.scheduler.info("Unable to send calendar reminder sms since %s is not set", Provisioning.A_zimbraCalendarReminderDeviceEmail);
return;
}
mm.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(to));
mm.setText(getText(calItem, invite, locale, tz), MimeConstants.P_CHARSET_UTF8);
mm.saveChanges();
MailSender mailSender = calItem.getMailbox().getMailSender();
mailSender.setSaveToSent(false);
mailSender.sendMimeMessage(null, calItem.getMailbox(), mm);
}
Aggregations