簡體   English   中英

Java電子郵件附件在正文中作為純文本發送

[英]java email attachment being sent as plain text in body

我在使用Java郵件(1.4.6)通過Java通過電子郵件發送附件時遇到問題。 看來,當我附加任何類型的文檔並將其發送時,收件人將在正文中獲取純文本格式的文件。 並非您猜到了,而是像您期望的那樣發送了整個文件,並且身體沒有受到干擾。

    try 
    {

        // Create a message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(Compose.to));
        message.setSubject(Compose.subject);
        //message.setText(Compose.body);
        //If there are no CC's then skip it. This if seemed to decrease send time.
        if(Compose.cc != null)
        {
            message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(Compose.cc));
            message.saveChanges();
        }
        else
            message.saveChanges();

        /*
         * For adding the attached file to the email. This time the if
         * statement is used to stop the email attachment process if there
         * is none. Other wise due to the way I've set it up it'll try to
         * send file path and file name as null, and we fail an otherwise valid email.
         */

        if(Compose.filename != null)
        {
            String file = Compose.filepath;
            String fileName = Compose.filename;

            Multipart multipart = new MimeMultipart();
            BodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);
            multipart.addBodyPart(messageBodyPart);

            BodyPart messageBodyPart2 = new MimeBodyPart();
            messageBodyPart2.setText(Compose.body);
            multipart.addBodyPart(messageBodyPart2);

            message.setContent(multipart);
        }
        else
        {
            message.setText(Compose.body);
            message.saveChanges();
        }

        //Send the message by javax.mail.Transport .            
        Transport tr = session.getTransport("smtp");            // Get Transport object from session        
        tr.connect(smtphost, username, password);               // We need to connect
        tr.sendMessage(message, message.getAllRecipients());    // Send message

        //Notify the user everything functioned fine.
        JOptionPane.showMessageDialog(null, "Your mail has been sent.");

    } 

考慮這一點,我記得FileDataSource()是一個重載的語句,將字符串或文件類型作為參數,嘗試兩者都得到相同的結果,但是現在我將對文件類型進行更多的實驗。

編輯:經過更多測試后,我注意到有時文件不會與發送時正文中的任何內容一起出現。

您將附件設置為主體的第一部分。 它必須是第二部分。

另外,請考慮升級到JavaMail 1.5.4,並使用MimeBodyPart.attachFile方法附加文件。

對於每個零件,您必須將主體的處置設置Part.INLINE ,並將附件的處置設置Part.ATTACHMENT attachFile方法將為您完成此操作。 避免使用JavaMail 1.4.6來支持最新版本,或者至少使用JavaMail 1.4.7,其中包含針對JavaMail 1.4.6的已知問題的修復程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM