2011-12-14 18 views
3

我想在用户第一次登录时从我的应用程序发送电子邮件。该电子邮件由激活码组成。需要协助建立一个类来发送电子邮件,它从主类中调用

更新:我下载了最新的javamail API以及最新的JAF(JavaBeans Activation Framework),并且由于导入是未知符号而不能编译。如何从javax包导入?

这里是我到目前为止的代码:

import java.util.ArrayList; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class Email { 
    static String smtpHost; 
    static int smtpPort; 
    static String from; 
    static String to; 
    static String subject; 
    static String content; 

    public Email(String toEmail, String code){ 
     smtpHost = "localhost"; 
     smtpPort = 25; 
     from = "[email protected]"; 
     to = toEmail; 
     subject = "Hello from Nestroia.com"; 
     content = "This is your activation code: " + code; 
    } 

    public Email(String toEmail, String name, String password){ 
     smtpHost = "localhost"; 
     smtpPort = 25; 
     from = "[email protected]"; 
     to = toEmail; 
     subject = "Hello from Nestroia.com"; 
     content = "This is your Login information.\n" 
       + "Name: " + name + "\n" 
       + "Password: " + password;   
    } 

    public static void send() throws AddressException, MessagingException { 

     // Create a mail session 
     java.util.Properties props = new java.util.Properties(); 
     props.put("mail.smtp.host", smtpHost); 
     props.put("mail.smtp.port", ""+smtpPort); 
     Session session = Session.getDefaultInstance(props, null); 

     // Construct the message 
     Message msg = new MimeMessage(session); 
     msg.setFrom(new InternetAddress(from)); 
     msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
     msg.setSubject(subject); 
     msg.setText(content); 

     // Send the message 
     Transport.send(msg); 

这里是堆栈跟踪:

java.lang.RuntimeException: java.lang.ClassFormatError: Absent Code attribute in  method that is not native or abstract in class file javax/mail/Address 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source) 
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source) 
at com.sun.glass.ui.View.notifyMouse(Unknown Source) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source) 
at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source) 
at java.lang.Thread.run(Thread.java:722) 
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/Address 
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 java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
at Nestroia.Nestroia_Main$6$1.handle(Nestroia_Main.java:669) 
at Nestroia.Nestroia_Main$6$1.handle(Nestroia_Main.java:559) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
at javafx.event.Event.fireEvent(Unknown Source) 
at javafx.scene.Node.fireEvent(Unknown Source) 
at javafx.scene.control.Button.fire(Unknown Source) 
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source) 
at com.sun.javafx.scene.control.skin.SkinBase$5.handle(Unknown Source) 
at com.sun.javafx.scene.control.skin.SkinBase$5.handle(Unknown Source) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
at javafx.event.Event.fireEvent(Unknown Source) 
at javafx.scene.Scene$MouseHandler.process(Unknown Source) 
at javafx.scene.Scene$MouseHandler.process(Unknown Source) 
at javafx.scene.Scene$MouseHandler.access$1300(Unknown Source) 
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source) 
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source) 
... 7 more 
+1

这里有什么问题?你是否遇到异常? – 2011-12-14 03:09:45

+0

即时获得例外。它不允许我发送电子邮件,这是我发布的原因。可以马上发现的代码有问题吗? – 2011-12-14 03:11:14

+3

然后粘贴例外...它会给出一个清晰的图片.. – 2011-12-14 03:11:49

回答

3

我猜你可能只是javaee.jar在类路径中,其中具有javax.mail。*类的定义,但不是实际的实现。

如果你没有,你应该下载javamail api。找到here

2

您正在运行一个不包含实现的javaee.jar。这是一个“桩”实现,只是为了编译而不是执行。这post提供了更多的细节。 (它指的是Java EE 5,但对于6也是如此)。

0

This post提供了解释和可能的解决方案。你可能需要调整你的班级路径。

相关问题