• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Cannot send email which is a business service during login process

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is my JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Process</title>
</head>
<body>
<form action="RegistrationProcess" method="post">
First Name : <input type="text" name="fName" /><br>
Last Name : <input type="text" name="lName" /><br>
Email Address : <input type="text" name="emailAddress" /><br><br>
<input type="submit"/>
</form>
</body>
</html>

Below is the servlet.

package org.javabrains.registration;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/RegistrationProcess")
public class Registration extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

RegistrationUserBean user = new RegistrationUserBean();

user.setfName(request.getParameter("fName"));
user.setlName(request.getParameter("lName"));
user.setEmailAddress(request.getParameter("emailAddress"));
user.setRefNo(request.getParameter("refNo"));

if(user.getRefNo()==null || user.getRefNo()==""){

RegistrationService regService = new RegistrationService();
regService.registerUser(user);
}
}
}

Below is the business service.
My Code stops at this line: EmailSendingService emailService = new EmailSendingService();

package org.javabrains.registration;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.UUID;

import org.connections.ConnectionManager;
import org.emailService.EmailSendingService;

public class RegistrationService {

private Statement stmt;
private static Connection conn;
private boolean result;

@SuppressWarnings("finally")
public boolean registerUser(RegistrationUserBean user) {

try {
//Connect To DB;
conn = ConnectionManager.getConnection();
stmt = conn.createStatement();

Date date = new Date(); // java.util
java.sql.Date today = new java.sql.Date(date.getTime()); // SQL Date
user.setRegDate(today);
user.setIsValid("YES");

UUID uuid = UUID.randomUUID();
user.setRefNo(uuid.toString());

//Inserting User Record
String sql = "INSERT INTO REGISTRATION_DTLS (REG_FNAME, REG_LNAME, REG_EMAILADDR, REG_REFNO, REG_DATE, REG_ISVALID) VALUES ('"
+ user.getfName() + "','" + user.getlName() + "','"
+ user.getEmailAddress() + "','" + user.getRefNo() + "',"
+ "TO_DATE('" + user.getRegDate() + "','RR-MM-DD')" + ",'" + user.getIsValid() + "')";

int rows = stmt.executeUpdate(sql);
System.out.println("Rows - " + rows);
if (rows == 0)
result = false;
else{
System.out.println("Initiating Email Service");
EmailSendingService emailService = new EmailSendingService();
System.out.println("Email Service Instance Created");
boolean EmailStatus = emailService.sendMail(user);
if(EmailStatus)
result = true;
else
result = false;
}
System.out.println("Out Of Else Statement");
stmt.close();
conn.close();
} catch (SQLException se) {
// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// finally block used to close resources
if (stmt != null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}

if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
}

This is my mail sending class. I have included username and password in this class for time being for my code to get executed.
This class runs fine when i run it as java application (with main method in place).
I have include both jars - mail.jar & Activation.jar

package org.emailService;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.javabrains.registration.RegistrationUserBean;

public class EmailSendingService {

public boolean sendMail(RegistrationUserBean user) {
try{
System.out.println("Initiated For Email Service");

final String username = "XXX@gmail.com";
final String password = "XXX";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
System.out.println("Authentication Completed");

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("XXX@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(user.getEmailAddress()));
message.setRecipients(Message.RecipientType.CC,
InternetAddress.parse("XXX@gmail.com"));
message.setSubject("Proceed To Complete Registration");
message.setText("Hi,"
+ "\nPlease Click Link Below To Complete Registration"
+ "\nI am Very Happy");

Transport.send(message);

System.out.println("Done...Mail Sent");
}
catch (Exception ex){
System.out.println("Exception Thrown");
return false;
}
return true;
}
}


In the business service class.
My Code stops at this line: EmailSendingService emailService = new EmailSendingService();
Error.jpg
[Thumbnail for Error.jpg]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!