Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SpringJavaMailSender以附件或内联方式发送文件_Java_Spring_Spring Mvc_Jakarta Mail - Fatal编程技术网

使用SpringJavaMailSender以附件或内联方式发送文件

使用SpringJavaMailSender以附件或内联方式发送文件,java,spring,spring-mvc,jakarta-mail,Java,Spring,Spring Mvc,Jakarta Mail,我创建了一个简单的spring应用程序,它向用户发送电子邮件。这似乎与SimpleEmailMessage完美结合——整个消息都是纯文本。我想要的是将一些文件作为附件发送,所以我尝试使用javax.mail.internet.MimeMessage,但得到一些有线错误,我无法跟踪。下面是我的片段 try { javax.mail.internet.MimeMessage message = mailSender.createMimeMessage();

我创建了一个简单的spring应用程序,它向用户发送电子邮件。这似乎与SimpleEmailMessage完美结合——整个消息都是纯文本。我想要的是将一些文件作为附件发送,所以我尝试使用javax.mail.internet.MimeMessage,但得到一些有线错误,我无法跟踪。下面是我的片段

try {
            javax.mail.internet.MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setTo(employee.getEmailId());
            helper.setFrom("abc@xyz.com");
            helper.setText(mailMessage, true);
            helper.setSubject(subject);
            FileSystemResource res = new FileSystemResource(new File("C:/software/Koala.jpeg"));
            helper.addAttachment("happyBirthday", res);
            mailSender.send(message);
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
错误是:

Caused by: java.lang.LinkageError: loader constraint violation: when resolving method "javax.mail.internet.MimeBodyPart.setDataHandler(Ljavax/activation/DataHandler;)V" the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) of the current class, org/springframework/mail/javamail/MimeMessageHelper, and the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) for resolved class, javax/mail/internet/MimeBodyPart, have different Class objects for the type n/DataHandler;)V used in the signature
    at org.springframework.mail.javamail.MimeMessageHelper.addAttachment(MimeMessageHelper.java:997)
    at org.springframework.mail.javamail.MimeMessageHelper.addAttachment(MimeMessageHelper.java:1077)
    at org.springframework.mail.javamail.MimeMessageHelper.addAttachment(MimeMessageHelper.java:1047)
    at Example1.sendEmail(Example1.java:89)

任何帮助都将不胜感激。

除了

FileSystemResource res = new FileSystemResource(new File("C:/software/Koala.jpeg"));
您只需将文件附加到邮件即可

helper.addAttachment("happyBirthday", new File("C:/software/Koala.jpeg"));

一切看起来都很好,除了

FileSystemResource res = new FileSystemResource(new File("C:/software/Koala.jpeg"));
您只需将文件附加到邮件即可

helper.addAttachment("happyBirthday", new File("C:/software/Koala.jpeg"));

尝试使用Byte[]类型,您必须使用以下代码将文件转换为Byte[]:

Path path = Paths.get("path/to/file");
byte[] content = Files.readAllBytes(path);
并在准备发送邮件时调用此函数:

public void sendEmail(String to,String from,String sub,String msgBody,byte[] content){

    MimeMessage message = mailSender.createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(sub);
        helper.setText(msgBody);
        helper.addAttachment("MyTestFile.txt", new ByteArrayResource(content));
        mailSender.send(message);
    } catch (MessagingException e) {

        e.printStackTrace();
    }
}

尝试使用Byte[]类型,您必须使用以下代码将文件转换为Byte[]:

Path path = Paths.get("path/to/file");
byte[] content = Files.readAllBytes(path);
并在准备发送邮件时调用此函数:

public void sendEmail(String to,String from,String sub,String msgBody,byte[] content){

    MimeMessage message = mailSender.createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(sub);
        helper.setText(msgBody);
        helper.addAttachment("MyTestFile.txt", new ByteArrayResource(content));
        mailSender.send(message);
    } catch (MessagingException e) {

        e.printStackTrace();
    }
}

试试这个,这种让邮件附件的方式对我来说很有效

MimeMesage msg=javaMailSender.createMimeMessage();

msg.addRecipient(Message.RecipientType.TO,InternetAddress(email));
msg.setSubject("Subject");

Multipart emailContent=new MimeMultipart();
MimeBodyPart textBodyPart=new MimeBodyPart();
textBodyPart.setText("My multipart");

MimeBodyPart jpgBodyPart=new MimeBodyPart();
jpgBodyPart.attachFile("C:/software/Koala.jpeg");

emailContent.addBodyPart(textBodyPart);
emailContent.addBodyPart(jpgBodyPart);

msg.setContent(emailContent);
javaMailSender.send(msg);

试试这个,这种让邮件附件的方式对我来说很有效

MimeMesage msg=javaMailSender.createMimeMessage();

msg.addRecipient(Message.RecipientType.TO,InternetAddress(email));
msg.setSubject("Subject");

Multipart emailContent=new MimeMultipart();
MimeBodyPart textBodyPart=new MimeBodyPart();
textBodyPart.setText("My multipart");

MimeBodyPart jpgBodyPart=new MimeBodyPart();
jpgBodyPart.attachFile("C:/software/Koala.jpeg");

emailContent.addBodyPart(textBodyPart);
emailContent.addBodyPart(jpgBodyPart);

msg.setContent(emailContent);
javaMailSender.send(msg);


尽管如此,我还是得到了与上面描述的相同的错误。看起来问题可能是由两个版本的Java激活框架引起的。试着看看从类路径中删除activation.jar是否解决了这个问题。从jboss中删除了activation.jar,并且删除了所有在jboss的lib文件夹中编译的特定邮件jar,仍然看到相同的异常?是的。。仍然是同一个问题。我将得到如上所述的相同错误。看起来问题可能是由两个版本的Java激活框架引起的。试着看看从类路径中删除activation.jar是否解决了这个问题。从jboss中删除了activation.jar,并且删除了所有在jboss的lib文件夹中编译的特定邮件jar,仍然看到相同的异常?是的。。还是一样的问题我可以把jpg文件作为附件发送给你当然可以!在helper.addAttachmenti中将MyTestFile.txt更改为Koala.jpeg我已经尝试了你所说的,但仍然存在相同的问题你的libs中有activation.jar吗?我从jboss中删除了activation.jar。。。除此之外,我还将附件更改为inputStreamResource,但现在我正在传入包含开放流的资源:无效参数。JavaMail需要一个InputStreamSource,为每个调用创建一个新的流。我是否可以将jpg文件作为附件发送?当然可以!在helper.addAttachmenti中将MyTestFile.txt更改为Koala.jpeg我已经尝试了你所说的,但仍然存在相同的问题你的libs中有activation.jar吗?我从jboss中删除了activation.jar。。。除此之外,我还将附件更改为inputStreamResource,但现在我正在传入包含开放流的资源:无效参数。JavaMail需要一个InputStreamSource,为每个调用创建一个新的流。