JavaMail邮件和附件

时间:2019-02-27 03:48:45

标签: java email gmail javamail html-email

我正在尝试使用JavaMail发送带有html文本和附件的电子邮件。 但是,我似乎只能一次完成一项工作,要么发送html文本,要么发送附件,但不能两者都行,我不知道如何使两者都行。

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");

final String username = "email@gmail.com";
final String password = "**********************";

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@gmail.com"));
    message.setRecipients(Message.RecipientType.TO,
        InternetAddress.parse("email to send"));
    message.setSubject("Testing Subject");

    MimeBodyPart messageBodyPart = new MimeBodyPart();
    Multipart multipart = new MimeMultipart();
    messageBodyPart = new MimeBodyPart();

    // Attachment
    String file = "/Users/user/Desktop/file.rtf";
    String fileName = "file.rtf";
    DataSource source = new FileDataSource(file);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(fileName);
    multipart.addBodyPart(messageBodyPart);

   message.setContent("<h1>HTML Text</h1>",
                      "text/html"); 
                      //HTML Text

   message.setContent(multipart); //attachment

    //Send
    System.out.println("Sending");
    Transport.send(message);
    System.out.println("Done");

} catch (MessagingException e) 
{
   e.printStackTrace();
   throw new RuntimeException(e);
}

2 个答案:

答案 0 :(得分:0)

这是我最近从事的项目的工作代码。希望对您有帮助!

String from = "JohnDoe@gmail.com";
    String pass = "***********";
    String[] to = { "JohnDoe@gmail.com" }; // list of recipient email addresses
    String subject = "Subject Line";

    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }

       BodyPart messageBodyPart = new MimeBodyPart();

     // Now set the actual message
     messageBodyPart.setText("");

     // Create a multipar message
     Multipart multipart = new MimeMultipart();

     // Set text message part
     multipart.addBodyPart(messageBodyPart);

     // Part two is attachment
     messageBodyPart = new MimeBodyPart();
     String filename = "send.jpeg";
     DataSource source = new FileDataSource(filename);
     messageBodyPart.setDataHandler(new DataHandler(source));
     messageBodyPart.setFileName(filename);
     multipart.addBodyPart(messageBodyPart);
     message.setContent(multipart);

     message.setSubject(subject);
     Transport transport = session.getTransport("smtp");
     transport.connect(host, from, pass);
     transport.sendMessage(message, message.getAllRecipients());
     transport.close();
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }

答案 1 :(得分:0)

您两次调用message.setContent。第二个呼叫将覆盖第一个呼叫,替换内容。

有关完整的工作示例,请参见JavaMail示例程序sendfile.java