Steps:
1.) First nosotros ask to utilization a JavaMail session by, opening Glassfish admin: http://localhost:4848, in addition to navigating to Resources->JavaMail Sessions.
2.) Click "New" push in addition to input the next values:
JNDI Name: mailSession
Mail Host: smtp.gmail.com
Default User in addition to Default Sender Address: whatsoever of your valid gmail account
3.) Leave Advance Setting unchange.
4.) On the Additional Properties section, nosotros ask to add together the next 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 in conclusion a sample code that utilization 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 November 18, 2012 **/ @Startup @Singleton populace shape StartupListener { @Resource(name = "mailSession") soul Session mailSession; @PostConstruct soul void init() throws NamingException, MessagingException, UnsupportedEncodingException { Message msg = novel MimeMessage(mailSession); msg.setSubject("Hello World!"); msg.setRecipient(RecipientType.TO, novel InternetAddress( "to@gmail.com", "czetsuya 2")); msg.setFrom(new InternetAddress("from@gmail.com", "czetsuya 1")); BodyPart messageBodyPart = novel MimeBodyPart(); messageBodyPart.setText("Hello World!."); Multipart multipart = novel MimeMultipart(); multipart.addBodyPart(messageBodyPart); msg.setContent(multipart); Transport.send(msg); } }
0 komentar:
Please comment if there are any that need to be asked.