JavaMail邮件和附件

问题描述 投票:0回答:2

我正在尝试使用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 protected]";
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 protected]"));
    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);
}
java email gmail javamail html-email
2个回答
0
投票

这是我工作的最近项目的工作代码。希望能帮助到你!

String from = "[email protected]";
    String pass = "***********";
    String[] to = { "[email protected]" }; // 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();
    }

0
投票

你正在调用message.setContent两次。第二个调用会覆盖第一个调用,替换内容。

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

© www.soinside.com 2019 - 2024. All rights reserved.