【发布时间】:2016-10-23 12:13:20
【问题描述】:
我创建了一种使用带有多个文件附件的 java GUI 发送邮件的方法。当该方法发送邮件时,它会将附件文件类型(如 microsoft office 和 pdf)作为 BitSarver 发送到接收邮件。我需要按类型发送文件,以便其他自动系统可以通过读取附件文件并打印它来工作。
这就是方法。
static public boolean sendMail(String to,String subject,String text_filed)
{
Properties props = new Properties();
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.socketFactory.port ","465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("eamil", pass);
}
}
);
try
{
Message m = new MimeMessage(session);
m.setFrom(new InternetAddress(userMAil));
System.out.println("from "+userMAil);
m.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
System.out.println("to "+to);
m.setSubject(subject);
System.out.println("the subject "+subject);
MimeBodyPart bodypart = new MimeBodyPart();
bodypart.setText(text_filed);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(bodypart);
System.out.println("the content "+text_filed);
for(int i = 0;i< main_controller.attachments_path.size();i++)
{
bodypart = new MimeBodyPart();
DataSource source = new FileDataSource(main_controller.attachments_path.get(i));
System.out.println("the file location"+main_controller.attachments_path.get(i));
bodypart.setDataHandler(new DataHandler(source));
//bodypart.setFileName(main_controller.filesFrame.getFilename());
System.out.println("the file name"+main_controller.filesFrame.getFilename());
multipart.addBodyPart(bodypart);
}
m.setContent(multipart);
//m.setText(text_filed);
Transport.send(m);
JOptionPane.showMessageDialog(null, "done!");
return true;
}
catch(Exception e)
{
String m = e.getMessage();
JOptionPane.showMessageDialog(null, e);
System.out.println(m);
return false;
}
}
希望你能帮助我:D
【问题讨论】:
标签: java email pdf jakarta-mail