Examples of SMTPMessage


Examples of com.sun.mail.smtp.SMTPMessage

    }

  if (sEncoding==null) sEncoding="ASCII";

    SMTPMessage oSentMessage = new SMTPMessage(oMailSession);

    Multipart oDraftParts = getParts();
    final int iDraftParts = oDraftParts.getCount();

    if (DebugFile.trace) DebugFile.writeln("Multipart.getCount() = " + String.valueOf(iDraftParts));

    MimeBodyPart oMsgPlainText = new MimeBodyPart();
    MimeMultipart oSentMsgParts = new MimeMultipart("mixed");

    if (sContentType.equalsIgnoreCase("html")) {

      MimeMultipart oHtmlRelated  = new MimeMultipart("related");
      MimeMultipart oTextHtmlAlt  = new MimeMultipart("alternative");

      // ************************************************************************
      // Replace image CIDs

      String sSrc, sCid, sText = "";

      Parser oPrsr = Parser.createParser(sBody, getEncoding());

      // String sCid, sSrc;

      HtmlMimeBodyPart oHtmBdy = new HtmlMimeBodyPart(sBody, getEncoding());

      try {

        // ****************************
        // Extract plain text from HTML
        if (DebugFile.trace) DebugFile.writeln("new StringBean()");

        StringBean oStrBn = new StringBean();

        try {
          oPrsr.visitAllNodesWith (oStrBn);
        } catch (ParserException pe) {
          if (DebugFile.trace) {
            DebugFile.writeln("org.htmlparser.util.ParserException " + pe.getMessage());
          }
          throw new MessagingException(pe.getMessage(), pe);
        }

        sText = oStrBn.getStrings();

        oStrBn = null;

        // *******************************
        // Set plain text alternative part

        oMsgPlainText.setDisposition("inline");
        oMsgPlainText.setText(sText,Charset.forName(sEncoding).name(),"plain");
        if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/alternative).addBodyPart(text/plain)");
        oTextHtmlAlt.addBodyPart(oMsgPlainText);

        // *****************************************
        // Iterate images from HTML and replace CIDs

    if (bAttachInlineImages) {
      sBody = oHtmBdy.addPreffixToImgSrc("cid:");         
    } // fi (bAttachInlineImages)
      }
      catch (ParserException pe) {
        if (DebugFile.trace) {
          DebugFile.writeln("org.htmlparser.util.ParserException " + pe.getMessage());
        }
        throw new MessagingException(pe.getMessage(), pe);
      }
      // End replace image CIDs
      // ************************************************************************


      // ************************************************************************
      // Some defensive programming: ensure that all src="..." attributes point
      // either to an absolute http:// URL or to a cid:
      oHtmBdy.setHtml (sBody);
      ArrayList<String> aLocalUrls = oHtmBdy.extractLocalUrls();
      if (aLocalUrls.size()>0) {
        if (DebugFile.trace) {
          DebugFile.writeln("HTML body part contains local references to external resources");
          for (String i : aLocalUrls) {
            DebugFile.writeln(i);
          }
          DebugFile.write(sBody+"\n");
        }
        throw new MessagingException("HTML body part "+(sId==null ? "" : "of message"+sId)+" contains local references to external resources such as "+aLocalUrls.get(0));
      } // aLocalUrls != {}

     
      // ************************************************************************
      // Add HTML related images

      if (oHtmBdy.getImagesCids().isEmpty()) {
          // Set HTML part
          MimeBodyPart oMsgHtml = new MimeBodyPart();
          oMsgHtml.setDisposition("inline");

          // ****************************************************************************
          // Replace <!--WEBBEACON SRC="http://..."--> tag by an <IMG SRC="http://..." />

        try {
            iWebBeaconStart = Gadgets.indexOfIgnoreCase(sBody,"<!--WEBBEACON", 0);
            if (iWebBeaconStart>0) {
              iSrcTagStart = sBody.indexOf('"', iWebBeaconStart+13);
              iSrcTagEnd = sBody.indexOf('"', iSrcTagStart+1);
              iWebBeaconEnd = sBody.indexOf("-->", iWebBeaconStart+13);
              if (iWebBeaconEnd>0) {
            sBody = sBody.substring(0,iWebBeaconStart)+"<IMG SRC=\""+sBody.substring(iSrcTagStart+1,iSrcTagEnd)+"\" WIDTH=\"1\" HEIGHT=\"1\" BORDER=\"0\" ALT=\"\" />"+sBody.substring(iWebBeaconEnd+3);
              }
            }
        } catch (Exception malformedwebbeacon) {
          if (DebugFile.trace) DebugFile.writeln("Malformed Web Beacon");
        }
         
          oMsgHtml.setText(sBody,Charset.forName(sEncoding).name(),"html");
          oTextHtmlAlt.addBodyPart(oMsgHtml);
         
      } else {

        // Set HTML text related part

        MimeBodyPart oMsgHtmlText = new MimeBodyPart();
        oMsgHtmlText.setDisposition("inline");

        // ****************************************************************************
        // Replace <!--WEBBEACON SRC="http://..."--> tag by an <IMG SRC="http://..." />
         
        try {
          iWebBeaconStart = Gadgets.indexOfIgnoreCase(sBody,"<!--WEBBEACON", 0);
          if (iWebBeaconStart>0) {
            iSrcTagStart = sBody.indexOf('"', iWebBeaconStart+13);
            iSrcTagEnd = sBody.indexOf('"', iSrcTagStart+1);
            iWebBeaconEnd = sBody.indexOf("-->", iWebBeaconStart+13);
            if (iWebBeaconEnd>0) {
            sBody = sBody.substring(0,iWebBeaconStart)+"<IMG SRC=\""+sBody.substring(iSrcTagStart+1,iSrcTagEnd)+"\" WIDTH=\"1\" HEIGHT=\"1\" BORDER=\"0\" ALT=\"\" />"+sBody.substring(iWebBeaconEnd+3);
            }
          }
      } catch (Exception malformedwebbeacon) {
          if (DebugFile.trace) DebugFile.writeln("Malformed Web Beacon");
      }
       
        oMsgHtmlText.setText(sBody,Charset.forName(sEncoding).name(),"html");
        if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/related).addBodyPart(text/html)");
        oHtmlRelated.addBodyPart(oMsgHtmlText);

        // Set HTML text related inline images

        Iterator oImgs = oHtmBdy.getImagesCids().keySet().iterator();

        while (oImgs.hasNext()) {
          BodyPart oImgBodyPart = new MimeBodyPart();

          sSrc = (String) oImgs.next();
          sCid = (String) oHtmBdy.getImagesCids().get(sSrc);

          if (sSrc.startsWith("www."))
            sSrc = "http://" + sSrc;

          if (sSrc.startsWith("http://") || sSrc.startsWith("https://")) {
            oImgBodyPart.setDataHandler(new DataHandler(new URL(Hosts.resolve(sSrc))));
          }
          else {
            oImgBodyPart.setDataHandler(new DataHandler(new FileDataSource(sSrc)));
          }
     
      if (sSrc.endsWith(".png")) oImgBodyPart.setHeader("Content-Type", "image/png;name="+sCid);
          oImgBodyPart.setDisposition("inline");
          oImgBodyPart.setHeader("Content-ID", sCid);
          oImgBodyPart.setFileName(sCid);

          // Add image to multi-part
          if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/related).addBodyPart("+sCid+")");
          oHtmlRelated.addBodyPart(oImgBodyPart);
        } // wend

        // Set html text alternative part (html text + inline images)
        MimeBodyPart oTextHtmlRelated = new MimeBodyPart();
        oTextHtmlRelated.setContent(oHtmlRelated);
        if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/alternative).addBodyPart(multipart/related)");
        oTextHtmlAlt.addBodyPart(oTextHtmlRelated);
      }

      // ************************************************************************
      // Create message to be sent and add main text body to it

      if (0==iDraftParts) {
        oSentMessage.setContent(oTextHtmlAlt);
      } else {
        MimeBodyPart oMixedPart = new MimeBodyPart();
        oMixedPart.setContent(oTextHtmlAlt);
        oSentMsgParts.addBodyPart(oMixedPart);
      }

    } else { // (sContentType=="plain")

      // *************************************************
      // If this is a plain text message just add the text

      if (0==iDraftParts) {
        oSentMessage.setText(sBody, Charset.forName(sEncoding).name(), "plain");
      } else {
        oMsgPlainText.setDisposition("inline");
        oMsgPlainText.setText(sBody,Charset.forName(sEncoding).name(),"plain");
        if (DebugFile.trace) DebugFile.writeln("MimeBodyPart(multipart/mixed).addBodyPart(text/plain)");
        oSentMsgParts.addBodyPart(oMsgPlainText);
      }
    }
    // fi (sContentType=="html")

    // ************************************************************************
    // Add attachments to message to be sent

    if (iDraftParts>0) {

      for (int p=0; p<iDraftParts; p++) {
        DBMimePart oPart = (DBMimePart) oDraftParts.getBodyPart(p);

        String sDisposition = oPart.getDisposition();
        if (sDisposition==null)
          sDisposition = "inline";
        else if (sDisposition.equals("reference") || sDisposition.equals("pointer"))
          sDisposition = "attachment";

        int iSize = oPart.getSize();
        if (iSize<=0) iSize = 4000;
        InputStream oInStrm = oPart.getInputStream();
        ByteArrayOutputStream oByStrm = new java.io.ByteArrayOutputStream(iSize);
        new StreamPipe().between(oInStrm, oByStrm);
        oInStrm.close();

        if (DebugFile.trace) DebugFile.writeln("part " + String.valueOf(p) + " size is " + String.valueOf(oByStrm.size()));

        ByteArrayDataSource oDataSrc = new ByteArrayDataSource(oByStrm.toByteArray(), oPart.getContentType());
        MimeBodyPart oAttachment = new MimeBodyPart();
        oAttachment.setDisposition(sDisposition);
        if (sDisposition.equals("attachment"))
        if (null==oPart.getDescription())
          oAttachment.setFileName(oPart.getFileName());
        else
          oAttachment.setFileName(oPart.getDescription());
        oAttachment.setHeader("Content-Transfer-Encoding", "base64");
        oAttachment.setDataHandler(new DataHandler(oDataSrc));
        oSentMsgParts.addBodyPart(oAttachment);
      } // next
      oSentMessage.setContent(oSentMsgParts);
    } // fi (iDraftParts>0)

    if (null!=sSubject) oSentMessage.setSubject(sSubject);

    if (sId!=null)
      if (sId.trim().length()>0)
        oSentMessage.setContentID(sId);

    if (DebugFile.trace) {
      DebugFile.decIdent();
      DebugFile.writeln("End DBMimeMessage.composeFinalMessage()");
    }
