Java Code Examples for javax.mail.MessagingException

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project ajah, under directory /ajah-rfcmail/src/main/java/com/ajah/rfcmail/util/.

Source file: MessageUtils.java

  21 
vote

/** 
 * Extracts as much text as possible from a message.
 * @param msg The message to extract text from.
 * @return The text that was extracted from a message.
 * @throws MessagingException If the message could not be understood.
 * @throws IOException If the body of the message could not be read.
 */
public static String getContentAsText(final AjahMimeMessage msg) throws MessagingException, IOException {
  if (msg.getAjahMimeType() == AjahMimeType.TEXT_PLAIN) {
    return (String)msg.getContent();
  }
 else   if (msg.getAjahMimeType() == AjahMimeType.TEXT_HTML && msg.getContent() instanceof String) {
    try {
      return getHTMLAsText((String)msg.getContent());
    }
 catch (    final IllegalArgumentException e) {
      throw new MessagingException((String)msg.getContent(),e);
    }
  }
  throw new MessagingException(msg.getAjahMimeType() + " has a " + msg.getContent().getClass().getName());
}
 

Example 2

From project b3log-latke, under directory /latke/src/main/java/org/b3log/latke/mail/local/.

Source file: MailSender.java

  21 
vote

/** 
 * Transport recipients to InternetAddress array.
 * @param recipients the set of all recipients
 * @return  InternetAddress array of all recipients internetAddress
 * @throws MessagingException messagingException from javax.mail
 */
private InternetAddress[] transformRecipients(final Set<String> recipients) throws MessagingException {
  if (recipients.isEmpty()) {
    throw new MessagingException("recipients of mail should not be empty");
  }
  final InternetAddress[] ret=new InternetAddress[recipients.size()];
  int i=0;
  for (  String recipient : recipients) {
    ret[i]=new InternetAddress(recipient);
    i++;
  }
  return ret;
}
 

Example 3

From project echo2, under directory /src/exampleapp/email/src/java/echo2example/email/faux/.

Source file: FauxFolder.java

  21 
vote

/** 
 * @see javax.mail.Folder#getFolder(java.lang.String)
 */
public Folder getFolder(String name) throws MessagingException {
  if (type == HOLDS_FOLDERS) {
    return store.inboxFolder;
  }
 else {
    throw new MessagingException();
  }
}
 

Example 4

From project geronimo-javamail, under directory /geronimo-javamail_1.3.1/geronimo-javamail_1.3.1_provider/src/main/java/org/apache/geronimo/javamail/authentication/.

Source file: CramMD5Authenticator.java

  21 
vote

/** 
 * Evaluate a CRAM-MD5 login challenge, returning the a result string that should satisfy the clallenge.
 * @param challenge The decoded challenge data, as a byte array.
 * @return A formatted challege response, as an array of bytes.
 * @exception MessagingException
 */
public byte[] evaluateChallenge(byte[] challenge) throws MessagingException {
  byte[] passBytes;
  try {
    passBytes=password.getBytes("UTF-8");
    byte[] digest=computeCramDigest(passBytes,challenge);
    String responseString=username + " " + new String(Hex.encode(digest));
    complete=true;
    return responseString.getBytes();
  }
 catch (  UnsupportedEncodingException e) {
    throw new MessagingException("Invalid character encodings");
  }
}
 

Example 5

From project dawn-isencia, under directory /com.isencia.passerelle.engine/src/main/java/com/isencia/passerelle/message/.

Source file: MessageHelper.java

  20 
vote

/** 
 * Get the content dispostion of a part. The part is interogated for a valid content disposition. If the content disposition is missing, a default disposition is created based on the type of the part.
 * @param part The part to interogate
 * @return ContentDisposition of the part
 * @throws MessagingException DOCUMENT ME!
 * @see javax.mail.Part
 */
static ContentDisposition getContentDisposition(Part part) throws MessagingException {
  String header=part.getDisposition();
  try {
    if (header != null) {
      return new ContentDisposition(header);
    }
  }
 catch (  ParseException e) {
    throw new MessagingException(e.toString());
  }
  if (part instanceof MimeBodyPart) {
    Multipart parentMultipart=((MimeBodyPart)part).getParent();
    if (parentMultipart != null) {
      Part parentPart=parentMultipart.getParent();
      logger.debug("trying get Disposition on Parent");
      if (parentPart != null) {
        String disposition=parentPart.getDisposition();
        logger.debug("Disposition on Parent : " + disposition);
        if (disposition != null) {
          return new ContentDisposition(disposition);
        }
      }
    }
    return new ContentDisposition(Part.ATTACHMENT);
  }
  return new ContentDisposition(Part.INLINE);
}
 

Example 6

From project agileBase, under directory /gtpb_server/src/com/gtwm/pb/auth/.

Source file: AppUser.java

  19 
vote

