Class MailClient

  • All Implemented Interfaces:
    IMailClient

    public final class MailClient
    extends javax.mail.Authenticator
    implements IMailClient
    A small mail client that simplifies the sending of emails using of JavaMail API.

    Just instantiate this class and use sendMessage(String, String, String, From, String[]) to send the email via SMTP.

    If the SMTP host starts with file:// the mail is not send to a real SMTP server but it is stored in a file in the directory specified by the relative path following this prefix.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String MAIL_SMTP_PASSWORD
      This system property is not supported by the JavaMail API
      static java.lang.String MAIL_TEST_ADDRESS
      This system property is not supported by the JavaMail API
    • Constructor Summary

      Constructors 
      Constructor Description
      MailClient​(MailClientParameters parameters)  
      MailClient​(java.lang.String from, java.lang.String smtpHost)  
      MailClient​(java.lang.String from, java.lang.String smtpHost, java.lang.String smtpPort, java.lang.String smtpUsername, java.lang.String smtpPassword, java.lang.String testAddress)  
      MailClient​(java.util.Properties properties)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected javax.mail.PasswordAuthentication getPasswordAuthentication()  
      void sendEmailMessage​(java.lang.String subject, java.lang.String content, EMailAddress replyToOrNull, EMailAddress fromOrNull, EMailAddress... recipients)
      Sends a mail with given subject and content to given recipients.
      void sendEmailMessageWithAttachment​(java.lang.String subject, java.lang.String content, java.lang.String filename, javax.activation.DataHandler attachmentContent, EMailAddress replyToOrNull, EMailAddress fromOrNull, EMailAddress... recipients)
      Sends a mail with given subject and content to given recipients, includig the given attachment
      void sendMessage​(java.lang.String subject, java.lang.String content, java.lang.String replyToOrNull, From fromOrNull, java.lang.String... recipients)
      Sends a mail with given subject and content to given recipients.
      void sendMessageWithAttachment​(java.lang.String subject, java.lang.String content, java.lang.String filename, javax.activation.DataHandler attachmentContent, java.lang.String replyTo, From fromOrNull, java.lang.String... recipients)
      Sends a mail with given subject and content to given recipients, includig the given attachment
      void sendTestEmail()
      Sends a test email if a test address was configured for the mail client.
      • Methods inherited from class javax.mail.Authenticator

        getDefaultUserName, getRequestingPort, getRequestingPrompt, getRequestingProtocol, getRequestingSite
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MAIL_SMTP_PASSWORD

        public static final java.lang.String MAIL_SMTP_PASSWORD
        This system property is not supported by the JavaMail API
        See Also:
        Constant Field Values
      • MAIL_TEST_ADDRESS

        public static final java.lang.String MAIL_TEST_ADDRESS
        This system property is not supported by the JavaMail API
        See Also:
        Constant Field Values
    • Constructor Detail

      • MailClient

        public MailClient​(java.lang.String from,
                          java.lang.String smtpHost)
      • MailClient

        public MailClient​(java.lang.String from,
                          java.lang.String smtpHost,
                          java.lang.String smtpPort,
                          java.lang.String smtpUsername,
                          java.lang.String smtpPassword,
                          java.lang.String testAddress)
      • MailClient

        public MailClient​(java.util.Properties properties)
    • Method Detail

      • sendTestEmail

        public void sendTestEmail()
        Description copied from interface: IMailClient
        Sends a test email if a test address was configured for the mail client.
        Specified by:
        sendTestEmail in interface IMailClient
      • sendMessage

        public final void sendMessage​(java.lang.String subject,
                                      java.lang.String content,
                                      java.lang.String replyToOrNull,
                                      From fromOrNull,
                                      java.lang.String... recipients)
                               throws ch.systemsx.cisd.common.exceptions.EnvironmentFailureException
        Sends a mail with given subject and content to given recipients.
        Specified by:
        sendMessage in interface IMailClient
        Parameters:
        recipients - list of recipients (of type Message.RecipientType.TO)
        replyToOrNull - reply-to part of the email header. Can be null.
        fromOrNull - from part of the email header. Can be null. If specified - will overwrite the 'from' value specified for the client.
        Throws:
        ch.systemsx.cisd.common.exceptions.EnvironmentFailureException
      • sendEmailMessage

        public void sendEmailMessage​(java.lang.String subject,
                                     java.lang.String content,
                                     EMailAddress replyToOrNull,
                                     EMailAddress fromOrNull,
                                     EMailAddress... recipients)
                              throws ch.systemsx.cisd.common.exceptions.EnvironmentFailureException
        Description copied from interface: IMailClient
        Sends a mail with given subject and content to given recipients.
        Specified by:
        sendEmailMessage in interface IMailClient
        replyToOrNull - Reply-to email header. Can be null.
        fromOrNull - from part of the email header. Can be null. If specified - will overwrite the 'from' value specified for the client.
        recipients - list of recipients (of type Message.RecipientType.TO)
        Throws:
        ch.systemsx.cisd.common.exceptions.EnvironmentFailureException
      • sendMessageWithAttachment

        public final void sendMessageWithAttachment​(java.lang.String subject,
                                                    java.lang.String content,
                                                    java.lang.String filename,
                                                    javax.activation.DataHandler attachmentContent,
                                                    java.lang.String replyTo,
                                                    From fromOrNull,
                                                    java.lang.String... recipients)
                                             throws ch.systemsx.cisd.common.exceptions.EnvironmentFailureException
        Sends a mail with given subject and content to given recipients, includig the given attachment
        Specified by:
        sendMessageWithAttachment in interface IMailClient
        Parameters:
        recipients - list of recipients (of type Message.RecipientType.TO)
        replyTo - reply-to part of the email header. Can be null.
        fromOrNull - from part of the email header. Can be null. If specified - will overwrite the 'from' value specified for the client.
        Throws:
        ch.systemsx.cisd.common.exceptions.EnvironmentFailureException
      • sendEmailMessageWithAttachment

        public void sendEmailMessageWithAttachment​(java.lang.String subject,
                                                   java.lang.String content,
                                                   java.lang.String filename,
                                                   javax.activation.DataHandler attachmentContent,
                                                   EMailAddress replyToOrNull,
                                                   EMailAddress fromOrNull,
                                                   EMailAddress... recipients)
                                            throws ch.systemsx.cisd.common.exceptions.EnvironmentFailureException
        Description copied from interface: IMailClient
        Sends a mail with given subject and content to given recipients, includig the given attachment
        Specified by:
        sendEmailMessageWithAttachment in interface IMailClient
        replyToOrNull - Reply-to email header. Can be null.
        fromOrNull - from part of the email header. Can be null. If specified - will overwrite the 'from' value specified for the client.
        recipients - list of recipients (of type Message.RecipientType.TO)
        Throws:
        ch.systemsx.cisd.common.exceptions.EnvironmentFailureException
      • getPasswordAuthentication

        protected final javax.mail.PasswordAuthentication getPasswordAuthentication()
        Overrides:
        getPasswordAuthentication in class javax.mail.Authenticator