2012-07-19 2 views
1

다음 코드를 사용하여 javamail과 함께 gmail에서 이메일을 보내고 있습니다.javamail이 작동하지 않습니다. MessagingException이 발생합니다.

import java.util.Properties; 
import javax.mail.*; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class JavaApplication1 { 
    public static void main(String[] args) { 
     Properties props = new Properties(); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", 
       "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 

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

     try { 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse("[email protected]")); 
      message.setSubject("Testing Subject"); 
      message.setText("Dear Mail Crawler," + 
        "\n\n No spam to my email, please!"); 

      Transport.send(message); 

      System.out.println("Done"); 

     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 

그러나 나는 다음과 같은 오류 스택 트레이스 점점 오전 :

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) 
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    at java.lang.Class.getDeclaredMethods0(Native Method) 
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2442) 
    at java.lang.Class.getMethod0(Class.java:2685) 
    at java.lang.Class.getMethod(Class.java:1620) 
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:484) 
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476) 
Exception in thread "main" Java Result: 1 

내가 MessagingException의가 무엇을하고 있는지지고 있지 않다과 이유가 발생했습니다. 어떤 몸이라도이 예외를 없앨 수 있습니까?

답변

0

아마도 아무것도 포함하지 않은 잘못된 javaee.jar 버전을 사용하고있을 것입니다.

M.

+0

그러나 빌드가 성공적입니다. 내 최신 netbeans 7.1.2 편집기에서 빨간색 전구로 모든 오류를 보여줍니다. 하지만 실행 후 stacktrace 오류가 발생합니다. 실제로 Netbeans을 사용하고 "Java ee 6 api"라이브러리를 가져오고 필요한 모든 가져 오기 클래스를 제공합니다. 그러나 오류가 있습니다. 'java ee 6 api'가 잘못 사용 된 것입니까? – codeofnode

+0

문제는 jar에 메서드 시그니처가 포함되어 있지만 코드는 포함되어 있지 않습니다. public void method (Object o) {/ * 여기에는 * /}가 없습니다. 그래서 컴파일되지만 코드가 없기 때문에 코드를 실행할 수 없습니다. 함께 시도이 하나 :이 병을 시도했지만 공식 사이트에서 마침내 아래있는 javamail 작업 항아리를 추가하지 않습니다 http://download.java.net/maven/2/javax/javaee-api/6.0/ – poussma

+0

클래스 패스에, 여기에 링크 http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.7-oth-JPR입니다 – user674158

1

http://www.mkyong.com/hibernate/java-lang-classformaterror-absent-code-attribute-in-method-that-is-not-native-or-abstract-in-class-file/ 나는 같은면 ClassNotFound 문제가 있었다 :

이 게시물을 보라. 나는

<dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-api</artifactId> 
     <version>6.0</version> 
     <type>jar</type> 
     <scope>provided</scope>    
    </dependency> 

    <dependency> 
     <groupId>org.glassfish.main.extras</groupId> 
     <artifactId>glassfish-embedded-all</artifactId> 
     <version>3.1.2</version> 
    </dependency> 

내가이 버전을 사용하고 있습니다 POM은 설정 다음으로 해결 한 글래스 피쉬 - 내장 내 모든 EJB 유닛 테스트에서, 나의 이해는이 항아리는 자바 EE API

에 필요한 모든 클래스를 포함하므로
관련 문제