Hello,
I am not able to send email attachment with JSP, I was just trying as although the ethics says code should be separated from JSP.
Here is my code below, remaining things like smtpuser, username and password are mentioned appropriately and plain email sending is working.
host ="smtp.gmail.com"; //"smtp.gmail.com";
user ="dpspne@gmail.com"; //"YourEmailId@gmail.com" // email id to send the emails
pass ="xyzzzz"; //Your gmail password
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to ="ryanpune@gmail.com"; // out going email id
String from ="dpspne@gmail.com"; //Email id of the recipient
String subject = request.getParameter("subject");
String messageText = request.getParameter("message");
String fileName = request.getParameter("thefile");
boolean sessionDebug = true;
String action = request.getParameter("action");
if ("Send".equals(action)) {
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText, "text/html"); // use setText if you want to send text
//Added for attachment
//messageBodyPart.setText("Pardon Ideas");
// Create the message part
javax.mail.BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText(messageText);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//MimeBodyPart attachmentPart = new MimeBodyPart();
// Part two is attachment
messageBodyPart = new MimeBodyPart();
if ("Upload".equals(action)) {
DataSource source = new FileDataSource(fileName);
//messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
}
// Put parts in message"
msg.setContent(multipart);
// end of mutipart
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, pass);
try {
transport.sendMessage(msg, msg.getAllRecipients());
out.println("message successfully sent"); // assume it was sent
}
catch (Exception err) {
out.println("message not sent"); // assume it’s a fail
}
transport.close();
}
Could anyone please let me know why it is not working and am not able to send attachment.
The HTML Form is as below
<form>
<table>
<tr>
<td align="center" colspan="2"><b>Pease send an email to let us know your requirement. Our representative will get back to you</b></td>
</tr>
<!-- <tr>
<td align="right">From:</td>
<td><input type="text" name="from"></td>
</tr>
<tr>
<td align="right">To:</td>
<td><input type="text" name="to"></td>
</tr> -->
<tr>
<td align="right">Subject:</td>
<td><input type="text" name="subject"></td>
</tr>
<tr>
<td align="right">Message:</td>
<td><textarea rows="15" cols="25" name="message"></textarea></td>
</tr>
<tr>
<td align="right" colspan="2">
<INPUT TYPE="file" NAME="thefile">
<input type="submit" name="action" value="Upload"> </td>
</tr>
<tr>
<td align="right" colspan="2">
<input type="submit" name="action" value="Send"> <input type="reset"></td>
</tr>
</table>
</form>