Meaningless Drivel is fun!
The moose likes Other JSE/JEE APIs and the fly likes please help me in sending email using java mail api Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "please help me in sending email using java mail api" Watch "please help me in sending email using java mail api" New topic

please help me in sending email using java mail api

shobha devi

Joined: Mar 11, 2009
Posts: 11
i have encountered a problem where the statements after the Transport.send(msg) are not getting executed in sendmail2.jsp. it indefinitely hangs without any exception. i am using netbeans and i have included all the jar files in the project libraries. hope some one helps me out in this regard.


function schedule()

function draft()
function send()
<title>E-Mail Scheduler</title>
<body style="margin:10" onload="SetFocus()">
<form name="f1">
<h1><center>COMPOSE MAIL</center></h1>
<table cellpadding=5 cellspacing=2 align="center">
<td><input type=text name="to" size=76></td>
<td><input type=text name="cc" size=76></td>
<td><input type=text name="bcc" size=76></td>
<td><input type=text name="sub" size=76></td>

<table cellpadding=5 cellspacing=2 align="center">
<tr><td><textarea name="body" rows=15 cols=65></textarea></td></tr>

<br><center><INPUT TYPE="submit" name="s" value = "Schedule" onclick="return schedule()"></input>
    <INPUT TYPE="submit" name="sd" value = "Save and Draft" onclick="return draft()"></input>    <INPUT TYPE="submit" name="si" value = "Send immediately" onclick="return send()"></input></center>


<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@page import="java.util.Properties,java.util.Date,javax.mail.*,javax.mail.Transport,javax.mail.internet.*,javax.activation.*,*,javax.naming.*"%>

String to=request.getParameter("to");
String cc=request.getParameter("cc");
String bcc=request.getParameter("bcc");
String sub=request.getParameter("sub");
String body=request.getParameter("body");
String from="";
String host="";
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("", host);
//set the authentication
//enable SSL connection
//STARTTLS is the ESMTP keyword used to initiate a secure SMTP connection between
// two servers using the Secure Sockets Layer (SSL) (also known as TLS).
Session ses = Session.getInstance(props);
try {
// Instantiatee a message
MimeMessage msg = new MimeMessage(ses);
//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
InternetAddress[] address1 = {new InternetAddress(cc)};
InternetAddress[] address2 = {new InternetAddress(bcc)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setRecipients(Message.RecipientType.CC, address1);
msg.setRecipients(Message.RecipientType.BCC, address2);
msg.setSentDate(new Date());
// Set message content
//Send the message

Transport tr=ses.getTransport("smtp");

out.println("hi mail sending is under process<br>");
out.println("Message has been sent sucessfully");
catch (Exception mex) {
// Prints all nested (chained) exceptions as well

in the above code of sendmail2.jsp statements after the line which is in bold are not getting executed. please help me out. i am doing mail scheduling as my final year project and this is one of the important module
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read as it is, making it less likely that people will do so.

One thing that jumps out is that the code doesn't handle authentication (which Google Mail requires).

Also, it's considered bad design to do stuff like this in a JSP; use a backing bean or a servlet instead.
I agree. Here's the link:
subject: please help me in sending email using java mail api
It's not a secret anymore!