5

Getting exception while sending mail through Java

exception is::

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)

Here is the Java code::

    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);
    }

I am getting error at Transport.send(msg); Jars i am using here:: smtp 1.6.0, Javax Mail 1.4.7 , mailactivation

2
  • 1
    Do you set the mime type? Commented Jan 16, 2018 at 8:16
  • Apparently the order of the parts matters according to the multipart MIME spec, as per this question: stackoverflow.com/questions/14744197/… Commented Jan 17, 2018 at 3:39

3 Answers 3

10

Most likely you have multiple versions of the JavaMail classes on your class path. Check all your dependencies and your application server environment for conflicts. If more than one jar file contains the javax.mail.* or com.sun.mail.* classes, you have a problem.

And be sure to fix all these common JavaMail mistakes, and definitely be sure to use the latest version of JavaMail.

Sign up to request clarification or add additional context in comments.

Comments

2

I fix the issue: java.lang.NoSuchMethodError: 'void com.sun.mail.util.LineOutputStream.(java.io.OutputStream, boolean)'

Added in my pom.xml the dependencies com.sun.mail with same version of the javax.mail-api

<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>

Comments

1

check your jar's version,try to change some versions,if it is a 'maven project',check the enviroment dependencies.as you know,java's enviroment usually follows with errores,you may be careful.

1 Comment

I have tried with javax mail 1.4.7 , 1.5.1, 1.6.0, but still i am getting the error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.