Tôi đang cố gắng gửi email có tệp đính kèm trong Java.Gửi email có tệp đính kèm bằng cách sử dụng javamail API
Khi tôi gửi email mà không có tệp đính kèm, tôi nhận được email, nhưng khi tôi thêm tệp đính kèm, tôi không nhận được bất kỳ thứ gì và tôi không nhận được bất kỳ thông báo lỗi nào.
này được mã Tôi đang sử dụng:
public void send() throws AddressException, MessagingException{
//system properties
Properties props = new Properties();
props.put("mail.smtp.localhost", "localhost");
props.put("mail.smtp.host",Configurations.getInstance().email_serverIp);
/*
* create some properties and get the default Session
*/
session = Session.getDefaultInstance(props, null);
//session
Session session = Session.getInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
message.setSubject("Testing Subject");
message.setText("PFA");
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
generateCsvFile("/tmp/test.csv");
messageBodyPart = new MimeBodyPart();
String file = "/tmp/test.csv";
String fileName = "test.csv";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
System.out.println("Sending");
Transport.send(message);
System.out.println("Done");
}
private static void generateCsvFile(String sFileName)
{
try
{
FileWriter writer = new FileWriter(sFileName);
writer.append("DisplayName");
writer.append(',');
writer.append("Age");
writer.append(',');
writer.append("YOUR NAME");
writer.append(',');
writer.append('\n');
writer.append("Zou");
writer.append(',');
writer.append("26");
writer.append(',');
writer.append("zouhaier");
//generate whatever data you want
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
Làm thế nào tôi có thể sửa chữa điều này?