View Full Code Here

Examples of com.sun.mail.smtp.SMTPMessage

    }

    // ***********************************
    // Once session is created set message

    SMTPMessage oSentMsg;

    try {
   
      // Personalize mail if needed by replacing {#...} tags by values taken from Atom fields
      String sFormat = oAtm.getStringNull(DB.id_format,"plain").toLowerCase();
      if (sFormat.equals("text") || sFormat.equals("txt")) sFormat = "plain";
      if (sFormat.equals("htm")) sFormat = "html";
     
    String sAttachInlineImages = getParameter("attachimages");
    if (sAttachInlineImages==null) sAttachInlineImages = Yes;
    boolean bAttachInlineImages = Yes.equals(sAttachInlineImages) || Activated.equals(sAttachInlineImages);

    String sEncoding = getParameter("encoding");
    if (sEncoding==null) sEncoding = "UTF-8";
   
    String sPBody;

    if (bHasCSSStyles==null) {
      bHasCSSStyles = new Boolean (!inlineStyles(sBody).equals(sBody));       
    }
   
    if (bHasCSSStyles.booleanValue()) {
        if (DebugFile.trace)
          DebugFile.writeln("Message body has linked CSS styles that have been inlined inside the HTML");
        if (bPersonalized)
          sPBody = personalizeBody(inlineStyles(sBody), this, oAtm);
        else
          sPBody = inlineStyles(sBody);
    } else {
        if (DebugFile.trace)
          DebugFile.writeln("Message body does not have linked CSS styles");
        if (bPersonalized)
          sPBody = personalizeBody(sBody, this, oAtm);
        else
          sPBody = sBody;
    }

    String sClickThrough = getParameter("clickthrough");
    if (sClickThrough==null) sClickThrough = No;
    if (Yes.equals(sClickThrough) || Activated.equals(sClickThrough)) {
      sPBody = redirectExternalLinks(sPBody,this,oAtm);
    }

    // Insert Web Beacon just before </BODY> tag
    String sWebBeacon = getParameter("webbeacon");
    if (sWebBeacon==null) sWebBeacon= No;
    if (Yes.equals(sWebBeacon) || Activated.equals(sWebBeacon)) {
      int iEndBody = Gadgets.indexOfIgnoreCase(sPBody, "</body>", 0);
      if (iEndBody>0) {
        String sWebBeaconDir = getProperty("webbeacon");       
        if (sWebBeaconDir==null) {
          sWebBeaconDir = Gadgets.chomp(getParameter("webserver"),'/')+"hipermail/";
        } else if (sWebBeaconDir.trim().length()==0) {
          sWebBeaconDir = Gadgets.chomp(getParameter("webserver"),'/')+"hipermail/";
        }
        sPBody = sPBody.substring(0, iEndBody)+"<!--WEBBEACON SRC=\""+Gadgets.chomp(sWebBeaconDir,'/')+"web_beacon.jsp?gu_job="+getString(DB.gu_job)+"&pg_atom="+String.valueOf(oAtm.getInt(DB.pg_atom))+"&gu_company="+oAtm.getStringNull(DB.gu_company,"")+"&gu_contact="+oAtm.getStringNull(DB.gu_contact,"")+"&tx_email="+oAtm.getStringNull(DB.tx_email,"")+"\"-->"+sPBody.substring(iEndBody);
      } // fi </body>
    } // fi (bo_webbeacon)

      oSentMsg = oDraft.composeFinalMessage(oHndlr.getSession(), oDraft.getSubject(),
                                            sPBody, getParameter("id"),
                                            sFormat, sEncoding, bAttachInlineImages);

      // If there is no mail address at the atom then send message to recipients
      // that are already set into message object itself.
      // If there is a mail address at the atom then send message to that recipient
      if (!oAtm.isNull(DB.tx_email)) {
        if (DebugFile.trace) DebugFile.writeln("tx_email="+oAtm.getString(DB.tx_email));
        String sSanitizedEmail = MailMessage.sanitizeAddress(oAtm.getString(DB.tx_email));
        if (DebugFile.trace) DebugFile.writeln("sanitized tx_email="+sSanitizedEmail);
        // An AddressException can be thrown here even after sanitizing the e-mail address
        InternetAddress oRec = DBInetAddr.parseAddress(sSanitizedEmail);

    // No blacklisted e-mails allowed to pass through
      if (aBlackList==null) {
        bBlackListed = false;
      } else {
        bBlackListed = (Arrays.binarySearch(aBlackList,oAtm.getString(DB.tx_email).toLowerCase())>=0 ||
                        Arrays.binarySearch(aBlackList,sSanitizedEmail.toLowerCase())>=0);
      }
    if (bBlackListed)
      throw new SQLException("Could not sent message to "+sSanitizedEmail+" because it is blacklisted");
       
        String sRecType = oAtm.getStringNull(DB.tp_recipient,"to");
        if (sRecType.equalsIgnoreCase("to"))
          oSentMsg.setRecipient(Message.RecipientType.TO, oRec);
        else if (sRecType.equalsIgnoreCase("cc"))
          oSentMsg.setRecipient(Message.RecipientType.CC, oRec);
        else
          oSentMsg.setRecipient(Message.RecipientType.BCC, oRec);
      } else {
        if (DebugFile.trace) DebugFile.writeln("tx_email is null");
      }

      // Set From and Reply-To addresses
    if (DebugFile.trace) {
      DebugFile.writeln("from "+aFrom[0].getAddress());
      DebugFile.writeln("reply-to "+aReply[0].getAddress());
    }
      oSentMsg.addFrom(aFrom);
      oSentMsg.setReplyTo(aReply);

    // Request read notification
    String sNotification = getParameter("notification");
      if (Activated.equals(sNotification) || Yes.equals(sNotification)) {
        if (DebugFile.trace) DebugFile.writeln("Disposition-Notification-To "+aFrom[0].getAddress());
      oSentMsg.addHeader("Disposition-Notification-To", aFrom[0].getAddress());
      }
   
      // Send message here
      String sTestMode = getParameter("testmode");
      if (Activated.equals(sTestMode) || Yes.equals(sTestMode)) {
View Full Code Here

Examples of com.sun.mail.smtp.SMTPMessage

            throw new RuntimeException(ex);
        }
    }

    public MimeMessage newMessage() {
        return new SMTPMessage(getSession());
    }
