please help me in sending email using java mail api
shobha devi
Greenhorn
Joined: Mar 11, 2009
Posts: 11
posted
0
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.
<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: 35249
7
posted
0
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.