Tutorial #99: Send e-mail using JavaMail in JSP

Hi everyone!

JavaMail is a well known Java API used to send and receive email via SMTP, POP3 and IMAP. It mainly provides a platform-independent and protocol-independent framework to build mail and messaging applications. In addition, the JavaMail API package is also included in the Java EE platform.

JavaMail facilitates the sending of an e-mail via Gmail SMTP server, using both TLS and SSL connection. Through this tutorial, we will learn how to send an e-mail using JavaMail in a sample JSP page.

Pre-requisites: Eclipse IDE for Java EE developers, Apache Tomcat 7

Step 1: Create new Dynamic web project

Launch Eclipse IDE and create a new Dynamic web project called SendEmailUsingJSP with target runtime as Apache Tomcat 7.

Step 2: Add external jar files

In order to implement the sending of an e-mail, developers need to download and use the latest version of the JavaMail API in their web projects. For this tutorial, I will be using the mail-1.4.7.jar version of the JavaMail API. Make sure you add the .jar file in the WEB-INF/lib folder of your web project.

Step 3: Create JSP page

Create a new index.jsp page inside the WebContent folder of your web project and write the following code.

index.jsp

<%@ pageimport="javax.servlet.http.*,javax.servlet.*" %>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%

    //username for abc@gmail.com will be "abc"
	String username = "sender_username";
	String password = "sender_password";
	String result = null;

	try {
		
        Properties props = System.getProperties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", "smtp.gmail.com");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.debug", "true");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session emailSession = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("sender_username","sender_password");
				}
		});

		emailSession.setDebug(true);
		Message message = new MimeMessage(emailSession);
		message.setFrom(new InternetAddress(
				"sender_username@gmail.com"));
		message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse("xyz@hotmail.com"));
		message.setSubject("Test mail from Java");
		message.setText("Hello. this is a test");

		Transport transport = emailSession.getTransport("smtps");
		transport.connect("smtp.gmail.com", username, password);
		transport.sendMessage(message, message.getAllRecipients());

		result = "Successfully sent email";

	   } catch (MessagingException e) {
		result = "Unable to send email";
	}
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
	<center>
		<h1>Send Email using JSP</h1>
	</center>
	<p align="center">
		<%
			out.println("Result: " + result + "\n");
		%>
	</p>
</body>
</html>

Finally, save all changes and run the application on the Tomcat server. If no errors occur then you should see the following output!

output

Reference: JavaMail API

Send e-mail using Jython in Oracle Data Integrator

Hello friends!

Oracle Data Integrator includes a built-in tool called OdiSendMail which is used to send an email through a SMTP server. However, OdiSendMail will work only if the mail server does not require authentication.

In this post, I will demonstrate how to send an e-mail with the help of Gmail’s SMTP server. Instead of going for the OdiSendMail tool, I will be using the Jython technology in order to implement the same functionality.

Pre-requisites: Oracle Data Integrator 11g (build version 11.1.1.7.0)

Open ODI Studio and follow the below steps!

Step 1: Create a new Procedure

Create a new Procedure under any of your existing Projects in the Designer tab. Name the procedure SendEmail. Switch to Details and add a new entry.

create new procedure

procedure_1

Now, under Command on Target, you need to write the Jython code. Please make sure you have selected Technology as Jython.

procedure_3

Jython code

import smtplib
import string
BODY = string.join((
"From: %s" % 'senderemailaddress@gmail.com',
"To: %s" % 'recipientemailaddress@gmail.com',
"Subject: %s" % 'Test Mail',
"",'This is a test email from ODI'
), "\r\n")
sender = smtplib.SMTP('smtp.gmail.com',587)
sender.set_debuglevel(1)
sender.ehlo()
sender.starttls()
sender.ehlo()
sender.login('senderusername', 'senderpassword')
sender.sendmail('senderemailaddress',['recipientemailaddress'],BODY)
sender.close()

Note: You need to enter your valid Gmail credentials and replace sender and recipient address with valid email addresses in the above code.

Finally, run this procedure. Check the status of the operation under the Operator tab.

Output

Output

output_2

Source: Send mail in ODI using Gmail Credentials