View Full Code Here

Examples of com.sun.mail.smtp.SMTPMessage

                    }
                }

                String content = "You've been invited to join the CloudStack project id=" + projectId + ". Please use token " + token + " to complete registration";

                SMTPMessage msg = new SMTPMessage(_smtpSession);
                msg.setSender(new InternetAddress(_emailSender, _emailSender));
                msg.setFrom(new InternetAddress(_emailSender, _emailSender));
                msg.addRecipient(RecipientType.TO, address);
                msg.setSubject("You are invited to join the cloud stack project id=" + projectId);
                msg.setSentDate(new Date(DateUtil.currentGMTTime().getTime() >> 10));
                msg.setContent(content, "text/plain");
                msg.saveChanges();

                SMTPTransport smtpTrans = null;
                if (_smtpUseAuth) {
                    smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                } else {
                    smtpTrans = new SMTPTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                }
                smtpTrans.connect();
                smtpTrans.sendMessage(msg, msg.getAllRecipients());
                smtpTrans.close();
            } else {
                throw new CloudRuntimeException("Unable to send email invitation; smtp ses");
            }
        }
View Full Code Here

Examples of com.sun.mail.smtp.SMTPMessage

                }
                return;
            }

            if (_smtpSession != null) {
                SMTPMessage msg = new SMTPMessage(_smtpSession);
                msg.setSender(new InternetAddress(_emailSender, _emailSender));
                msg.setFrom(new InternetAddress(_emailSender, _emailSender));
                for (InternetAddress address : _recipientList) {
                    msg.addRecipient(RecipientType.TO, address);
                }
                msg.setSubject(subject);
                msg.setSentDate(new Date());
                msg.setContent(content, "text/plain");
                msg.saveChanges();

                SMTPTransport smtpTrans = null;
                if (_smtpUseAuth) {
                    smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                } else {
                    smtpTrans = new SMTPTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                }
                smtpTrans.connect();
                smtpTrans.sendMessage(msg, msg.getAllRecipients());
                smtpTrans.close();
            }
        }
