use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class ZMimeMultipart method writeTo.
// JavaMail makes it impossible to set MimeMultipart.allowEmpty if you don't use their parser, and that
// means an empty multipart always throws an exception on MimeMultipart.writeTo regardless of what
// "mail.mime.multipart.allowempty" is set to. So we have to copy the whole method from the superclass
// just so we can strip that conditional out.
@Override
public synchronized void writeTo(OutputStream os) throws IOException, MessagingException {
if (ZPARSER) {
parse();
String boundary = "--" + getBoundary();
LineOutputStream los = new LineOutputStream(os);
// if there's a preamble, write it out
String preamble = getPreamble();
if (preamble != null) {
byte[] pb = ASCIIUtility.getBytes(preamble);
los.write(pb);
// make sure it ends with a newline
if (pb.length > 0 && !(pb[pb.length - 1] == '\r' || pb[pb.length - 1] == '\n')) {
los.writeln();
}
// XXX - could force a blank line before start boundary
}
if (parts.size() == 0) {
// write out a single empty body part
// put out boundary
los.writeln(boundary);
// put out empty line
los.writeln();
} else {
for (int i = 0; i < parts.size(); i++) {
// put out boundary
los.writeln(boundary);
((MimeBodyPart) parts.elementAt(i)).writeTo(os);
// put out empty line
los.writeln();
}
}
// put out last boundary
los.writeln(boundary + "--");
} else {
super.writeTo(os);
}
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class ParseMimeMessageTest method attachZimbraDocument.
@Test
public void attachZimbraDocument() throws Exception {
Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
OperationContext octxt = new OperationContext(acct);
Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_APPLICATION_ZIMBRA_DOC, "author", "description", new ByteArrayInputStream("test123".getBytes()));
Element el = new Element.JSONElement(MailConstants.E_MSG);
el.addAttribute(MailConstants.E_SUBJECT, "attach message");
el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
el.addElement(MailConstants.E_ATTACH).addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
ZimbraSoapContext zsc = getMockSoapContext();
MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
MimeMultipart mmp = (MimeMultipart) mm.getContent();
MimeBodyPart part = (MimeBodyPart) mmp.getBodyPart(1);
Assert.assertEquals(MimeConstants.CT_TEXT_HTML, new ContentType(part.getContentType()).getContentType());
}
use of javax.mail.internet.MimeBodyPart in project zm-mailbox by Zimbra.
the class ParseMimeMessageTest method staleReference.
@Test
public void staleReference() throws Exception {
Account acct = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
// first, create the original draft
OperationContext octxt = new OperationContext(acct);
Document doc = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test1", 8192));
Document doc2 = mbox.createDocument(octxt, Mailbox.ID_FOLDER_BRIEFCASE, "testdoc2", MimeConstants.CT_TEXT_PLAIN, null, null, randomContent("test2", 8192));
Element el = new Element.JSONElement(MailConstants.E_MSG), attach;
el.addAttribute(MailConstants.E_SUBJECT, "has attachment");
el.addElement(MailConstants.E_EMAIL).addAttribute(MailConstants.A_ADDRESS_TYPE, EmailType.TO.toString()).addAttribute(MailConstants.A_ADDRESS, "rcpt@zimbra.com");
el.addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_CONTENT_TYPE, "text/plain").addAttribute(MailConstants.E_CONTENT, "This is the content.");
attach = el.addElement(MailConstants.E_ATTACH);
attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc.getId());
attach.addElement(MailConstants.E_DOC).addAttribute(MailConstants.A_ID, doc2.getId());
ZimbraSoapContext zsc = getMockSoapContext();
MimeMessage mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
Message draft = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
// then, create a new draft that references one of the original draft's attachments
attach.detach();
(attach = el.addElement(MailConstants.E_ATTACH)).addElement(MailConstants.E_MIMEPART).addAttribute(MailConstants.A_MESSAGE_ID, draft.getId()).addAttribute(MailConstants.A_PART, "3");
mm = ParseMimeMessage.parseMimeMsgSoap(zsc, octxt, null, el, null, new ParseMimeMessage.MimeMessageData());
// delete the draft itself and then try to save the new draft
mbox.delete(octxt, draft.getId(), MailItem.Type.MESSAGE);
Message draft2 = mbox.saveDraft(octxt, new ParsedMessage(mm, false), -1);
// check that the attachment's content is present and correct
MimeMultipart multi = (MimeMultipart) (draft2.getMimeMessage().getContent());
Assert.assertEquals("2 parts in draft", 2, multi.getCount());
Assert.assertEquals("attached part content", "test2", firstLine((MimeBodyPart) multi.getBodyPart(1)));
}
use of javax.mail.internet.MimeBodyPart 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.internet.MimeBodyPart in project perun by CESNET.
the class MessagePreparator method prepare.
/*
* (non-Javadoc)
*
* @see org.springframework.mail.javamail.MimeMessagePreparator#prepare(javax.mail.internet.MimeMessage)
*/
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMultipart mpRoot = new MimeMultipart("mixed");
Multipart mp = new MimeMultipart("alternative");
// Create a body part to house the multipart/alternative Part
MimeBodyPart contentPartRoot = new MimeBodyPart();
contentPartRoot.setContent(mp);
// Add the root body part to the root multipart
mpRoot.addBodyPart(contentPartRoot);
// adding recipients, cc and bcc
if (getTo() != null) {
for (String to : getTo()) {
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
}
}
if (getCc() != null) {
for (String cc : getCc()) {
mimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
}
}
if (getBcc() != null) {
for (String bcc : getBcc()) {
mimeMessage.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
}
}
// adding from and subject
mimeMessage.setFrom(new InternetAddress(getFrom(), getFromText()));
mimeMessage.setSubject(getSubject());
if (getEmailType().equals(EmailType.HTML)) {
mp.addBodyPart(createHtmlMessage());
} else if (getEmailType().equals(EmailType.PLAIN)) {
mp.addBodyPart(createTextMessage());
}
// Create an "ATTACHMENT" - we must put it to mpRoot(mixed content)
if (getFileNames() != null) {
for (String filename : getFileNames()) {
mpRoot.addBodyPart(createAttachment(filename));
}
}
mimeMessage.setContent(mpRoot);
logger.debug("Message is prepared to send");
}
Aggregations