在java中以附件形式发送文件

在java中以附件形式发送文件,java,file,jakarta-mail,Java,File,Jakarta Mail,我有一个java代码,我必须发送一个附件,它可能是.doc、.db或.file。因此,我使用以下代码,消息成功传递,特定的附件文件未发送也未接收 我的代码是: import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.

我有一个java代码,我必须发送一个附件,它可能是.doc、.db或.file。因此,我使用以下代码,消息成功传递,特定的附件文件未发送也未接收

我的代码是:

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendEmail {

    private String from = "example1@gmail.com";
    private String to;
    private String subject;
    private String text;
    String filename = "hardWare_Dtls.file";
    String host = "smtp.gmail.com";

    public SendEmail(String from, String to, String subject, String text,String filename) {
        // System.out.println("From Adress inside constr"+from);
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;
        this.filename=filename;
    }

    public void send() {

        Properties props = System.getProperties();
        System.out.println("Email Options SendEmail");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", "123456");
        props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo
                                            // mail
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session mailSession = Session.getDefaultInstance(props, null);
        Message simpleMessage = new MimeMessage(mailSession);

        InternetAddress fromAddress = null;
        InternetAddress toAddress = null;
        try {
            fromAddress = new InternetAddress(from);
            toAddress = new InternetAddress(to);

        } catch (AddressException e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            System.out.println("Address Exception" + e.getMessage());
        }

        try {
            System.out.println("From Address" + fromAddress);
            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipient(RecipientType.TO, toAddress);
            System.out.println("To Address" + toAddress);
            simpleMessage.setSubject(subject);
            simpleMessage.setText(text);
            simpleMessage.setSentDate(new Date());

             MimeBodyPart attachmentPart = new MimeBodyPart();
                FileDataSource fileDataSource = new FileDataSource(filename) {
                    @Override
                    public String getContentType() {
                        return "application/octet-stream";
                    }
                };
                attachmentPart.setDataHandler(new DataHandler(fileDataSource));
                attachmentPart.setFileName(filename);

                Multipart multipart = new MimeMultipart();
              //  multipart.addBodyPart(messagePart);
                multipart.addBodyPart(attachmentPart);


            // simpleMessage.setText(attachment);

            Transport transport = mailSession.getTransport("smtps");
            transport.connect("smtp.gmail.com", "example1@gmail.com",
                    "123456");
            simpleMessage.saveChanges();
            transport.sendMessage(simpleMessage,
                    simpleMessage.getAllRecipients());
             Transport.send(simpleMessage);

            transport.close();
            // Transport.send(simpleMessage);
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            System.out.println("Messagine Exception" + e.getMessage());
        }
    }

}
在这段代码中我犯了什么错误。如果使用Javacode发送带有附件的消息的任何其他代码,请发送给我。提前谢谢

我在我的主类中使用此代码:

            String from = "example@gmail.com";
        String to = "example@yahoo.com";
        String subject = "Sample Text Message";
        String message = "Sample Msg with File attachment";
        String filename="hardWare_Dtls.file";

        SendEmail sendMail = new SendEmail(from, to, subject, message,filename);
        sendMail.send();

您拥有几乎所有的片段,但是您应该使用(而不仅仅是消息),并将其内容设置为您的多部分。我认为您确实尝试过这样做,但您的代码无法编译,因此您注释掉了这一行:

 // simpleMessage.setText(attachment);
而不是这样做:

((MimeMessage) simpleMessage).setContent(multipart);

然后再次测试。它应该可以工作。

您是否查看了收到的原始消息,看看是什么导致了它。您将多部分请求发送到哪里?您只发送了一条简单的消息。@BenDennison:请查看我作为答案发布的链接。“Messagine异常”。。我想如果找不到源代码。@BenDennison-您确定可以在指定的位置从程序中读取您的文件吗?路径可能是个问题(我没有看到,只是一个基本文件名)。