File1: index.jsp
<form action="target.jsp" method="post">
<table>
<tr><td>
<input type="checkbox" name="chk" id="chk" value="xyz.doc">
</td></tr>
<tr><td>
<input type="checkbox" name="chk" id="chk" value="abc.png">
</td></tr>
<tr><td>
<input type="checkbox" name="chk" id="chk" value="abc.pdf">
</td></tr>
<tr><td>
<input type="checkbox" name="chk" id="chk" value="xyz.jpg">
</td></tr>
<tr><td>
<input type="submit" name="submit" value="Submit">
</td></tr>
</table>
</form>
File2: target.jsp
<%@ page import="java.util.*"%>
<%@ page import="mail.*"%>
ArrayList arr=new ArrayList();
HashMap submap=new HashMap();
String[] chkname=request.getParameterValues("chk");
if (chkname != null)
{
for (int i = 0; i < chkname.length; i++)
{
submap.put("chk"+i,chkname[i]);
arr.add(submap);
}//for
}//if
mail sm = new mail();
res=sm.SendMail(SUBJECT,"BODY",emailid,ccemailid,arr);
File3: mail.java
public int SendMail(String subject, String messageText,String TO,String cc,ArrayList arr)
throws MessagingException
{
URL url=null;
String username = "";
String password = "";
String mail_transport_protocol = "";
String mail_smtp_port = "";
String mail_smtp_host = "";
try
{
username = <SMTP USERNAME>;
password = <SMTP PASSWORD>;
mail_transport_protocol = smtp;
mail_smtp_port = <PORT>;
mail_smtp_host = <SMTP HOST NAME>;
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setContent(messageText, mail_messageText);
message.setSubject(subject);
int index=0;
while(index < arr.size())
{
HashMap data=(HashMap) arr.get(index);
messageBodyPart = new MimeBodyPart();
url = new URL("http://domain:8080/"+data.get("doc"+index));
String filename=data.get("doc"+index).toString();
filename=filename.substring(filename.lastIndexOf("/")+1,filename.length());
//String extension=filename.substring(filename.lastIndexOf(".")+1,filename.length());
URLDataSource sourcedocs = new URLDataSource(url);
messageBodyPart.setDataHandler(new DataHandler(sourcedocs));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
index++;
}//while
message.setContent(multipart);
Transport.send(message);
}try
catch(Exception e)
{
System.out.println((new StringBuilder("Error in sending mail:")).append(e).toString());
}//catch
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
No comments:
Post a Comment