i have a classA where i have file object and another classB to send email with attachment. What i try to do is, get the file object from the classA in classB and send it by email as attachment.
When i execute my code, it sends the file as attachment but in the email, the file has no name.
ClassA:
import java.io.File;
import javax.mail.MessagingException;
public class A{
public void A(){
System.out.println("Sending the file...");
File file = new File("c:\\temp\\FileA.txt");
}
}
ClassB:
public class B {
public static void B(File file) throws MessagingException {
String host = "smtp.gmail.com";
String Password = "***";
String from = "***@gmail.com";
String toAddress = "***@gmail.com";
//Here i don t want to use this file
//String filename = "C:/file.txt";
// Get system properties
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject("Attachment TEST ");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("coucou the file is here");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
//messageBodyPart.setFileName(file);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
try {
Transport tr = session.getTransport("smtps");
tr.connect(host, from, Password);
tr.sendMessage(message, message.getAllRecipients());
System.out.println("Mail Sent Successfully");
tr.close();
} catch (SendFailedException sfe) {
System.out.println(sfe);
}
}
public static void main(String args[]) throws MessagingException{
B file = new B();
}
}
What should i do to get the exact file name in my email? Thank you