File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

please help me in sending email using java mail api

shobha devi
Greenhorn

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.

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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42920
    
  68
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: http://aspose.com/file-tools
 
subject: please help me in sending email using java mail api