public void sendPasswordReset(String appUrl) throws CantDoThatException, CodingErrorException, MessagingException {
  if (this.getEmail() == null) {
    throw new CantDoThatException("The user has no email address");
  }
  if (!this.getEmail().contains("@")) {
    throw new CantDoThatException("The user's email isn't valid");
  }
  try {
    String password=RandomString.generate();
    this.hashAndSetPassword(password);
    String passwordResetLink=appUrl + "?return=gui/set_password/email_reset&u=" + this.getUserName()+ "&x="+ password;
    if (this.getAllowPasswordReset()) {
      throw new CantDoThatException("The previous password reset request hasn't timed out yet, please use that: " + passwordResetLink);
    }
    Set<String> recipients=new HashSet<String>();
    recipients.add(this.getEmail());
    String subject="Set your password";
    String body="Please choose a password for your account by following this link:\n\n";
    body+=passwordResetLink + "\n";
    Helpers.sendEmail(recipients,body,subject);
  }
 catch (  MissingParametersException mpex) {
    throw new CodingErrorException("Error generating a password: " + mpex);
  }
  this.passwordResetSent=System.currentTimeMillis();
}
 

Example 7

From project ALP, under directory /workspace/alp-reporter/src/main/java/com/lohika/alp/mailer/.

Source file: Mailer.java

  19 
vote

/** 
 * Attach file.
 * @param file the file
 * @param newFileName the new file name
 * @param multipart the multipart
 * @throws MessagingException the messaging exception
 */
private void attachFile(File file,String newFileName,Multipart multipart) throws MessagingException {
  MimeBodyPart messageBodyPart=new MimeBodyPart();
  DataSource fds=new FileDataSource(file);
  messageBodyPart.setDataHandler(new DataHandler(fds));
  messageBodyPart.setFileName(newFileName);
  multipart.addBodyPart(messageBodyPart);
}
 

Example 8

From project azkaban, under directory /azkaban/src/java/azkaban/app/.

Source file: Mailman.java

  19 
vote

public void sendEmail(String fromAddress,List<String> toAddress,String subject,String body) throws MessagingException {
  Properties props=new Properties();
  props.setProperty("mail.transport.protocol","smtp");
  props.put("mail.host",_mailHost);
  props.put("mail.user",_mailUser);
  props.put("mail.password",_mailPassword);
  Session session=Session.getDefaultInstance(props);
  Message message=new MimeMessage(session);
  InternetAddress from=new InternetAddress(fromAddress == null ? "[email protected]" : fromAddress,false);
  message.setFrom(from);
  for (  String toAddr : toAddress)   message.addRecipient(Message.RecipientType.TO,new InternetAddress(toAddr,false));
  message.setSubject(subject);
  message.setText(body);
  message.setSentDate(new Date());
  Transport.send(message);
}
 

Example 9

From project BHT-FPA, under directory /mailer-common/de.bht.fpa.mail.common/src/de/bht/fpa/mail/s000000/common/mail/imapsync/.

Source file: ImapHelper.java

  19 
vote

private static int caculateTotalNumberOfFolders(IMAPFolder imapFolder) throws MessagingException {
  javax.mail.Folder[] subFolders=imapFolder.list();
  if (subFolders == null || subFolders.length == 0) {
    return 0;
  }
  int temp=subFolders.length;
  for (  javax.mail.Folder f : subFolders) {
    temp+=caculateTotalNumberOfFolders((IMAPFolder)f);
  }
  return temp;
}
 

Example 10

From project capedwarf-blue, under directory /mail/src/main/java/org/jboss/capedwarf/mail/.

Source file: JBossMailService.java

  19 
vote

public void send(Message message) throws IOException {
  try {
    send(convertToJavaMail(message));
  }
 catch (  MessagingException e) {
    throw new IOException("Could not send message",e);
  }
}
 

Example 11

From project Carolina-Digital-Repository, under directory /persistence/src/test/java/edu/unc/lib/dl/util/.

Source file: MockJavaMailSender.java

  19 
vote

private void logit(MimeMessage msg){
  log.debug("Dumping message content to standard out.");
  try {
    msg.writeTo(System.out);
  }
 catch (  IOException e) {
    e.printStackTrace();
  }
catch (  MessagingException e) {
    e.printStackTrace();
  }
}
 

Example 12

From project codjo-webservices, under directory /codjo-webservices-common/src/main/java/com/tilab/wsig/servlet/.

Source file: WSIGServlet.java

  19 
vote

/** 
 * extract SOAP Message from http request
 * @param request
 * @return
 * @throws Exception
 */
