Javax.mail.message
javax.mail.Message
is a class in the JavaMail API that represents an email message. It is the top-level class for all email messages and provides methods for setting and getting various attributes of the message, such as the sender, recipient, subject, and body.
Here are some key methods and attributes of the Message
class:
Attributes:
getFrom()
: Returns the sender of the message.getRecipients()
: Returns the recipients of the message.getSubject()
: Returns the subject of the message.getContent()
: Returns the content of the message.getContentType()
: Returns the content type of the message.
Methods:
setFrom()
: Sets the sender of the message.addRecipient()
: Adds a recipient to the message.setSubject()
: Sets the subject of the message.setContent()
: Sets the content of the message.setContent(String, String)
: Sets the content of the message with a specific content type.addHeader()
: Adds a header to the message.removeHeader()
: Removes a header from the message.
Example usage:
Here is an example of how to create a new Message
object and set its attributes:
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
// Create a new message
Message msg = new MimeMessage(session);
// Set the sender
msg.setFrom(new InternetAddress("[email protected]"));
// Set the recipient
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
// Set the subject
msg.setSubject("Test email");
// Set the content
msg.setText("This is a test email");
// Send the message
Transport.send(msg);
In this example, we create a new MimeMessage
object using the Session
object, set the sender, recipient, subject, and content, and then send the message using the Transport
object.