Java Mail 方法尚未实现 mimemessage

标签 java jakarta-mail

收到错误“方法尚未实现 mimemessage”

尝试发送电子邮件时

   protected static void addAtachments(String[] attachments, Multipart multipart) throws MessagingException, MessagingException {

    for (int i = 0; i <= attachments.length - 1; i++) {
    String filename = attachments[i];
    MimeBodyPart attachmentBodyPart = new MimeBodyPart();

    DataSource source = new FileDataSource(filename);
    attachmentBodyPart.setDataHandler(new DataHandler(source));

    attachmentBodyPart.setFileName(filename);
    multipart.addBodyPart(attachmentBodyPart);
}}



  protected static void sendMessage(List<String> recipients, String subject,
                              String messageContent, String from, String[] attachments)
    throws MessagingException, MessagingException {
boolean debug = false;

Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props,
        new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xxxxx", "xxxxxx");
            }
        });

session.setDebug(debug);

Message message = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
message.setFrom(addressFrom);

for (String recipient : recipients) {
    message.addRecipient(Message.RecipientType.TO,
            new InternetAddress(recipient));
}

message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(messageContent, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
addAtachments(attachments, multipart);

message.setContent(multipart);
Transport.send(message);}

异常

Exception in thread "main" java.lang.UnsupportedOperationException: Method not yet implemented
at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:89)
at uk.co.newsint.bp.reg.report.Main.sendMessage(Main.java:84)
at uk.co.newsint.bp.reg.report.Main.main(Main.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

最佳答案

请确保java邮件依赖项如下 1、

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>`
</dependency>

因为还有另一个jar包含javax.mail.* 包裹在下面 2、

<dependency>
    <groupId>geronimo-spec</groupId>
    <artifactId>geronimo-spec-j2ee</artifactId>
    <version>1.4-rc4</version>`
</dependency>

解决方案是将第二个依赖项替换为第一个依赖项

关于Java Mail 方法尚未实现 mimemessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15063984/

相关文章:

java - 通过引用 bean 名称在 @Scheduled 注释中使用 @ConfigurationProperties

java - Hibernate - 如何验证是否真的执行了批量插入

Java SimpleDateFormat 在不同操作系统上对 'z' 的解释不同

javax.mail 附加的 PDF 文件已损坏

java - 在 Java 中连接两个整数以获得 float

java - 为 Spring MVC 和 Maven WebApplication 创建文件/图像文件夹目录

java - 如何以编程方式将照片上传到图像托管服务器?

java javaMail NoClassDefFoundError on Message msg = new MimeMessage(session)

java - 客户端未通过身份验证,无法在 MAIL FROM 期间发送匿名邮件

java - Spring JavaMailSender : recipient set as "default"