निम्न जावा कोड को किसी फ़ाइल को फ़ाइल संलग्न करने के लिए उपयोग किया जाता है। मैं ईमेल के माध्यम से एकाधिक फाइल संलग्नक भेजना चाहता हूं। किसी भी सुझाव की सराहना की जाएगी।JavaMail का उपयोग कर ईमेल में एकाधिक फ़ाइलों को कैसे संलग्न करें?
public class SendMail {
public SendMail() throws MessagingException {
String host = "smtp.gmail.com";
String Password = "mnmnn";
String from = "[email protected]";
String toAddress = "[email protected]";
String filename = "C:/Users/hp/Desktop/Write.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("JavaMail Attachment");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Here's the file");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
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);
}
}
}`
के साथ 100% woking है, तो यह काम करने के लिए प्रतीत नहीं होता। यह केवल अंतिम फ़ाइल संलग्न करता है (इस मामले में, ईमेल में केवल file2.txt है) – Jason
समाधान अच्छी तरह से काम करता है !! –