【问题标题】:JavaMail mail and attachmentJavaMail 邮件和附件
【发布时间】:2019-07-20 17:15:48
【问题描述】:

我正在尝试使用 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);
}

【问题讨论】:

    标签: java email gmail jakarta-mail html-email


    【解决方案1】:

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

    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();
        }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 是的,我认为它正在覆盖它,谢谢比尔,设法修复它,#thumbsUp
      猜你喜欢
      • 2018-02-02
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 2012-02-02
      • 2019-05-28
      • 2014-01-06
      相关资源
      最近更新 更多