Created
June 25, 2013 18:43
-
-
Save Snugglepantz/5861128 to your computer and use it in GitHub Desktop.
JavaMail EJB Resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.annotation.Resource; | |
import javax.ejb.Stateless; | |
import javax.mail.*; | |
import javax.mail.internet.InternetAddress; | |
import javax.mail.internet.MimeMessage; | |
/** | |
* | |
* @author jphoff | |
*/ | |
@Stateless | |
public class EmailBean { | |
@Resource(mappedName = "mail/") | |
private Session mailSession; | |
public void submitEmail() { | |
MimeMessage message = new MimeMessage(mailSession); | |
try { | |
message.setContent("Content", "text/html"); | |
message.setSubject("Subject"); | |
final Address from = new InternetAddress("Email@mail.com", false);//Any Email | |
message.setRecipient(Message.RecipientType.TO, from); | |
message.setFrom(from); | |
// Transport message | |
message.saveChanges(); // implicit with send() | |
Transport transport = mailSession.getTransport("smtp"); | |
transport.connect(); | |
if (transport.isConnected() == false) { | |
return; | |
} | |
transport.sendMessage(message, message.getAllRecipients()); | |
transport.close(); | |
} catch (MessagingException e) { | |
// handle catch | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment