• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please help me in sending email using java mail api

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Composemail.jsp


<html>
<script>
function schedule()
{

f1.action="mailschedule.jsp";
f1.submit();
}
function draft()
{
f1.action="savedraft.jsp";
f1.submit();
}
function send()
{
f1.action="sendmail2.jsp";
f1.submit();
}
</script>
<head>
<title>E-Mail Scheduler</title>
</head>
<body style="margin:10" onload="SetFocus()">
<form name="f1">
<h1><center>COMPOSE MAIL</center></h1>
<table cellpadding=5 cellspacing=2 align="center">
<tr>
<td>To</td>
<td><input type=text name="to" size=76></td>
</tr>
<tr>
<td>Cc</td>
<td><input type=text name="cc" size=76></td>
</tr>
<tr>
<td>Bcc</td>
<td><input type=text name="bcc" size=76></td>
</tr>
<tr>
<td>Subject</td>
<td><input type=text name="sub" size=76></td>
</tr>
</table>

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

<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>
</body>
</html>


sendmail2.jsp


<%@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.*,java.io.*,javax.naming.*"%>

<html>
<body>
<%
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="shobhabusseti@gmail.com";
String host="smtp.gmail.com";
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
//set the authentication
props.put("mail.smtp.auth","true");
//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).
props.put("mail.smtp.starttls.enable","true");
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.setSubject(sub);
msg.setSentDate(new Date());
// Set message content
msg.setText(body);
//Send the message

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

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

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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic