자바 메일 API를 사용하여 이메일에 첨부 파일을 첨부하고 있지만 프로그램을 실행하면 텍스트 파일 내용이 복사되어 메시지 본문에 삽입됩니다. 첨부 파일 대신자바 메일을 사용하여 텍스트 파일이 첨부 파일로 첨부되지 않음
public class EmailCMSUsers {
public static void main(String args[])
{
Properties props = new Properties();
props.put("mail.smtp.host", "10.10.55.11");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.starttls.enable", "true");
// To see what is going on behind the scene
//props.put("mail.debug", "true");
props.put("mail.from", "[email protected]");
Session session = Session.getInstance(props, null);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,"[email protected]");
msg.setSubject("JavaMail hello world example");
msg.setSentDate(new Date());
msg.setText("Hello, world!\n");
//Transport.send(msg);
msg.saveChanges();
/**
* for attaching the documents
*/
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("This is message body");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "D:\\Deployment Docs\\Document.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setDisposition(Part.ATTACHMENT);
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
msg.setContent(multipart);
Transport transport = session.getTransport("smtp");
transport.connect("10.10.55.11", "test123", "test123");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
System.out.println(" email sucessfully sent");
} catch (MessagingException mex) {
System.out.println("send failed, exception: " + mex);
}
}
}
setDataHandler, setDisposition 및 setFileName을 호출하는 대신 [[attachFile'] (http://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeBodyPart.html#attachFile%28java)를 호출 해보십시오. .lang.String % 29). – VGR