Advertisements
Offline
Ragavarajan. J
Posts: 1
Joined: Thu Nov 01, 2012 8:01 pm
Location: VIRUDHUNAGAR
I want exactly working code for sending Email with attachment in JSP, with all details.

I am using Appache tomcat Server.

And this is my code working without attachment...

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>

<%@page import="javax.mail.*" %>
<%@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="java.util.Random" %>
<%@page import="javax.mail.internet.*"%>
<%@page import="java.io.*"%>


<%
//Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
try
{
String email=request.getParameter("mailaddress");
Random r = new Random();
int no=r.nextInt();
if(no<0)
{
no=no*-1;
}
String username="give u r username of mailid";
String password="password of u r mailid";
String recipients=email;
String block= new Integer(no).toString();

// seekerRegbean ob=new seekerRegbean();

//System.out.println("Usrename is**************"+recipients);
System.out.println("mail iddddddddddddddd"+email);
String mailhost ="smtp.gmail.com";
String subject="WWW.Uours.com";
System.out.println("SUBJECT:"+subject);
String sender=username;
//String body=block;




System.out.println("*try block");
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", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
javax.mail.Authenticator pa;

System.out.println("**try block");

pa = new javax.mail.Authenticator()
{
public javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication("uername@xx.com","your password");

}
};
Session session1 = Session.getInstance(props,pa);
MimeMessage message = new MimeMessage(session1);
message.setSender(new InternetAddress(sender));
BodyPart messageBodyPart = new MimeBodyPart();

message.setSubject(subject);

// message.setContent("hello", "text/html");


Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send(message);



//Transport.send(message);
response.sendRedirect("success.jsp");



}

catch(Exception e)
{
System.out.println(e);
}


System.out.println("*****End1*****");

%>