Hello Genius, here my problem was i am able to send a text mail simply in jsp. But With a attachment, I am not able to send mail through jsp coding.
Here My coding. Please Help to solve this Problem want to work in jsp.
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@page import="javax.mail.*" %>
<%@page import="java.io.*" %>
<%@page import="javax.activation.*" %>
<%@page import="java.security.Security" %>
<%@page import="java.util.Properties" %>
<%@page import="javax.mail.Message" %>
<%@page import="javax.mail.Session" %>
<%@page import="javax.mail.Transport" %>
<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.internet.MimeBodyPart"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.internet.MimeMultipart"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.util.Properties"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.Properties"%>
<%@page import="javax.activation.DataHandler"%>
<%@ page import="java.util.Random" %>
<%
try{
String email=request.getParameter("mailaddress");
String username="zzz@gmail.com"; // Mailid
String password="xxx"; //Password
String recipients=email;
String attachFiles[]=new String[50];
String mailhost ="smtp.gmail.com";
String subject="WWW.Uours.com";
String filename=request.getParameter("f1"); // getting filename from previous page & assigned
String mytext="Sample";
System.out.println("SUBJECT:"+subject);
String sender=username;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "111");
props.put("mail.debug","true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.n et.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
javax.mail.Authenticator pa;
pa = new javax.mail.Authenticator()
{
public javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication("Your MailID@gmail.com","Your Password");
}
};
Session session1 = Session.getInstance(props,pa);
Message message = new MimeMessage(session1);
message.setFrom(new InternetAddress(sender));
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(mytext, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
MimeBodyPart attachPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(new File(filename).getName());
multipart.addBodyPart(attachPart);
// sets the multi-part as e-mail's content
message.setContent(multipart);
// sends the e-mail
Transport.send(message);
//if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
//else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
//Transport.send(message);
response.sendRedirect("success.jsp");
}
catch (MessagingException mex) {
}
%>