2

I would like to know what I am doing wrong here (what I am missing). My email does get sent out but without the attachment. The file I am sending is simply called "log". As you will see I am trying multiple things here, I have also tried them one by one, but none of them are working:

            MimeMessage message = new MimeMessage(session);
            MimeBodyPart emailAttachment = new MimeBodyPart();
            Multipart multipart = new MimeMultipart();
            int len = build.getLogFile().getPath().length();
            //I have verified that "file" provides the right path
            String file = build.getLogFile().getPath().substring(0, (len-3));
            String fileName = "log";
            DataSource source = new FileDataSource(file);
            emailAttachment.setDataHandler(new DataHandler(source));
            //I know this .attachFile is not needed but I added it when nothing was working
            emailAttachment.attachFile(build.getLogFile());
            emailAttachment.setFileName(fileName);
            multipart.addBodyPart(emailAttachment);
            message.setContent(multipart);  
            message.setFrom(adminAddress);
            message.setText(builder.toString());
            message.setSentDate(new Date());


            mailSender.send(message);

Thanks

S M
  • 3,699
  • 3
  • 10
  • 5
  • it might help http://stackoverflow.com/questions/17156544/sending-an-email-with-an-attachment-using-javamail-api – RockAndRoll Oct 23 '15 at 05:45
  • Are you _certain_ that the file to which you are referring _exists_ and also has content? Your code looks fine other than this possibility. – Tim Biegeleisen Oct 23 '15 at 05:47
  • I think you should try debugging and step through your send method line by line. – Tim Biegeleisen Oct 23 '15 at 06:00
  • I did verify that the file exists by trying to catch if the file was null. It was not null. I can manually go to that directory and find the file called log. and it does have context but it does not have a file extension. would the fact that it does not have a file extension make a difference? – S M Oct 23 '15 at 06:27

1 Answers1

0

use this code=>

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendFileEmail
 {
 public static void main(String [] args)
{


  String to = "abcd@gmail.com";

  String from = "web@gmail.com";

  String host = "localhost";

  Properties properties = System.getProperties();

  properties.setProperty("mail.smtp.host", host);

  Session session = Session.getDefaultInstance(properties);

  try{

     MimeMessage message = new MimeMessage(session);

     message.setFrom(new InternetAddress(from));

     message.addRecipient(Message.RecipientType.TO,
                              new InternetAddress(to));

     message.setSubject("This is the Subject Line!");

     BodyPart messageBodyPart = new MimeBodyPart();

     messageBodyPart.setText("This is message body");

     Multipart multipart = new MimeMultipart();

     multipart.addBodyPart(messageBodyPart);

     messageBodyPart = new MimeBodyPart();
     String filename = "file.txt";
     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("Sent message successfully....");
  }catch (MessagingException mex) {
     mex.printStackTrace();
   }
}
}
Viraj Amarasinghe
  • 911
  • 10
  • 20