use of com.sun.mail.smtp.SMTPMessage in project zm-mailbox by Zimbra.
the class SmtpTransportTest method nullMailFrom.
@Test(timeout = 3000)
public void nullMailFrom() throws Exception {
server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("354 OK").swallowUntil("\r\n.\r\n").sendLine("250 OK").recvLine().sendLine("221 bye").build().start(PORT);
Session session = JMSession.getSession();
session.getProperties().setProperty("mail.smtp.from", "from@zimbra.com");
Transport transport = session.getTransport("smtp");
transport.connect("localhost", PORT, null, null);
String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
SMTPMessage msg = new SMTPMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
// this should override the previously set mail.smtp.from
msg.setEnvelopeFrom("<>");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
server.shutdown(1000);
Assert.assertEquals("EHLO localhost\r\n", server.replay());
Assert.assertEquals("MAIL FROM:<>\r\n", server.replay());
Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
Assert.assertEquals("DATA\r\n", server.replay());
Assert.assertEquals("QUIT\r\n", server.replay());
Assert.assertNull(server.replay());
}
use of com.sun.mail.smtp.SMTPMessage in project zm-mailbox by Zimbra.
the class Notification method interceptIfNecessary.
/**
* If <tt>zimbraInterceptAddress</tt> is specified, sends a message to that
* address with the given message attached.
*
* @param operation name of the operation being performed (send, add message, save draft, etc.)
* @param folder the folder that the message was filed into, or <tt>null</tt>
*/
void interceptIfNecessary(Mailbox mbox, MimeMessage msg, String operation, Folder folder) throws ServiceException {
// Don't do anything if intercept is turned off.
Account account = mbox.getAccount();
String[] interceptAddresses = account.getMultiAttr(Provisioning.A_zimbraInterceptAddress);
if (interceptAddresses.length == 0) {
return;
}
for (String interceptAddress : interceptAddresses) {
try {
ZimbraLog.mailbox.info("Sending intercept of message %s to %s.", msg.getMessageID(), interceptAddress);
// Fill templates
String folderName = "none";
String folderId = "none";
if (folder != null) {
folderName = folder.getName();
folderId = Integer.toString(folder.getId());
}
Map<String, String> vars = new HashMap<String, String>();
vars.put("ACCOUNT_DOMAIN", getDomain(account.getName()));
vars.put("ACCOUNT_ADDRESS", account.getName());
vars.put("MESSAGE_SUBJECT", Mime.getSubject(msg));
vars.put("OPERATION", operation);
vars.put("FOLDER_NAME", folderName);
vars.put("FOLDER_ID", folderId);
vars.put("NEWLINE", "\r\n");
String from = StringUtil.fillTemplate(account.getAttr(Provisioning.A_zimbraInterceptFrom), vars);
String subject = StringUtil.fillTemplate(account.getAttr(Provisioning.A_zimbraInterceptSubject), vars);
String bodyText = StringUtil.fillTemplate(account.getAttr(Provisioning.A_zimbraInterceptBody), vars);
// Assemble outgoing message
MimeMessage attached = msg;
boolean headersOnly = account.getBooleanAttr(Provisioning.A_zimbraInterceptSendHeadersOnly, false);
if (headersOnly) {
attached = new MimeMessageWithId(msg.getMessageID());
Enumeration e = msg.getAllHeaderLines();
while (e.hasMoreElements()) {
attached.addHeaderLine((String) e.nextElement());
}
attached.setContent("", msg.getContentType());
attached.saveChanges();
}
SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
out.setHeader("Auto-Submitted", "auto-replied (zimbra; intercept)");
InternetAddress address = new JavaMailInternetAddress(from);
out.setFrom(address);
address = new JavaMailInternetAddress(interceptAddress);
out.setRecipient(javax.mail.Message.RecipientType.TO, address);
String charset = getCharset(account, subject);
out.setSubject(subject, charset);
charset = getCharset(account, bodyText);
MimeMultipart multi = new ZMimeMultipart();
// Add message body
MimeBodyPart part = new ZMimeBodyPart();
part.setText(bodyText, charset);
multi.addBodyPart(part);
// Add original message
MimeBodyPart part2 = new ZMimeBodyPart();
part2.setContent(attached, MimeConstants.CT_MESSAGE_RFC822);
multi.addBodyPart(part2);
out.setContent(multi);
String envFrom = "<>";
out.setEnvelopeFrom(envFrom);
out.saveChanges();
Transport.send(out);
// clean up after ourselves...
multi.removeBodyPart(part2);
} catch (MessagingException e) {
ZimbraLog.lmtp.warn("Unable to send intercept message to %s.", interceptAddress, e);
}
}
}
Aggregations