Tech-Today
How to send an email in Glassfish using JavaMail session injection
This tutorial will teach us how to send an email in Glassfish using JavaMail session injection. It use gmail for sending the email.
Steps:
1.) First we need to create a JavaMail session by, opening Glassfish admin: http://localhost:4848, and navigating to Resources->JavaMail Sessions.
2.) Click "New" button and input the following values:
JNDI Name: mailSession
Mail Host: smtp.gmail.com
Default User and Default Sender Address: any of your valid gmail account
3.) Leave Advance Setting unchange.
4.) On the Additional Properties section, we need to add the following properties:
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.password=yourPassword
mail.smtp.auth=true
mail.smtp.port=465
mail.smtp.socketFactory.port=465
5.) And finally a sample code that do the sending:
package com.czetsuya.demo;
import java.io.UnsupportedEncodingException;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.naming.NamingException;
/**
* @author Edward P. Legaspi
* @since Nov 18, 2012
**/
@Startup
@Singleton
public class StartupListener {
@Resource(name = "mailSession")
private Session mailSession;
@PostConstruct
private void init() throws NamingException, MessagingException,
UnsupportedEncodingException {
Message msg = new MimeMessage(mailSession);
msg.setSubject("Hello World!");
msg.setRecipient(RecipientType.TO, new InternetAddress(
"[email protected]", "czetsuya 2"));
msg.setFrom(new InternetAddress("[email protected]", "czetsuya 1"));
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hello World!.");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport.send(msg);
}
}
-
Do Some Initialization Work After The Web Application Initialization Process
We can achieve this by using a webListener. See code below: package com.czetsuya; import java.text.MessageFormat; import java.util.ResourceBundle; import javax.inject.Inject; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;...
-
Send Email using MFMailComposer (Template)1.) Import MessageUI import MessageUI 2.) Show Email func showMail() { let mailComposeViewController = configuredMailComposeViewController() if MFMailComposeViewController.canSendMail()...
-
How To Create A Custom Bean Validation In Javaee6
Bean validation has been mentioned on many articles online but I found few that explain the actual class that implements the constraint validator. For example: the bean validation article from oracle javaee6 documentation: http://docs.oracle.com/javaee/6/tutorial/doc/gkfgx.html....
-
How To Implement Jms Request/response Feature In Glassfish
This tutorial will provide a sample code on how you can send request and receive reply using jms. I've used Glassfish 3.1.1, with its built in JMS Broker for testing set to LOCAL. We need 2 maven projects (1 is for request and 1 for receiving the...
-
Send Email Using Codeigniter In 1and1 Server
I get a timeout at first try. Using the default mail type from codeigniter will not, to use the built in email feature from codeigniter we have to change protocol into smtp: Like this: $config['mailtype'] = 'html'; $config['protocol']...
Tech-Today