View Full Code Here

Examples of com.sun.mail.smtp.SMTPMessage

                }
                return;
            }

            if (_smtpSession != null) {
                SMTPMessage msg = new SMTPMessage(_smtpSession);
                msg.setSender(new InternetAddress(_emailSender, _emailSender));
                msg.setFrom(new InternetAddress(_emailSender, _emailSender));
                for (InternetAddress address : _recipientList) {
                    msg.addRecipient(RecipientType.TO, address);
                }
                msg.setSubject(subject);
                msg.setSentDate(new Date());
                msg.setContent(content, "text/plain");
                msg.saveChanges();

                SMTPTransport smtpTrans = null;
                if (_smtpUseAuth) {
                    smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                } else {
View Full Code Here

Examples of com.sun.mail.smtp.SMTPMessage

                    }
                }

                String content = "You've been invited to join the CloudStack project id=" + projectId + ". Please use token " + token + " to complete registration";

                SMTPMessage msg = new SMTPMessage(_smtpSession);
                msg.setSender(new InternetAddress(_emailSender, _emailSender));
                msg.setFrom(new InternetAddress(_emailSender, _emailSender));
                msg.addRecipient(RecipientType.TO, address);
                msg.setSubject("You are invited to join the cloud stack project id=" + projectId);
                msg.setSentDate(new Date(DateUtil.currentGMTTime().getTime() >> 10));
                msg.setContent(content, "text/plain");
                msg.saveChanges();

                SMTPTransport smtpTrans = null;
                if (_smtpUseAuth) {
                    smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                } else {
                    smtpTrans = new SMTPTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                }
                smtpTrans.connect();
                smtpTrans.sendMessage(msg, msg.getAllRecipients());
                smtpTrans.close();
            } else {
                throw new CloudRuntimeException("Unable to send email invitation; smtp ses");
            }
        }
