Hi I am trying to send an email with attachment using html and jsp.my coding is working properly when I use that in my local machine but when I upload the code on the
tomcat web server it is returning error saying that messaging exception and file not found exception.Please help anybody help me out and let me know what could be the problem.here is the code I am using for sending the attachment.
<%@page import="java.util.*"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*,java.io.*,java.util.*,javax.mail.*,javax.activation.*"%>
<%
String to = request.getParameter("to");
System.out.println("the recipient address is:"+to);
String from = request.getParameter("from");
String sub = request.getParameter("subject");
String msg = request.getParameter("message");
String Attachment = request.getParameter("filename");
System.out.println("This is the file name"+Attachment);
System.out.println("this is from send mail:"+to + from + sub +msg);
Properties props = new Properties();
props.put("mail.smtp.host", "smtpgateway");
Session s = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(s);
StringTokenizer st = new StringTokenizer(to, ",");
int tokenCount = st.countTokens();
System.out.println("no.of receipients:"+tokenCount);
InternetAddress [] recipientList = new InternetAddress[tokenCount];
for (int i = 0; st.hasMoreTokens(); i++)
{
String msgTo = st.nextToken();
System.out.println("the recipients address is:"+msgTo);
recipientList[i]= new InternetAddress(msgTo);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(msgTo));
}
message.setFrom(new InternetAddress(from));
message.setSubject(sub);
message.setText(msg);
if(Attachment !="")
{
System.out.println("entered in to the if");
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(Attachment);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
System.out.println(fds.getName());
mp.addBodyPart(mbp);
// Add the Multipart to the message
message.setContent(mp);
}
// Set the Date: header
message.setSentDate(new Date());
// Send the message;
Transport.send(message);
%>
[ December 23, 2008: Message edited by: Anna Anna ]