(Unknown Sender) when sending email with pdf attac

2019-06-07 11:48发布

问题:

I have a link on a webpage that sends emails out from localhost SMTP with pdf attachments created from iText that I made following How to create an in-memory PDF report and send as an email attachment using iText and Java and it is working, however when I get the emails I can't see the sender name/address.

It shows as being from "(unknown sender)" in my gmail inbox and if I hit reply the 'to' box is completely blank. In hotmail is shows as being from "(Unknown)" but when i open it, the sender shows as "admin@whatever.com on behalf of (Unknown)"

When I test the SMTP on the server through telnet, the sender name I make up comes through just fine. How can I get it to show up when the application sends the email?

Here is the code that sends the email:

    String smtpHost = "localhost";
    int smtpPort = 25;
    String sender = "admin@whatever.com";
    String[] recipient = pdfEmail.replaceAll("\\ ", "").split(",");
    String content = "whatever"; 
    String subject = "whatever"; 

    Properties props = new Properties();
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.user", sender);
    props.put("mail.smtp.port", smtpPort);

    Session session = Session.getDefaultInstance(props, null);

    ByteArrayOutputStream outputStream = null;

    try {          
        //construct the text body part
        MimeBodyPart textBodyPart = new MimeBodyPart();
        textBodyPart.setText(content);

        //now write the PDF content to the output stream
        outputStream = new ByteArrayOutputStream();
        writePdf(outputStream); //creates PDF
        byte[] bytes = outputStream.toByteArray();

        //construct the pdf body part
        DataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf");
        MimeBodyPart pdfBodyPart = new MimeBodyPart();
        pdfBodyPart.setDataHandler(new DataHandler(dataSource));
        pdfBodyPart.setFileName("test.pdf");

        //construct the mime multi part
        MimeMultipart mimeMultipart = new MimeMultipart();
        mimeMultipart.addBodyPart(textBodyPart);
        mimeMultipart.addBodyPart(pdfBodyPart);

        //create the sender/recipient addresses
        InternetAddress iaSender = new InternetAddress(sender);     
        InternetAddress[] toAddress = new InternetAddress[recipient.length];

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

        //construct the mime message
        MimeMessage mimeMessage = new MimeMessage(session);

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

        mimeMessage.setSender(iaSender);
        mimeMessage.setSubject(subject);
        mimeMessage.setRecipients(Message.RecipientType.TO, toAddress);
        mimeMessage.setContent(mimeMultipart);

        //send off the email         
        Transport.send(mimeMessage);

    } catch(Exception ex) {
        ex.printStackTrace();
    } finally {
        //clean off
        if(null != outputStream) {
            try { outputStream.close(); outputStream = null; }
            catch(Exception ex) { }
        }

    }
}

回答1:

Try to use mimeMessage.setFrom(...) instead of mimeMessage.setSender(...).