Class Message

java.lang.Object
com.codename1.messaging.Message

public class Message extends Object

Represents a message to be sent using underlying platform e-mail client or the cloud.

The code below demonstrates sending a simple message with attachments using the devices native email client:

Message m = new Message("Body of message");
m.getAttachments().put(textAttachmentUri, "text/plain");
m.getAttachments().put(imageAttachmentUri, "image/png");
Display.getInstance().sendMessage(new String[] {"someone@gmail.com"}, "Subject of message", m);

The following code demonstrates sending an email via the Codename One cloud, notice that this is a pro specific feature:

Message m = new Message("Check out Codename One");
m.setMimeType(Message.MIME_HTML);

// notice that we provide a plain text alternative as well in the send method
boolean success = m.sendMessageViaCloudSync("Codename One", "destination@domain.com", "Name Of User", "Message Subject",
                            "Check out Codename One at https://www.codenameone.com/");
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    static final String
     
    static final String
     
    static final String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Message(String content)
    Constructor with the message body content
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets the message attachment file path
    Gets the attachment mime type
    Returns the attachment map which can be used to add multiple attachments.
    Gets the message content
    Gets the message mime type
    boolean
    Indicates whether the cloud message should produce an error dialog if sending failed
    static void
    sendMessage(String[] recipients, String subject, Message msg)
    Send an email using the platform mail client
    void
    sendMessageViaCloud(String sender, String recipient, String recipientName, String subject, String plainTextBody)
    Send an email message using the Codename One cloud to send the message, notice that this API will only work for pro accounts.
    boolean
    sendMessageViaCloudSync(String sender, String recipient, String recipientName, String subject, String plainTextBody)
    Send an email message using the Codename One cloud to send the message, notice that this API will only work for pro accounts.
    void
    Sets the message attachment if exists
    void
    Sets the attachment mime type.
    void
    setCloudMessageFailSilently(boolean cloudMessageFailSilently)
    Indicates whether the cloud message should produce an error dialog if sending failed
    void
    setMimeType(String mimeType)
    Sets the message mime type.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
     
    boolean
    Indicates whether some other object is "equal to" this one.
    final Class
    Returns the runtime class of an object.
    int
    Returns a hash code value for the object.
    final void
    Wakes up a single thread that is waiting on this object's monitor.
    final void
    Wakes up all threads that are waiting on this object's monitor.
    Returns a string representation of the object.
    final void
    Causes current thread to wait until another thread invokes the method or the method for this object.
    final void
    wait(long timeout)
    Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
    final void
    wait(long timeout, int nanos)
    Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
  • Field Details

  • Constructor Details

    • Message

      public Message(String content)

      Constructor with the message body content

      Parameters
      • content: the message content
  • Method Details

    • sendMessage

      public static void sendMessage(String[] recipients, String subject, Message msg)

      Send an email using the platform mail client

      Parameters
      • recipients: array of e-mail addresses

      • subject: e-mail subject

      • msg: the Message to send

    • getContent

      public String getContent()

      Gets the message content

      Returns

      content

    • getAttachments

      public Map<String,String> getAttachments()

      Returns the attachment map which can be used to add multiple attachments. The path needs to point at a full absolute file URI within com.codename1.io.FileSystemStorage, it will not work with com.codename1.io.Storage files!

      Returns

      a map of full file paths to mime type that can be used to add attachments

    • getMimeType

      public String getMimeType()

      Gets the message mime type

      Returns

      the mime type

    • setMimeType

      public void setMimeType(String mimeType)

      Sets the message mime type.

      Parameters
      • mimeType
    • getAttachmentMimeType

      public String getAttachmentMimeType()
      Gets the attachment mime type
    • setAttachmentMimeType

      public void setAttachmentMimeType(String mimeType)

      Sets the attachment mime type.

      Parameters
      • mimeType
    • getAttachment

      public String getAttachment()

      Gets the message attachment file path

      Returns

      the file path of the attachment

    • setAttachment

      public void setAttachment(String fileUri)

      Sets the message attachment if exists

      Parameters
      • fileUri: the file to attach to the message
    • sendMessageViaCloud

      public void sendMessageViaCloud(String sender, String recipient, String recipientName, String subject, String plainTextBody)

      Send an email message using the Codename One cloud to send the message, notice that this API will only work for pro accounts.

      Message m = new Message("Check out Codename One");
      m.setMimeType(Message.MIME_HTML);
      
      // notice that we provide a plain text alternative as well in the send method
      boolean success = m.sendMessageViaCloudSync("Codename One", "destination@domain.com", "Name Of User", "Message Subject",
                                  "Check out Codename One at https://www.codenameone.com/");
      
      Parameters
      • sender: the name of the sender, notice all email will arrive from Codename One to avoid spam issues

      • recipient: the email for the recipient

      • recipientName: the display name for the recipient

      • subject: e-mail subject

      • plainTextBody: @param plainTextBody when sending an HTML message you should also attach a plain text fallback message, this is redundant if the email is a plain text message to begin with

      Deprecated

      this functionality is retired and no longer works. You can use the sendgrid cn1lib or similar libraries

    • sendMessageViaCloudSync

      public boolean sendMessageViaCloudSync(String sender, String recipient, String recipientName, String subject, String plainTextBody)

      Send an email message using the Codename One cloud to send the message, notice that this API will only work for pro accounts.

      Message m = new Message("Check out Codename One");
      m.setMimeType(Message.MIME_HTML);
      
      // notice that we provide a plain text alternative as well in the send method
      boolean success = m.sendMessageViaCloudSync("Codename One", "destination@domain.com", "Name Of User", "Message Subject",
                                  "Check out Codename One at https://www.codenameone.com/");
      
      Parameters
      • sender: the name of the sender, notice all email will arrive from Codename One to avoid spam issues

      • recipient: the email for the recipient

      • recipientName: the display name for the recipient

      • subject: e-mail subject

      • plainTextBody: @param plainTextBody when sending an HTML message you should also attach a plain text fallback message, this is redundant if the email is a plain text message to begin with

      Returns

      true if sending succeeded

      Deprecated

      this functionality is retired and no longer works. You can use the sendgrid cn1lib or similar libraries

    • isCloudMessageFailSilently

      public boolean isCloudMessageFailSilently()

      Indicates whether the cloud message should produce an error dialog if sending failed

      Returns

      the cloudMessageFailSilently

    • setCloudMessageFailSilently

      public void setCloudMessageFailSilently(boolean cloudMessageFailSilently)

      Indicates whether the cloud message should produce an error dialog if sending failed

      Parameters
      • cloudMessageFailSilently: the cloudMessageFailSilently to set