2014-09-03 44 views
0

我想用java发送附件中的文件。 我有两个类,其中一个指定文件位置,第二个类用作工具类发送电子邮件 因此,当我执行第一个类时,它不会发送电子邮件。Java用附件中的文件发送电子邮件

第一类:

public class SendFile { 
    private static String[] args; 

    public static void sendEmail(File filetosend) throws IOException, Exception{ 

    //public static void main(String[] args) throws IOException { 

    final String username = "[email protected]"; 
    final String password = "password"; 

    Properties props = new Properties(); 
    props.put("mail.smtp.auth", true); 
    props.put("mail.smtp.starttls.enable", true); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.port", "587"); 

    Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password); 
       } 
      }); 

    try { 

     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
     message.setSubject("Attach file Test from Netbeans"); 
     message.setText("PFA"); 

     MimeBodyPart messageBodyPart = new MimeBodyPart(); 

     Multipart multipart = new MimeMultipart(); 

     messageBodyPart = new MimeBodyPart(); 

     //String filetosend = ("c:\\file.txt"); 

     DataSource source = new FileDataSource(filetosend); 
     System.out.println("The filetosend is ="+filetosend); 

     messageBodyPart.setDataHandler(new DataHandler(source)); 
     System.out.println("The source is ="+source); 

     messageBodyPart.attachFile(filetosend); 
     System.out.println("The file name is ="+messageBodyPart.getFileName()); 

     multipart.addBodyPart(messageBodyPart); 
     System.out.println("The message body part is ="+messageBodyPart); 

     message.setContent(multipart); 
     System.out.println("The message multi part is ="+multipart); 


     System.out.println("Sending"); 

     Transport.send(message); 
     System.out.println("The message is ="+message); 

     System.out.println("Done"); 

    } catch (MessagingException e) { 
     e.printStackTrace(); 
    } 
    } 
} 

而第二类:

import java.io.File; 
import java.io.IOException; 

public class Test { 

    public static void main(String[] args) throws Exception { 

    } 
    File file; 
    public void Test() throws IOException, Exception{ 
     System.out.println("Sending the file..."); 
     File filetosend = new File("c:\\file.txt"); 
     SendFile.sendEmail(filetosend); 
    } 
} 

没有错误,但没有发送的文件。 请任何帮助,谢谢

+0

我发现了一个类似的问题http://stackoverflow.com/questions/16117365/sending-mail-attachment-using-java – Jaydee 2014-09-03 14:48:06

+0

的问题不是要发送的文件。我想从另一个班级获得档案。 – 2014-09-03 15:27:19

回答

0

您的代码是错误的。如果你从某个地方复制它,原件也是错的,或者你错了。这是你想要什么:

Message message = new MimeMessage(session); 
    message.setFrom(new InternetAddress("[email protected]")); 
    message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse("[email protected]")); 
    message.setSubject("Attach file Test from Netbeans"); 

    MimeBodyPart messageBodyPart = new MimeBodyPart(); 
    messageBodyPart.setText("PFA"); 

    attachmentBodyPart = new MimeBodyPart(); 

    System.out.println("The filetosend is ="+filetosend); 
    System.out.println("The source is ="+source); 

    attachmentBodyPart.attachFile(filetosend); 
    System.out.println("The file name is ="+attachmentBodyPart.getFileName()); 

    Multipart multipart = new MimeMultipart(); 
    multipart.addBodyPart(messageBodyPart); 
    multipart.addBodyPart(attachmentBodyPart); 

    message.setContent(multipart); 
    System.out.println("The message multi part is ="+multipart); 

    System.out.println("Sending"); 

    Transport.send(message); 
+0

现在好了,谢谢 – 2014-09-03 19:25:00

0

你的代码看起来不错。实际上,我几乎使用完全相同的代码来发送带有附件的消息。您应该查看是否需要向Transport添加身份验证,并使用主机,端口,身份验证ID和身份验证传递进行连接。另外,请检查是否有防火墙阻止带附件的消息(令人难以置信的常见问题)。

如果你看看这个帖子Stack Overflow post on sending multiple attachments

你会看到,几乎同样的事情做,它给出了如何与验证发送消息的例子。

+0

当第二个类中指定要发送的文件时它正在工作,但是我的问题是如何从第一个类中获取文件。 – 2014-09-03 15:03:30

+0

Whay你的意思是通过获得头等档案? – tmarwen 2014-09-03 15:38:59

+0

SendFile类用作工具类,Test类用于通过SendFile类的调用方法发送文件。 – 2014-09-03 15:53:41