public class Message

  1. Object
  2. Message

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/");

Fields

public static final String MIME_TEXT = "text/plain"
public static final String MIME_HTML = "text/html"
public static final String MIME_IMAGE_JPG = "image/jpeg"
public static final String MIME_IMAGE_PNG = "image/png"

Constructors

public Message(String content)Constructor with the message body content

Methods

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

Inherited methods

Field details

MIME_TEXT

public static final String MIME_TEXT = "text/plain"

MIME_HTML

public static final String MIME_HTML = "text/html"

MIME_IMAGE_JPG

public static final String MIME_IMAGE_JPG = "image/jpeg"

MIME_IMAGE_PNG

public static final String MIME_IMAGE_PNG = "image/png"

Constructor details

Message

public Message(String content)
Constructor with the message body content

Parameters

content String
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 String[]
array of e-mail addresses
subject String
e-mail subject
msg Message
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.

getAttachmentMimeType

public String getAttachmentMimeType()
Gets the attachment mime type

setAttachmentMimeType

public void setAttachmentMimeType(String mimeType)
Sets the attachment mime type.

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 String
the file to attach to the message

sendMessageViaCloud

public void sendMessageViaCloud(String sender, String recipient, String recipientName, String subject, String plainTextBody)
Deprecated. this functionality is retired and no longer works. You can use the sendgrid cn1lib or similar libraries

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 String
the name of the sender, notice all email will arrive from Codename One to avoid spam issues
recipient String
the email for the recipient
recipientName String
the display name for the recipient
subject String
e-mail subject
plainTextBody String
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

sendMessageViaCloudSync

public boolean sendMessageViaCloudSync(String sender, String recipient, String recipientName, String subject, String plainTextBody)
Deprecated. this functionality is retired and no longer works. You can use the sendgrid cn1lib or similar libraries

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 String
the name of the sender, notice all email will arrive from Codename One to avoid spam issues
recipient String
the email for the recipient
recipientName String
the display name for the recipient
subject String
e-mail subject
plainTextBody String
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

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 boolean
the cloudMessageFailSilently to set