diff --git a/extensions/modules/src/org/exist/xquery/modules/mail/MessageListFunctions.java b/extensions/modules/src/org/exist/xquery/modules/mail/MessageListFunctions.java index 4dbe4619cc1..c6c9d3ae180 100644 --- a/extensions/modules/src/org/exist/xquery/modules/mail/MessageListFunctions.java +++ b/extensions/modules/src/org/exist/xquery/modules/mail/MessageListFunctions.java @@ -271,9 +271,9 @@ private void prefetchMessages( Folder folder, Message[] msgList ) throws Messagi FetchProfile fp = new FetchProfile(); fp.add( FetchProfile.Item.ENVELOPE ); - for( int i = 0; i < PREFETCH_HEADERS.length; i++ ) { - fp.add( PREFETCH_HEADERS[i] ); - } + for (String PREFETCH_HEADERS : PREFETCH_HEADERS) { + fp.add(PREFETCH_HEADERS); + } folder.fetch( msgList, fp ); } @@ -337,84 +337,82 @@ private Sequence getMessageListAsXML( Sequence[] args, Sequence contextSequence // Recipients builder.startElement( new QName( "recipients", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); // To Recipients - Address[] to = message.getRecipients( Message.RecipientType.TO ); - if( to != null ) { - for( int j = 0; j < to.length; j++ ) { - builder.startElement( new QName( "recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "to" ); - builder.characters( to[j].toString() ); - builder.endElement(); - } + Address[] toAddresses = message.getRecipients( Message.RecipientType.TO ); + if( toAddresses != null ) { + for (Address to : toAddresses) { + builder.startElement( new QName( "recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "to" ); + builder.characters(to.toString()); + builder.endElement(); + } } // cc Recipients - Address[] cc = message.getRecipients( Message.RecipientType.CC ); - if( cc != null ) { - for( int j = 0; j < cc.length; j++ ) { - builder.startElement( new QName( "recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "cc" ); - builder.characters( cc[j].toString() ); - builder.endElement(); - } + Address[] ccAddresses = message.getRecipients( Message.RecipientType.CC ); + if( ccAddresses != null ) { + for (Address ccAddress : ccAddresses) { + builder.startElement( new QName( "recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "cc" ); + builder.characters(ccAddress.toString()); + builder.endElement(); + } } // bcc Recipients Address[] bccAddresses = message.getRecipients( Message.RecipientType.BCC ); if( bccAddresses != null ) { for (Address bccAddress : bccAddresses) { - builder.startElement( new QName( "recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "bcc" ); + builder.startElement( new QName( "recipient", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "bcc" ); builder.characters(bccAddress.toString()); - builder.endElement(); - } + builder.endElement(); + } } builder.endElement(); // Flags Flags flags = message.getFlags(); - Flags.Flag[] sf = flags.getSystemFlags(); - String[] uf = flags.getUserFlags(); + Flags.Flag[] systemFlags = flags.getSystemFlags(); + String[] userFlags = flags.getUserFlags(); - if( sf.length > 0 || uf.length > 0 ) { + if( systemFlags.length > 0 || userFlags.length > 0 ) { builder.startElement( new QName( "flags", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - // System Flags - for( int f = 0; f < sf.length; f++ ) { - if( sf[f] == Flags.Flag.ANSWERED ) { - builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "answered" ); - builder.endElement(); - } else if( sf[f] == Flags.Flag.DELETED ) { - builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "deleted" ); - builder.endElement(); - } else if( sf[f] == Flags.Flag.DRAFT ) { - builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "draft" ); - builder.endElement(); - } else if( sf[f] == Flags.Flag.FLAGGED ) { - builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "flagged" ); - builder.endElement(); - } else if( sf[f] == Flags.Flag.RECENT ) { - builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "recent" ); - builder.endElement(); - } else if( sf[f] == Flags.Flag.SEEN ) { - builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "seen" ); - builder.endElement(); - } - } + for (Flags.Flag systemFlag : systemFlags) { + if (systemFlag == Flags.Flag.ANSWERED) { + builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "answered" ); + builder.endElement(); + } else if (systemFlag == Flags.Flag.DELETED) { + builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "deleted" ); + builder.endElement(); + } else if (systemFlag == Flags.Flag.DRAFT) { + builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "draft" ); + builder.endElement(); + } else if (systemFlag == Flags.Flag.FLAGGED) { + builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "flagged" ); + builder.endElement(); + } else if (systemFlag == Flags.Flag.RECENT) { + builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "recent" ); + builder.endElement(); + } else if (systemFlag == Flags.Flag.SEEN) { + builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "seen" ); + builder.endElement(); + } + } - // User Flags - for( int f = 0; f < uf.length; f++ ) { - builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); - builder.addAttribute( new QName( "type", null, null ), "user" ); - builder.addAttribute( new QName( "value", null, null ), uf[ f ] ); - builder.endElement(); - } + for (String userFlag : userFlags) { + builder.startElement( new QName( "flag", MailModule.NAMESPACE_URI, MailModule.PREFIX ), null ); + builder.addAttribute( new QName( "type", null, null ), "user" ); + builder.addAttribute(new QName( "value", null, null ), userFlag); + builder.endElement(); + } builder.endElement(); } diff --git a/extensions/modules/src/org/exist/xquery/modules/mail/SendEmailFunction.java b/extensions/modules/src/org/exist/xquery/modules/mail/SendEmailFunction.java index ba87622dc79..7e33c61cc65 100644 --- a/extensions/modules/src/org/exist/xquery/modules/mail/SendEmailFunction.java +++ b/extensions/modules/src/org/exist/xquery/modules/mail/SendEmailFunction.java @@ -363,7 +363,7 @@ private List sendBySMTP(List mails, String SMTPServer) throws SMT //First line sent to us from the SMTP server should be "220 blah blah", 220 indicates okay smtpResult = smtpIn.readLine(); - if(!smtpResult.substring(0, 3).toString().equals("220")) + if(!smtpResult.substring(0, 3).equals("220")) { String errMsg = "Error - SMTP Server not ready: '" + smtpResult + "'"; LOG.error(errMsg); @@ -376,7 +376,7 @@ private List sendBySMTP(List mails, String SMTPServer) throws SMT //get "HELLO" response, should be "250 blah blah" smtpResult = smtpIn.readLine(); - if(!smtpResult.substring(0, 3).toString().equals("250")) + if(!smtpResult.substring(0, 3).equals("250")) { String errMsg = "Error - SMTP HELO Failed: '" + smtpResult + "'"; LOG.error(errMsg); @@ -441,36 +441,31 @@ private boolean writeSMTPMessage(Mail mail, PrintWriter smtpOut, BufferedReader //Get "MAIL FROM:" response smtpResult = smtpIn.readLine(); - if(!smtpResult.substring(0, 3).toString().equals("250")) + if(!smtpResult.substring(0, 3).equals("250")) { LOG.error("Error - SMTP MAIL FROM failed: " + smtpResult); return false; } //RCPT TO should be issued for each to, cc and bcc recipient - List allrecipients = new ArrayList(); + List allrecipients = new ArrayList<>(); allrecipients.addAll(mail.getTo()); allrecipients.addAll(mail.getCC()); allrecipients.addAll(mail.getBCC()); - for(int x = 0; x < allrecipients.size(); x++) - { + for (String recipient : allrecipients) { //Send "RCPT TO:" //Check format of to address does it include a name as well as the email address? - if(((String)allrecipients.get(x)).indexOf("<") != -1) - { + if (((String) recipient).contains("<")) { //yes, just send the email address - smtpOut.println("RCPT TO:<" + ((String)allrecipients.get(x)).substring(((String)allrecipients.get(x)).indexOf("<") + 1, ((String)allrecipients.get(x)).indexOf(">")) + ">"); - } - else - { - smtpOut.println("RCPT TO:<" + ((String)allrecipients.get(x)) + ">"); + smtpOut.println("RCPT TO:<" + ((String) recipient).substring(((String) recipient).indexOf("<") + 1, ((String) recipient).indexOf(">")) + ">"); + } else { + smtpOut.println("RCPT TO:<" + ((String) recipient) + ">"); } smtpOut.flush(); - //Get "RCPT TO:" response smtpResult = smtpIn.readLine(); - if(!smtpResult.substring(0, 3).toString().equals("250")) + if(!smtpResult.substring(0, 3).equals("250")) { LOG.error("Error - SMTP RCPT TO failed: " + smtpResult); } @@ -483,7 +478,7 @@ private boolean writeSMTPMessage(Mail mail, PrintWriter smtpOut, BufferedReader //Get "DATA" response, should be "354 blah blah" smtpResult = smtpIn.readLine(); - if(!smtpResult.substring(0, 3).toString().equals("354")) + if(!smtpResult.substring(0, 3).equals("354")) { LOG.error("Error - SMTP DATA failed: " + smtpResult); return false; @@ -494,7 +489,7 @@ private boolean writeSMTPMessage(Mail mail, PrintWriter smtpOut, BufferedReader //Get end message response, should be "250 blah blah" smtpResult = smtpIn.readLine(); - if(!smtpResult.substring(0, 3).toString().equals("250")) + if(!smtpResult.substring(0, 3).equals("250")) { LOG.error("Error - Message not accepted: " + smtpResult); return false; @@ -584,14 +579,14 @@ else if(!aMail.getText().equals("") && !aMail.getXHTML().equals("")) } // TODO - need to put out a multipart/mixed boundary here when HTML, text and attachment present - if(!aMail.getText().toString().equals("") && !aMail.getXHTML().toString().equals("") && aMail.attachmentIterator().hasNext()) + if(!aMail.getText().equals("") && !aMail.getXHTML().equals("") && aMail.attachmentIterator().hasNext()) { out.println("Content-Type: multipart/alternative; boundary=\"" + MultipartBoundary + "_alt\";"); out.println("--" + MultipartBoundary + "_alt"); } //text email - if(!aMail.getText().toString().equals("")) + if(!aMail.getText().equals("")) { out.println("Content-Type: text/plain; charset=" + charset); out.println("Content-Transfer-Encoding: 8bit"); @@ -602,9 +597,9 @@ else if(!aMail.getText().equals("") && !aMail.getXHTML().equals("")) if(multipartBoundary != null) { - if(!aMail.getXHTML().toString().equals("") || aMail.attachmentIterator().hasNext()) + if(!aMail.getXHTML().equals("") || aMail.attachmentIterator().hasNext()) { - if(!aMail.getText().toString().equals("") && !aMail.getXHTML().toString().equals("") && aMail.attachmentIterator().hasNext()) + if(!aMail.getText().equals("") && !aMail.getXHTML().equals("") && aMail.attachmentIterator().hasNext()) { out.println("--" + MultipartBoundary + "_alt"); } @@ -615,7 +610,7 @@ else if(!aMail.getText().equals("") && !aMail.getXHTML().equals("")) } else { - if(!aMail.getText().toString().equals("") && !aMail.getXHTML().toString().equals("") && aMail.attachmentIterator().hasNext()) + if(!aMail.getText().equals("") && !aMail.getXHTML().equals("") && aMail.attachmentIterator().hasNext()) { out.println("--" + MultipartBoundary + "_alt--"); } @@ -628,7 +623,7 @@ else if(!aMail.getText().equals("") && !aMail.getXHTML().equals("")) } //HTML email - if(!aMail.getXHTML().toString().equals("")) + if(!aMail.getXHTML().equals("")) { out.println("Content-Type: text/html; charset=" + charset); out.println("Content-Transfer-Encoding: 8bit"); @@ -642,7 +637,7 @@ else if(!aMail.getText().equals("") && !aMail.getXHTML().equals("")) { if(aMail.attachmentIterator().hasNext()) { - if(!aMail.getText().toString().equals("") && !aMail.getXHTML().toString().equals("") && aMail.attachmentIterator().hasNext()) + if(!aMail.getText().equals("") && !aMail.getXHTML().equals("") && aMail.attachmentIterator().hasNext()) { out.println("--" + MultipartBoundary + "_alt--"); out.println("--" + multipartBoundary); @@ -654,7 +649,7 @@ else if(!aMail.getText().equals("") && !aMail.getXHTML().equals("")) } else { - if(!aMail.getText().toString().equals("") && !aMail.getXHTML().toString().equals("") && aMail.attachmentIterator().hasNext()) + if(!aMail.getText().equals("") && !aMail.getXHTML().equals("") && aMail.attachmentIterator().hasNext()) { out.println("--" + MultipartBoundary + "_alt--"); } @@ -741,7 +736,7 @@ private String eXistVersion() throws IOException */ private List parseMailElement(List mailElements) throws TransformerException { - List mails = new ArrayList(); + List mails = new ArrayList<>(); for(Element mailElement : mailElements) { @@ -758,63 +753,55 @@ private List parseMailElement(List mailElements) throws Transform //Parse each of the child nodes if(child.getNodeType() == Node.ELEMENT_NODE && child.hasChildNodes()) { - if(child.getLocalName().equals("from")) - { + switch (child.getLocalName()) { + case "from": mail.setFrom(child.getFirstChild().getNodeValue()); - } - if(child.getLocalName().equals("reply-to")) - { + break; + case "reply-to": mail.setReplyTo(child.getFirstChild().getNodeValue()); - } - else if(child.getLocalName().equals("to")) - { + break; + case "to": mail.addTo(child.getFirstChild().getNodeValue()); - } - else if(child.getLocalName().equals("cc")) - { + break; + case "cc": mail.addCC(child.getFirstChild().getNodeValue()); - } - else if(child.getLocalName().equals("bcc")) - { + break; + case "bcc": mail.addBCC(child.getFirstChild().getNodeValue()); - } - else if(child.getLocalName().equals("subject")) - { + break; + case "subject": mail.setSubject(child.getFirstChild().getNodeValue()); - } - else if(child.getLocalName().equals("message")) - { - //If the message node, then parse the child text and xhtml nodes - Node bodyPart = child.getFirstChild(); - while(bodyPart != null) - { - if(bodyPart.getLocalName().equals("text")) + break; + case "message": + //If the message node, then parse the child text and xhtml nodes + Node bodyPart = child.getFirstChild(); + while(bodyPart != null) { + if(bodyPart.getLocalName().equals("text")) + { mail.setText(bodyPart.getFirstChild().getNodeValue()); - } - else if(bodyPart.getLocalName().equals("xhtml")) - { - //Convert everything inside to text - TransformerFactory transFactory = TransformerFactory.newInstance(); - Transformer transformer = transFactory.newTransformer(); - DOMSource source = new DOMSource(bodyPart.getFirstChild()); - StringWriter strWriter = new StringWriter(); - StreamResult result = new StreamResult(strWriter); - transformer.transform(source, result); - - mail.setXHTML(strWriter.toString()); - } - - //next body part - bodyPart = bodyPart.getNextSibling(); - } - - } - else if(child.getLocalName().equals("attachment")) - { - Element attachment = (Element)child; - MailAttachment ma = new MailAttachment(attachment.getAttribute("filename"), attachment.getAttribute("mimetype"), attachment.getFirstChild().getNodeValue()); - mail.addAttachment(ma); + } + else if(bodyPart.getLocalName().equals("xhtml")) + { + //Convert everything inside to text + TransformerFactory transFactory = TransformerFactory.newInstance(); + Transformer transformer = transFactory.newTransformer(); + DOMSource source = new DOMSource(bodyPart.getFirstChild()); + StringWriter strWriter = new StringWriter(); + StreamResult result = new StreamResult(strWriter); + transformer.transform(source, result); + + mail.setXHTML(strWriter.toString()); + } + + //next body part + bodyPart = bodyPart.getNextSibling(); + } break; + case "attachment": + Element attachment = (Element)child; + MailAttachment ma = new MailAttachment(attachment.getAttribute("filename"), attachment.getAttribute("mimetype"), attachment.getFirstChild().getNodeValue()); + mail.addAttachment(ma); + break; } } @@ -854,7 +841,7 @@ else if(child.getLocalName().equals("attachment")) */ private List parseMessageElement(Session session, List mailElements) throws IOException, MessagingException, TransformerException { - List mails = new ArrayList(); + List mails = new ArrayList<>(); for (Element mailElement : mailElements) { //Make sure that message has a Mail node @@ -863,11 +850,11 @@ private List parseMessageElement(Session session, List mailEle // create a message MimeMessage msg = new MimeMessage(session); - ArrayList replyTo = new ArrayList(); + ArrayList replyTo = new ArrayList<>(); boolean fromWasSet = false; MimeBodyPart body = null; Multipart multibody = null; - ArrayList attachments = new ArrayList(); + ArrayList attachments = new ArrayList<>(); String firstContent = null; String firstContentType = null; String firstCharset = null; @@ -878,128 +865,132 @@ private List parseMessageElement(Session session, List mailEle while (child != null) { //Parse each of the child nodes if (child.getNodeType() == Node.ELEMENT_NODE && child.hasChildNodes()) { - if (child.getLocalName().equals("from")) { - // set the from and to address - InternetAddress[] addressFrom = {new InternetAddress(child.getFirstChild().getNodeValue())}; - msg.addFrom(addressFrom); - - fromWasSet = true; - } else if (child.getLocalName().equals("reply-to")) { - // As we can only set the reply-to, not add them, let's keep - // all of them in a list - replyTo.add(new InternetAddress(child.getFirstChild().getNodeValue())); - msg.setReplyTo(replyTo.toArray(new InternetAddress[0])); - } else if (child.getLocalName().equals("to")) { - msg.addRecipient(Message.RecipientType.TO, new InternetAddress(child.getFirstChild().getNodeValue())); - } else if (child.getLocalName().equals("cc")) { - msg.addRecipient(Message.RecipientType.CC, new InternetAddress(child.getFirstChild().getNodeValue())); - } else if (child.getLocalName().equals("bcc")) { - msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(child.getFirstChild().getNodeValue())); - } else if (child.getLocalName().equals("subject")) { - msg.setSubject(child.getFirstChild().getNodeValue()); - } else if (child.getLocalName().equals("header")) { - // Optional : You can also set your custom headers in the Email if you Want - msg.addHeader(((Element) child).getAttribute("name"), child.getFirstChild().getNodeValue()); - } else if (child.getLocalName().equals("message")) { - //If the message node, then parse the child text and xhtml nodes - Node bodyPart = child.getFirstChild(); - while (bodyPart != null) { - if (bodyPart.getNodeType() != Node.ELEMENT_NODE) - continue; - - Element elementBodyPart = (Element) bodyPart; - String content = null; - String contentType = null; - - if (bodyPart.getLocalName().equals("text")) { - // Setting the Subject and Content Type - content = bodyPart.getFirstChild().getNodeValue(); - contentType = "plain"; - } else if (bodyPart.getLocalName().equals("xhtml")) { - //Convert everything inside to text - TransformerFactory transFactory = TransformerFactory.newInstance(); - Transformer transformer = transFactory.newTransformer(); - DOMSource source = new DOMSource(bodyPart.getFirstChild()); - StringWriter strWriter = new StringWriter(); - StreamResult result = new StreamResult(strWriter); - transformer.transform(source, result); - - content = strWriter.toString(); - contentType = "html"; - } else if (bodyPart.getLocalName().equals("generic")) { - // Setting the Subject and Content Type - content = elementBodyPart.getFirstChild().getNodeValue(); - contentType = elementBodyPart.getAttribute("type"); - } - - // Now, time to store it - if (content != null && contentType != null && contentType.length() > 0) { - String charset = elementBodyPart.getAttribute("charset"); - String encoding = elementBodyPart.getAttribute("encoding"); - - if (body != null && multibody == null) { - multibody = new MimeMultipart("alternative"); - multibody.addBodyPart(body); + switch (child.getLocalName()) { + case "from": + // set the from and to address + InternetAddress[] addressFrom = {new InternetAddress(child.getFirstChild().getNodeValue())}; + msg.addFrom(addressFrom); + fromWasSet = true; + break; + case "reply-to": + // As we can only set the reply-to, not add them, let's keep + // all of them in a list + replyTo.add(new InternetAddress(child.getFirstChild().getNodeValue())); + msg.setReplyTo(replyTo.toArray(new InternetAddress[0])); + break; + case "to": + msg.addRecipient(Message.RecipientType.TO, new InternetAddress(child.getFirstChild().getNodeValue())); + break; + case "cc": + msg.addRecipient(Message.RecipientType.CC, new InternetAddress(child.getFirstChild().getNodeValue())); + break; + case "bcc": + msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(child.getFirstChild().getNodeValue())); + break; + case "subject": + msg.setSubject(child.getFirstChild().getNodeValue()); + break; + case "header": + // Optional : You can also set your custom headers in the Email if you Want + msg.addHeader(((Element) child).getAttribute("name"), child.getFirstChild().getNodeValue()); + break; + case "message": + //If the message node, then parse the child text and xhtml nodes + Node bodyPart = child.getFirstChild(); + while (bodyPart != null) { + if (bodyPart.getNodeType() != Node.ELEMENT_NODE) + continue; + + Element elementBodyPart = (Element) bodyPart; + String content = null; + String contentType = null; + + if (bodyPart.getLocalName().equals("text")) { + // Setting the Subject and Content Type + content = bodyPart.getFirstChild().getNodeValue(); + contentType = "plain"; + } else if (bodyPart.getLocalName().equals("xhtml")) { + //Convert everything inside to text + TransformerFactory transFactory = TransformerFactory.newInstance(); + Transformer transformer = transFactory.newTransformer(); + DOMSource source = new DOMSource(bodyPart.getFirstChild()); + StringWriter strWriter = new StringWriter(); + StreamResult result = new StreamResult(strWriter); + transformer.transform(source, result); + + content = strWriter.toString(); + contentType = "html"; + } else if (bodyPart.getLocalName().equals("generic")) { + // Setting the Subject and Content Type + content = elementBodyPart.getFirstChild().getNodeValue(); + contentType = elementBodyPart.getAttribute("type"); } - - if (charset == null || charset.length() == 0) { - charset = "UTF-8"; - } - - if (encoding == null || encoding.length() == 0) { - encoding = "quoted-printable"; - } - - if (body == null) { - firstContent = content; - firstCharset = charset; - firstContentType = contentType; - firstEncoding = encoding; + + // Now, time to store it + if (content != null && contentType != null && contentType.length() > 0) { + String charset = elementBodyPart.getAttribute("charset"); + String encoding = elementBodyPart.getAttribute("encoding"); + + if (body != null && multibody == null) { + multibody = new MimeMultipart("alternative"); + multibody.addBodyPart(body); + } + + if (charset == null || charset.length() == 0) { + charset = "UTF-8"; + } + + if (encoding == null || encoding.length() == 0) { + encoding = "quoted-printable"; + } + + if (body == null) { + firstContent = content; + firstCharset = charset; + firstContentType = contentType; + firstEncoding = encoding; + } + body = new MimeBodyPart(); + body.setText(content, charset, contentType); + if (encoding != null) { + body.setHeader("Content-Transfer-Encoding", encoding); + } + if (multibody != null) + multibody.addBodyPart(body); } - body = new MimeBodyPart(); - body.setText(content, charset, contentType); - if (encoding != null) { - body.setHeader("Content-Transfer-Encoding", encoding); - } - if (multibody != null) - multibody.addBodyPart(body); - } - - //next body part - bodyPart = bodyPart.getNextSibling(); - } - - } else if (child.getLocalName().equals("attachment")) { - Element attachment = (Element) child; - MimeBodyPart part; - // if mimetype indicates a binary resource, assume the content is base64 encoded - if (MimeTable.getInstance().isTextContent(attachment.getAttribute("mimetype"))) { - part = new MimeBodyPart(); - } else { - part = new PreencodedMimeBodyPart("base64"); - } - - StringBuilder content = new StringBuilder(); - Node attachChild = attachment.getFirstChild(); - while (attachChild != null) { - if (attachChild.getNodeType() == Node.ELEMENT_NODE) { - TransformerFactory transFactory = TransformerFactory.newInstance(); - Transformer transformer = transFactory.newTransformer(); - DOMSource source = new DOMSource(attachChild); - StringWriter strWriter = new StringWriter(); - StreamResult result = new StreamResult(strWriter); - transformer.transform(source, result); - - content.append(strWriter.toString()); + + //next body part + bodyPart = bodyPart.getNextSibling(); + } break; + case "attachment": + Element attachment = (Element) child; + MimeBodyPart part; + // if mimetype indicates a binary resource, assume the content is base64 encoded + if (MimeTable.getInstance().isTextContent(attachment.getAttribute("mimetype"))) { + part = new MimeBodyPart(); } else { - content.append(attachChild.getNodeValue()); - } - attachChild = attachChild.getNextSibling(); - } - part.setDataHandler(new DataHandler(new ByteArrayDataSource(content.toString(), attachment.getAttribute("mimetype")))); + part = new PreencodedMimeBodyPart("base64"); + } StringBuilder content = new StringBuilder(); + Node attachChild = attachment.getFirstChild(); + while (attachChild != null) { + if (attachChild.getNodeType() == Node.ELEMENT_NODE) { + TransformerFactory transFactory = TransformerFactory.newInstance(); + Transformer transformer = transFactory.newTransformer(); + DOMSource source = new DOMSource(attachChild); + StringWriter strWriter = new StringWriter(); + StreamResult result = new StreamResult(strWriter); + transformer.transform(source, result); + + content.append(strWriter.toString()); + } else { + content.append(attachChild.getNodeValue()); + } + attachChild = attachChild.getNextSibling(); + } part.setDataHandler(new DataHandler(new ByteArrayDataSource(content.toString(), attachment.getAttribute("mimetype")))); part.setFileName(attachment.getAttribute("filename")); // part.setHeader("Content-Transfer-Encoding", "base64"); - attachments.add(part); + attachments.add(part); + break; } } @@ -1302,13 +1293,13 @@ private class Mail { private String from = ""; //Who is the mail from private String replyTo = null; //Who should you reply to - private List to = new ArrayList(); //Who is the mail going to - private List cc = new ArrayList(); //Carbon Copy to - private List bcc = new ArrayList(); //Blind Carbon Copy to + private final List to = new ArrayList<>(); //Who is the mail going to + private final List cc = new ArrayList<>(); //Carbon Copy to + private final List bcc = new ArrayList<>(); //Blind Carbon Copy to private String subject = ""; //Subject of the mail private String text = ""; //Body text of the mail private String xhtml = ""; //Body XHTML of the mail - private List attachment = new ArrayList(); //Any attachments + private final List attachment = new ArrayList<>(); //Any attachments //From public void setFrom(String from)