View Full Code Here

Examples of com.sun.mail.smtp.SMTPMessage

                }
                return;
            }

            if (_smtpSession != null) {
                SMTPMessage msg = new SMTPMessage(_smtpSession);
                msg.setSender(new InternetAddress(_emailSender, _emailSender));
                msg.setFrom(new InternetAddress(_emailSender, _emailSender));
                for (InternetAddress address : _recipientList) {
                    msg.addRecipient(RecipientType.TO, address);
                }
                msg.setSubject(subject);
                msg.setSentDate(new Date());
                msg.setContent(content, "text/plain");
                msg.saveChanges();

                SMTPTransport smtpTrans = null;
                if (_smtpUseAuth) {
                    smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                } else {
                    smtpTrans = new SMTPTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                }
                smtpTrans.connect();
                smtpTrans.sendMessage(msg, msg.getAllRecipients());
                smtpTrans.close();
            }
        }
View Full Code Here

Examples of com.sun.mail.smtp.SMTPMessage

                }
                return;
            }

            if (_smtpSession != null) {
                SMTPMessage msg = new SMTPMessage(_smtpSession);
                msg.setSender(new InternetAddress(_emailSender, _emailSender));
                msg.setFrom(new InternetAddress(_emailSender, _emailSender));
                for (InternetAddress address : _recipientList) {
                    msg.addRecipient(RecipientType.TO, address);
                }
                msg.setSubject(subject);
                msg.setSentDate(new Date());
                msg.setContent(content, "text/plain");
                msg.saveChanges();

                SMTPTransport smtpTrans = null;
                if (_smtpUseAuth) {
                    smtpTrans = new SMTPSSLTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                } else {
                    smtpTrans = new SMTPTransport(_smtpSession, new URLName("smtp", _smtpHost, _smtpPort, null, _smtpUsername, _smtpPassword));
                }
                smtpTrans.connect();
                smtpTrans.sendMessage(msg, msg.getAllRecipients());
                smtpTrans.close();
            }
        }
View Full Code Here

Examples of com.sun.mail.smtp.SMTPMessage

public class Email {

    private final static Logger log = LoggerFactory.getLogger(Email.class);

    public static Email fromStream(InputStream in) throws MessagingException, IOException {
        SMTPMessage mm = new SMTPMessage(createSession(), in);
        return fromMessage(mm);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.