Class EMail

  • All Implemented Interfaces:
    Serializable

    public final class EMail
    extends Object
    implements Serializable
    EMail Object. Resources: http://java.sun.com/products/javamail/index.html http://java.sun.com/products/javamail/FAQ.html

    When I try to send a message, I get javax.mail.SendFailedException: 550 Unable to relay for my-address
    This is an error reply from your SMTP mail server. It indicates that your mail server is not configured to allow you to send mail through it.

    Version:
    $Id: EMail.java,v 1.4 2006/07/30 00:54:35 jjanke Exp $
    Author:
    Jorg Janke, Michael Judd BF [ 2736995 ] - toURL() in java.io.File has been depreciated
    See Also:
    Serialized Form
    • Constructor Detail

      • EMail

        public EMail​(MClient client,
                     String from,
                     String to,
                     String subject,
                     String message)
        Full Constructor
        Parameters:
        client - the client
        from - Sender's EMail address
        to - Recipient EMail address
        subject - Subject of message
        message - The message
      • EMail

        public EMail​(MClient client,
                     String from,
                     String to,
                     String subject,
                     String message,
                     boolean html)
        Full Constructor
        Parameters:
        client - the client
        from - Sender's EMail address
        to - Recipient EMail address
        subject - Subject of message
        message - The message
        html -
      • EMail

        public EMail​(Properties ctx,
                     String smtpHost,
                     String from,
                     String to,
                     String subject,
                     String message)
        Full Constructor
        Parameters:
        ctx - context
        smtpHost - The mail server
        from - Sender's EMail address
        to - Recipient EMail address
        subject - Subject of message
        message - The message
      • EMail

        public EMail​(Properties ctx,
                     String smtpHost,
                     String from,
                     String to,
                     String subject,
                     String message,
                     boolean html)
        Full Constructor
        Parameters:
        ctx - context
        smtpHost - The mail server
        from - Sender's EMail address
        to - Recipient EMail address
        subject - Subject of message
        message - The message
        html - html email
      • EMail

        public EMail​(Properties ctx,
                     String smtpHost,
                     int smtpPort,
                     boolean isSecureSmtp,
                     String from,
                     String to,
                     String subject,
                     String message,
                     boolean html)
        Full Constructor
        Parameters:
        ctx - context
        smtpHost - The mail server
        smtpPort -
        isSecureSmtp -
        from - Sender's EMail address
        to - Recipient EMail address
        subject - Subject of message
        message - The message
        html - html email
    • Method Detail

      • setAcknoledgmentReceipt

        public void setAcknoledgmentReceipt​(boolean ar)
      • send

        public String send()
        Send Mail direct
        Returns:
        OK or error message
      • getSentMsg

        public String getSentMsg()
        Get Send Result Msg
        Returns:
        msg
      • isSentOK

        public boolean isSentOK()
        Was sending the Msg OK
        Returns:
        msg == OK
      • getMimeMessage

        protected javax.mail.internet.MimeMessage getMimeMessage()
        Get the message directly
        Returns:
        mail message
      • getMessageID

        public String getMessageID()
        Get Message ID or null
        Returns:
        Message ID e.g. <20030130004739.15377.qmail@web13506.mail.yahoo.com> <25699763.1043887247538.JavaMail.jjanke@main>
      • createAuthenticator

        public EMailAuthenticator createAuthenticator​(String username,
                                                      String password)
        Create Authenticator for User
        Parameters:
        username - user name
        password - user password
        Returns:
        Authenticator or null
      • getFrom

        public javax.mail.internet.InternetAddress getFrom()
        Get Sender
        Returns:
        Sender's internet address
      • setFrom

        public void setFrom​(String newFrom)
        Set Sender
        Parameters:
        newFrom - Sender's email address
      • addTo

        public boolean addTo​(String newTo)
        Add To Recipient
        Parameters:
        newTo - Recipient's email address
        Returns:
        true if valid
      • getTo

        public javax.mail.internet.InternetAddress getTo()
        Get Recipient
        Returns:
        Recipient's internet address
      • getTos

        public javax.mail.internet.InternetAddress[] getTos()
        Get TO Recipients
        Returns:
        Recipient's internet address
      • addCc

        public boolean addCc​(String newCc)
        Add CC Recipient
        Parameters:
        newCc - EMail cc Recipient
        Returns:
        true if valid
      • getCcs

        public javax.mail.internet.InternetAddress[] getCcs()
        Get CC Recipients
        Returns:
        Recipient's internet address
      • addBcc

        public boolean addBcc​(String newBcc)
        Add BCC Recipient
        Parameters:
        newBcc - EMail cc Recipient
        Returns:
        true if valid
      • getBccs

        public javax.mail.internet.InternetAddress[] getBccs()
        Get BCC Recipients
        Returns:
        Recipient's internet address
      • setReplyTo

        public boolean setReplyTo​(String newTo)
        Set Reply to Address
        Parameters:
        newTo - email address
        Returns:
        true if valid
      • getReplyTo

        public javax.mail.internet.InternetAddress getReplyTo()
        Get Reply To
        Returns:
        Reply To internet address
      • setSubject

        public void setSubject​(String newSubject)
        Set Subject
        Parameters:
        newSubject - Subject
      • getSubject

        public String getSubject()
        Get Subject
        Returns:
        subject
      • setMessageText

        public void setMessageText​(String newMessage)
        Set Message
        Parameters:
        newMessage - message
      • getMessageCRLF

        public String getMessageCRLF()
        Get MIME String Message - line ending with CRLF.
        Returns:
        message
      • setMessageHTML

        public void setMessageHTML​(String html)
        Set HTML Message
        Parameters:
        html - message
      • setMessageHTML

        public void setMessageHTML​(String subject,
                                   String message)
        Set HTML Message
        Parameters:
        subject - subject repeated in message as H2
        message - message
      • getMessageHTML

        public String getMessageHTML()
        Get HTML Message
        Returns:
        message
      • addAttachment

        public void addAttachment​(File file)
        Add file Attachment
        Parameters:
        file - file to attach
      • addAttachments

        public void addAttachments​(Collection<File> files)
        Add a collection of attachments
        Parameters:
        files - collection of files
      • addAttachment

        public void addAttachment​(URI url)
        Add url based file Attachment
        Parameters:
        url - url content to attach
      • addAttachment

        public void addAttachment​(byte[] data,
                                  String type,
                                  String name)
        Add attachment. (converted to ByteArrayDataSource)
        Parameters:
        data - data
        type - MIME type
        name - name of attachment
      • addAttachment

        public void addAttachment​(javax.activation.DataSource dataSource)
        Add arbitrary Attachment
        Parameters:
        dataSource - content to attach
      • setSmtpHost

        public void setSmtpHost​(String newSmtpHost)
        Set SMTP Host or address
        Parameters:
        newSmtpHost - Mail server
      • getSmtpHost

        public String getSmtpHost()
        Get Mail Server name or address
        Returns:
        mail server
      • isValid

        public boolean isValid()
        Is Info valid to send EMail
        Returns:
        true if email is valid and can be sent
      • isValid

        public boolean isValid​(boolean recheck)
        Re-Check Info if valid to send EMail
        Parameters:
        recheck - if true check main variables
        Returns:
        true if email is valid and can be sent
      • getAttachments

        public Object[] getAttachments()
        Returns:
        attachments array or empty array. This method will never return null.
      • toString

        public String toString()
        String Representation
        Overrides:
        toString in class Object
        Returns:
        info
      • validate

        public static boolean validate​(String email)
        Validate format of an email address IDEMPIERE-1409
        Returns:
        true if email has proper format
      • main

        public static void main​(String[] args)
        Test. java -cp CTools.jar;CClient.jar org.compiere.util.EMail main info@adempiere.org jjanke@adempiere.org "My Subject" "My Message" -- If you get SendFailedException: 550 5.7.1 Unable to relay for .. Check: - Does the SMTP server allow you to relay (Exchange: SMTP server - Access) - Did you authenticate (setEmailUser)
        Parameters:
        args - Array of arguments
      • setHeader

        public void setHeader​(String name,
                              String value)
      • createInternetAddress

        public static javax.mail.internet.InternetAddress createInternetAddress​(String email)
                                                                         throws Exception
        Create an internet address with personal if the email address is formatted as "Personal "
        Parameters:
        email -
        Returns:
        internet address with personal if defined
        Throws:
        Exception
      • setForTenantSmtp

        public void setForTenantSmtp​(boolean forceTenantSmtp)