How To Send Email With Attachment in JSP
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   pratyus_sarangi
Posted On:   Friday, May 23, 2003 04:01 AM

Status I have written a Servlet Below But Its NOT WORKING SHOWING EXCEPTION message not send ======================================= import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import javax.servlet.*; import javax.servlet.http.*; public class SendMailA extends HttpServlet { private String smtpHost; public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, java.io.IOException { smtpHost = "mail.comptronics.org"; String from = request.getParameter("from"); String to = request.getParameter("to"); String cc = req   More>>
Status

I have written a Servlet Below But Its NOT WORKING SHOWING EXCEPTION message not send

=======================================
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SendMailA extends HttpServlet
{

private String smtpHost;

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, java.io.IOException
{
smtpHost = "mail.comptronics.org";
String from = request.getParameter("from");
String to = request.getParameter("to");
String cc = request.getParameter("cc");
String bcc = request.getParameter("bcc");
String subject = request.getParameter("subject");
String text = request.getParameter("text");
String fileAttachment = request.getParameter("attach");

String status =null;

try {
// Create the JavaMail session
java.util.Properties properties = System.getProperties();
properties.put("mail.smtp.host", smtpHost);
Session session = Session.getInstance(properties, null);

// Construct the message
MimeMessage message = new MimeMessage(session);

// Set the from address
Address fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);

// Parse and set the recipient addresses
Address[] toAddresses = InternetAddress.parse(to);
message.setRecipients(Message.RecipientType.TO,toAddresses);


Address[] ccAddresses = InternetAddress.parse(cc);
message.setRecipients(Message.RecipientType.CC,ccAddresses);

Address[] bccAddresses = InternetAddress.parse(bcc);
message.setRecipients(Message.RecipientType.BCC,bccAddresses);

// Set the subject and text
message.setSubject(subject);

MimeBodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setText(text);

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

messageBodyPart = new MimeBodyPart();

DataSource source = new FileDataSource(fileAttachment);

messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);

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

Transport.send(message);

status = "Your message was sent.";

} catch (AddressException e) {
status = "There was an error parsing the addresses.";
} catch (SendFailedException e) {
status = "There was an error sending the message.";
} catch (MessagingException e) {
status = "There was an unexpected error.";
}

// Output a status message
response.setContentType("text/html");

java.io.PrintWriter writer = response.getWriter();

writer.println(" ");
writer.println("

" + status + "

");

writer.close();
}
}    <<Less
About | Sitemap | Contact