email servlet ( java.net.SocketException: Connection reset)
im making a web form that will submit entries and send data to my yahoo email so i found the codes below but when i try to run the program from web form i get error below can u help me pls:
"There was a problem sending the email!
javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: Connection reset
"
here is the code below:
public class EmailServletattachments extends HttpServlet {
private static final String mailserver = "mail.yahoo.com";
private static final String correctPassword = "xxx";
private String from, to, password, filename;
private String cc, bcc, subject, body;
private String warning = "\n\n\nWarning! This mail is sent by a Web form. Sender could be invalid!";
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
res.setContentType("text/plain");
ServletOutputStream out = res.getOutputStream();
File dir = (File)getServletContext().getAttribute("javax.serv let.context.tempdir");
MultipartRequest mR = new MultipartRequest(req, dir.getAbsolutePath(), 1000000);
Enumeration e = mR.getParameterNames();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
if(key.equals("from")) {
from = mR.getParameter(key);
}
if(key.equals("to")) {
to = mR.getParameter(key);
}
if(key.equals("cc")) {
cc = mR.getParameter(key);
}
if(key.equals("bcc")) {
bcc = mR.getParameter(key);
}
if(key.equals("subject")) {
subject = mR.getParameter(key);
}
if(key.equals("message")) {
body = mR.getParameter(key);
}
if(key.equals("password")) {
password = mR.getParameter(key);
}
}
if(password.equals(correctPassword)) {
try {
// Get system properties
Properties props = System.getProperties();
// Set up the mail server
props.put("mail.smtp.host", mailserver);
// Get the email session
Session session = Session.getDefaultInstance(props, null);
// Define the message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
if(cc !=null && cc.length() != 0) {
message.addRecipient(Message.RecipientType.CC,
new InternetAddress(cc));
}
if(bcc !=null && bcc.length() != 0) {
message.addRecipient(Message.RecipientType.BCC,
new InternetAddress(bcc));
}
message.setSubject(subject);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body += warning);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//Attached files
Enumeration files = mR.getFileNames();
while(files.hasMoreElements()) {
String name = (String)files.nextElement();
filename = mR.getFilesystemName(name);
String type = mR.getContentType(name);
File file = mR.getFile(name);
if(file != null) {
String attachment = dir.getAbsolutePath()+"\\"+filename;
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
}
}
message.setContent(multipart);
message.setSentDate(new Date());
Transport.send(message);
out.println("************Message Sent!************\n\n");
Enumeration headerLines = message.getAllHeaderLines();
while(headerLines.hasMoreElements()) {
out.println((String)headerLines.nextElement());
}
out.println("Message: " + body);
}
catch(Exception ex) {
out.println("There was a problem sending the email!\n\n" + ex);
}
}
else {
out.println("Bad password, go back and try again!");
}
}
} // End class EmailServletAttachments