Нужна помощь в настройке класса для отправки электронной почты, он вызывается из основного класса - PullRequest
3 голосов
/ 14 декабря 2011

Я пытаюсь отправить электронное письмо из своего приложения при первом входе пользователя в систему.Письмо состоит из кода активации.

Обновление: я загрузил последнюю версию API javamail, а также последнюю версию 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 = "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);

Вот трассировка стека:

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

Ответы [ 3 ]

3 голосов
/ 14 декабря 2011

Я предполагаю, что у вас, вероятно, есть только javaee.jar в вашем classpath, который содержит определения классов javax.mail. *, Но не фактическую реализацию.

Вам следует скачать javamail api, еслиВы еще не сделали.Найдено здесь

2 голосов
/ 14 декабря 2011

Вы работаете с javaee.jar, который не содержит реализаций.Это «заглушенная» реализация, которая предназначена только для включения компиляции, а не выполнения.Это сообщение предоставляет более подробную информацию.(Это относится к Java EE 5, но то же самое относится и к 6.)

0 голосов
/ 13 января 2012

Этот пост содержит объяснение и возможное решение.Возможно, вам придется скорректировать свой путь к классу.

...