private Message extractSOAPMessage(HttpServletRequest request) throws Exception {
  String contentLocation=request.getHeader(HTTPConstants.HEADER_CONTENT_LOCATION);
  log.debug("contentLocation: " + contentLocation);
  String contentType=request.getHeader(HTTPConstants.HEADER_CONTENT_TYPE);
  log.debug("contentType: " + contentType);
  Message soapRequest=null;
  try {
    soapRequest=new Message(request.getInputStream(),false,contentType,contentLocation);
  }
 catch (  IOException e) {
    throw new Exception("Error extracting soap message",e);
  }
  MimeHeaders requestMimeHeaders=soapRequest.getMimeHeaders();
  SOAPContext soapContext=new SOAPContext();
  for (Enumeration e=request.getHeaderNames(); e.hasMoreElements(); ) {
    String headerName=(String)e.nextElement();
    for (Enumeration f=request.getHeaders(headerName); f.hasMoreElements(); ) {
      String headerValue=(String)f.nextElement();
      requestMimeHeaders.addHeader(headerName,headerValue);
      MimeBodyPart p=new MimeBodyPart();
      try {
        p.addHeader(headerName,headerValue);
        log.debug("headerName: " + headerName + ", headerValue: "+ headerValue);
        soapContext.addBodyPart(p);
      }
 catch (      MessagingException ex) {
        throw new Exception("Error building soap context",ex);
      }
    }
  }
  return soapRequest;
}
 

Example 13

From project Cours-3eme-ann-e, under directory /Java/FerryInpres/src/company_server/.

Source file: MobileCompanyServer.java

  19 
vote

private void envoiPoliceEmail(String immatriculation,String emplacement,String modele){
  try {
    Properties property=System.getProperties();
    property.put("mail.smtp.host",MobileCompanyServer.prop.getProperty("MAIL_SMTP"));
    property.put("file.encoding","iso-8859-1");
    Session smtpSession=Session.getDefaultInstance(property,null);
    MimeMessage msg=new MimeMessage(smtpSession);
    msg.setFrom(new InternetAddress(MobileCompanyServer.prop.getProperty("EMAIL_SERVICE")));
    msg.setRecipient(Message.RecipientType.TO,new InternetAddress(MobileCompanyServer.prop.getProperty("EMAIL_POLICE")));
    msg.setSubject("Nouvelle constatation d'infraction");
    msg.setText("Nous avons constat?s un v?hicule qui a tent? une intrusion.\n" + "Le v?hicule est immatricul? " + immatriculation + " (mod?le : "+ modele+ ") et se trouve ? "+ emplacement);
    Transport.send(msg);
  }
 catch (  MessagingException ex) {
    Logger.getLogger(MobileServerThread.class.getName()).log(Level.SEVERE,null,ex);
  }
}
 

Example 14

From project coverity-plugin, under directory /src/main/java/jenkins/plugins/coverity/.

Source file: CoverityMailSender.java

  19 
vote

public boolean execute(CoverityBuildAction action,BuildListener listener) throws InterruptedException {
  try {
    MimeMessage mail=getMail(action,listener);
    if (mail != null) {
      Address[] allRecipients=mail.getAllRecipients();
      if (allRecipients != null) {
        StringBuilder buf=new StringBuilder("Sending e-mails to:");
        for (        Address a : allRecipients)         buf.append(' ').append(a);
        listener.getLogger().println(buf);
        Transport.send(mail);
      }
    }
  }
 catch (  MessagingException e) {
    e.printStackTrace(listener.error(e.getMessage()));
  }
catch (  CovRemoteServiceException_Exception e) {
    e.printStackTrace(listener.error(e.getMessage()));
  }
catch (  IOException e) {
    e.printStackTrace(listener.error(e.getMessage()));
  }
  return true;
}
 

Example 15

From project des, under directory /daemon/lib/apache-log4j-1.2.16/src/main/java/org/apache/log4j/net/.

Source file: SMTPAppender.java

  19 
vote

/** 
 * Activate the specified options, such as the smtp host, the recipient, from, etc. 
 */
public void activateOptions(){
  Session session=createSession();
  msg=new MimeMessage(session);
  try {
    addressMessage(msg);
    if (subject != null) {
      try {
        msg.setSubject(MimeUtility.encodeText(subject,"UTF-8",null));
      }
 catch (      UnsupportedEncodingException ex) {
        LogLog.error("Unable to encode SMTP subject",ex);
      }
    }
  }
 catch (  MessagingException e) {
    LogLog.error("Could not activate SMTPAppender options.",e);
  }
  if (evaluator instanceof OptionHandler) {
    ((OptionHandler)evaluator).activateOptions();
  }
}
 

Example 16

From project email-preview, under directory /src/main/java/org/jasig/portlet/emailpreview/dao/javamail/.

Source file: JavamailAccountDaoImpl.java

  19 
vote

@Override public Folder getUserInbox(Session session,String folderName) throws MessagingException {
  if (session == null) {
    String msg="Argument 'session' cannot be null";
    throw new IllegalArgumentException(msg);
  }
  try {
    Store store=session.getStore();
    store.connect();
    if (log.isDebugEnabled()) {
      log.debug("Mail store connection established");
    }
    Folder root=store.getDefaultFolder();
    Folder inboxFolder=root.getFolder(folderName);
    return inboxFolder;
  }
 catch (  AuthenticationFailedException e) {
    throw new MailAuthenticationException(e);
  }
}