简体   繁体   English

需要帮助设置一个类来发送电子邮件,它从主类调用

[英]Need assistance setting up a class to send email, it gets called from the main class

I am trying to send an email from my application when a user logs in for the first time. 当用户第一次登录时,我正尝试从我的应用程序发送电子邮件。 The email consists of a activation code. 该电子邮件包含激活码。

Update: I downloaded the latest javamail API and also the latest JAF (JavaBeans Activation Framework) and it does not compile due to imports being unknown symbols. 更新:我下载了最新的javamail API以及最新的JAF(JavaBeans Activation Framework),由于导入是未知符号,因此无法编译。 How do I import from the javax package? 如何从javax包导入?

Here is the code that I got so far: 这是我到目前为止的代码:

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 = "admin@Nestroia.com";
        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 = "admin@Nestroia.com";
        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);

Here is the stacktrace: 这是堆栈跟踪:

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

I'm guessing you probably have just the javaee.jar in your classpath, which has the definitions of the javax.mail.* classes but not the actual implementation. 我猜你的类路径中可能只有javaee.jar,它有javax.mail。*类的定义,但不是实际的实现。

You should download the javamail api if you dont already have. 你应该下载javamail api,如果你还没有。 Found here 这里找到

You're running with a javaee.jar that does not contain implementations. 您正在使用不包含实现的javaee.jar。 It's a "stubbed" implementation that is just intended to enable compilation, not execution. 它是一个“存根”实现,只是为了实现编译,而不是执行。 This post provides more details. 这篇文章提供了更多细节。 (It's referring to Java EE 5, but the same holds true for 6.) (它指的是Java EE 5,但对于6.)也是如此。)

This post provides explanation and possible solution. 本文提供了解释和可能的解决方案。 You might need to adjust your class path. 您可能需要调整类路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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