0
votes

I have a function to send mail with attachment in java. It works when i uploaded the attachment. However, the problem is that if i have to send a mail without attachment, it says error when i send a mail and i did not upload any attachment.

here is my code:

 Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {
        String html = text;

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(email));
        message.setSubject(subject);
        BodyPart messageBodyPart = new MimeBodyPart(); 
        messageBodyPart.setContent(html, "text/html"); 
        Multipart multipart = new MimeMultipart(); 
        multipart.addBodyPart(messageBodyPart); 
        messageBodyPart = new MimeBodyPart(); 
        String filename = "C:/Users/gro/Desktop/"+attachment; 
        DataSource source = new FileDataSource(filename); 
        messageBodyPart.setDataHandler(new DataHandler(source)); 
        messageBodyPart.setFileName(filename); 
        multipart.addBodyPart(messageBodyPart);  
        message.setContent(multipart);

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

any idea how i can solve this?

i get this error:

org.apache.jasper.JasperException: java.lang.RuntimeException: javax.mail.MessagingException: IOException while sending message; nested exception is: java.io.FileNotFoundException: C:\Users\gro\Desktop (Access is denied)

5
What error do you get? - SLaks
org.apache.jasper.JasperException: java.lang.RuntimeException: javax.mail.MessagingException: IOException while sending message; nested exception is: java.io.FileNotFoundException: C:\Users\gro\Desktop (Access is denied) - Aalm
What part of the error don't you understand? Why would you expect your code to work? - SLaks
this error happens when i send a mail without attachment.. if i have not uploaded a file then i cant send the mail - Aalm
Read the error message. You're trying to attach a file that doesn't exist. You need to change your code to not do that. - SLaks

5 Answers

0
votes

Its just not able to find the file..

I am not sure where you are getting the attachment field set.

But, wrapping the fragment of code that is attaching the file to message, something like this below should work for you:

if(attachment != null && attachment.length() > 0) {    
    messageBodyPart = new MimeBodyPart(); 
    String filename = "C:/Users/gro/Desktop/"+attachment; 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 
    multipart.addBodyPart(messageBodyPart); 
}
0
votes

I don't quite understand your implementation as the filename is set within the code without any user interaction. I suggest you change the body a little bit to get something like:

if(filename!=null) {
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);

}
0
votes

Let me see if I understand, you get this when you try to sent an email without attachment??
So the variable "attachment" is null??
So you are trying to load the file "C:/Users/gro/Desktop/null"??
And you are get a FileNotFoundException for that?? -.-

0
votes

Without seeing what "attachment" is, it is going to be hard to figure out what you are doing. As others have already mentioned above, you need to check the value of "attachment" and if it exists (non-null or File.exists()) then the block of code for attaching the file to email should be executed.

0
votes

Can you please hardcode the file path (including file name) and try the operation, I think your 'attachement' is either getting set to null or some non existing directory.