SeamFramework.orgCommunity Documentation
While Seam Mail does provide methods to produce templated email, there is a core set of functionality that is shared whether you use a templating engine or not.
At it's base an email consists of various destinations and content. Seam Mail provides a wide varerity of methods of ways to configure the following address fields
From
To
CC
BCC
REPLY-TO
Seam Mail leverages the JavaMail InternetAddress object internally for parsing and storage and provides a varargs method for each of the contact types. Thus you can provide either a String, multiple Strings or a String []. Addresses are parsed as RFC 822 addresses and can be a valid Email Address or a Name + Email Address.
MailMessage m = mailMessage.get();
m.from("John Doe<john@test.com>")
.to("jane@test.com")
.cc("Dan<dan@test.com", "bill@test.com")
Since we leverage standard InternetAddress object we might as well provide a method to use it.
MailMessage m = mailMessage.get();
m.from(new InternetAddress("John Doe<john@test.com>"))
Since applications frequently have their own object to represent a user who will have an email set to them we provide a simple interface which your object can implement.
public interface EmailContact {
public String getName();
public String getAddress();
}
Let's define this interface on an example user entity
@Entity
public class User implements EmailContact {
private String username; //"john@test.com"
private String firstName; //"John"
private String lastName; //"Doe"
public String getName() {
return firstName + " " + lastName;
}
public String getAddress() {
return username;
}
}
Now we can use our User object directly in an of the contact methods
User user;
MailMessage m = mailMessage.get();
m.from("John Doe<john@test.com>");
m.to(user);
Adding content to a message is simply a matter of calling the appropriate method
For plain text body
MailMessage m = mailMessage.get();
m.bodyText("This is the body of my message");
For HTML body
MailMessage m = mailMessage.get();
m.bodyHtml("This is the <b>body</b> of my message");
For HTML + plain text alternative body
MailMessage m = mailMessage.get();
m.bodyHtmlTextAlt("This is the <b>body</b> of my message", "This is a plain text alternative");
Attachments can be added using a few different implementations of the EmailAttachment.java interface
BaseAttachment.java: byte[]
InputStreamAttachment.java: java.io.InputStream
FileAttachment.java: java.io.File
URLAttachment.java: Loads attachment from URL
Attachments are added by providing the relevant information per implementations as well as a ContentDisposition which specifics if the attachment is simply "ATTACHMENT" or "INLINE" which allows the attachment to be referenced in the body when using templating
MailMessage m = mailMessage.get();
m.addAttachment(new FileAttachment(ContentDispostion.ATTACHMENT, new File("myFile"));
Here is how to reference an attachment inline. Inline attachments are referenced by their filename.
<html xmlns="http://www.w3.org/1999/xhtml"> <body> <p><b>Dear <a href="mailto:$person.email">$person.name</a>,</b></p> <p>This is an example <i>HTML</i> email sent by $version and Velocity.</p> <p><img src="$mailContext.insert("seamLogo.png")" /></p> <p>It has an alternative text body for mail readers that don't support html.</p> </body> </html>