通过Java发送邮件时获取异常

时间:2018-01-16 08:14:15

标签: java email javamail

通过Java发送邮件时获取异常

例外是::

Exception in thread "main" java.lang.NoSuchMethodError: 
com.sun.mail.util.LineOutputStream.<init>(Ljava/io/OutputStream;Z)V
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1648)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1906)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1134)
at javax.mail.Transport.send0(Transport.java:255)
at javax.mail.Transport.send(Transport.java:124)
at com.yodlee.SendMail.sendMail(SendMail.java:78)
at com.yodlee.SendMail.main(SendMail.java:32)

这是Java代码::

    Properties props = new Properties();
    props.put(SMTP_HOST_KEY,SMTP_HOST);

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(".....",
                    "#.......");
        }
    });

    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(RECIPIENT_FROM));
        message.setSubject(SUBJECT);
        message.setSentDate(new Date());
        //message.setText("Test mails111");


        MimeBodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setContent(data, "text/html");
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);
        Transport.send(message);
        System.out.println("Done");
    } catch (MessagingException e) {
        status = -1;
        throw new RuntimeException(e);
    }

我在Transport.send(msg)收到错误; 我在这里使用的罐子:: smtp 1.6.0,Javax Mail 1.4.7,mailactivation

3 个答案:

答案 0 :(得分:4)

您很可能在类路径上有多个版本的JavaMail类。检查所有依赖项和应用程序服务器环境是否存在冲突。如果多个jar文件包含javax.mail。*或com.sun.mail。*类,则表示存在问题。

请务必修复所有这些common JavaMail mistakes,并确保使用latest version of JavaMail

答案 1 :(得分:1)

检查您的jar版本,尝试更改某些版本,如果它是一个&#39; maven项目&#39;,请检查环境依赖项。如果您知道,java的环境通常会随之而来错误,你可能会小心。

答案 2 :(得分:1)

我解决了这个问题: java.lang.NoSuchMethodError: 'void com.sun.mail.util.LineOutputStream.(java.io.OutputStream, boolean)'

在我的 pom.xml 中添加了与 javax.mail-api 相同版本的依赖项 com.sun.mail

<dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.6.2</version>
    </dependency>

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>
    </dependency>