I have the following issue, I am trying to attach a file to a mail being sent but it does not attach to the mail.
Please see code below for mail:
public static boolean sendEmail(String subject, String emailMessage, String from, String to[],String[] attachFiles ) throws MessagingException, IOException {
boolean result = true;
try {
final String username = "[email protected]";
final String password = "nMT@DMIn!";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for (int i = 0; i < to.length; i++) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
for (int i = 0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(javax.mail.Message.RecipientType.TO, toAddress[i]);
}
// creates message part MimeBodyPart messageBodyPart = new MimeBodyPart();
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();
try {
attachPart.attachFile(filePath);
} catch (IOException ex) {
ex.printStackTrace();
}
multipart.addBodyPart(attachPart);
}
}
message.setSubject(subject);
message.setContent("<p><font size=\"-1\" face=\"Verdana, Arial, Helvetica, sans-serif\">" + emailMessage + "</font></p>", "text/html");
Transport.send(message);
} catch (MessagingException ex) {
result = false;
throw ex;
}
return result;
}
And this is the code with the body of the mail:
public void queryMailPayAt() {
String sql = "select caseNumber from tblpay@qlheader where transactionid = " + selectedQuery.getRecId();
String caseId = "";
try {
Connection con = DatabaseHandler.getDBConnection(DatabaseTypes.TRANSACTION_DATABASE);
ResultSet result = DatabaseHandler.executeQuery(con, sql);
while (result.next()) {
caseId = result.getString("caseNumber");
}
} catch (Exception e) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "An error occurred.", e.getMessage()));
String ex = e.getMessage();
String ex1 = e.getLocalizedMessage();
System.out.println(ex + " " + ex1);
}
String from = "[email protected]";
String style = "<font size=\"-1\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
String msgPrefix = "<h4>New B.I.N has been logged</h4>";
String msg = "<p>See details for newly logged B.I.N.</p>"
+ "<p>Issue Case ID: " + caseId + "</p>"
+ "<table><tbody>"
+ "<tr><td>" + style + "Username: </font></td><td>" + style + user.getUsername() + "</font></td></tr>"
+ "<tr><td>" + style + "First Name: </font></td><td>" + style + user.getFirstName() + "</font></td></tr>"
+ "<tr><td>" + style + "Last Name: </font></td><td>" + style + user.getLastName() + "</font></td></tr>"
+ "<tr><td>" + style + "Transaction ID: </font></td><td>" + style + selectedQuery.getRecId() + "</font></td></tr>"
+ "</tbody></table>"
+ "<p> If this is a valid query please confirm on the Open queries page </p>"
+ "<p>Please contact <a href=\"mailto:[email protected]\">Pay@ Support</a> for any further assistance.</p>";
String emailMessage = msgPrefix + msg;
String subject = "New Logged B.I.N: " + caseId;
String[] attachFiles = new String[1];
attachFiles[0] = "C:\\Users\\kroon_000\\Desktop\\files\\"+ caseId + ".pdf" ;
try {
String[] to = {"[email protected]", user.getEmailAddress() };
Utils.sendEmail(subject, emailMessage, from, to, attachFiles);
} catch (Exception ex) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "An error occurred.", "Please try again."));
}
}
If anyone could help me where I am doing something wrong it would be much appreciated.
In your code, multipart
is created but not used. You should add:
message.setContent(multipart);