简体   繁体   中英

Java Email with Commons Mail

I'm trying to send a email with commons api, but, i goting error!

This is an example of Commons guide, but, i cant send here..

public class Emailsss {

/**
 * @param args the command line arguments
 */
public static void main(String[] args)  throws EmailException, MalformedURLException {
    // TODO code application logic here
    // Create the email message
    HtmlEmail email = new HtmlEmail();
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);
    email.setAuthenticator(new DefaultAuthenticator("username", "mypw"));
    email.setSSLOnConnect(true);
    email.setFrom("username");
    email.setSubject("TestMail");
    email.setMsg("This is a test mail ... :-)");
    email.addTo("to");

      URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
      String cid = email.embed(url, "Apache logo");
      email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
      email.setTextMsg("Your email client does not support HTML messages");

      // send the email
      email.send();
    } 
  }

And here's my error:

   Exception in thread "main" java.lang.NoSuchMethodError:     
javax.mail.internet.MimeBodyPart.setText(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
at org.apache.commons.mail.HtmlEmail.build(HtmlEmail.java:581)
at org.apache.commons.mail.HtmlEmail.buildMimeMessage(HtmlEmail.java:519)
at org.apache.commons.mail.Email.send(Email.java:1436)
at emailsss.Emailsss.main(Emailsss.java:46)
 Java Result: 1

You likely need the javax mail package that can be found at (for example) http://repo1.maven.org/maven2/javax/mail/mail/1.4/mail-1.4.jar


If you are building with maven itself (you didn't say you are, but using a dependancy manager is a good thing and probably would have solved your issue...)

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
</dependency>

If you note the maven dependancies for org.apache.commons you will see:

This artifact depends on ...

  • javax.mail

And if you had used maven, the pom file for org.apache.commons has javax.mail as the first depenancy.

All that said, download that link for http://repo1.maven.org/maven2/javax/mail/mail/1.4/mail-1.4.jar include it in your class path and you should be good.


While on the subject, you may wish to read javax vs java package which answers the question of why javax is different and separate from the java packages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM