0
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSender {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        final String username = "xxx.xxxx@xxx.com";
        final String password = "xxxo@xxxx";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "webmxzz.xxxxxxx.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("xxxx.x@xxxx.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("xxxx.x@xxxx.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear User,"
                + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

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

在编写代码以通过网络发送电子邮件时,我发现了此代码。我使用的 jar 是javaee-api-5.jaractivation.jarmail.jar

我编译代码并在 eclipse 中运行它。它工作正常并将邮件发送到所需的邮件 ID,但是当我尝试通过导出 jar 文件来制作 jar 文件时出现问题,我收到警告:Jar 已导出。以警告结束。

我得到的错误是:

Could not find the main class: (class). Program will exit.

可能的原因是什么?请帮助我找到解决方案。

4

1 回答 1

0

是的,应该创建一个清单文件。此文件包含带有“主类”的单行文本文件,例如:主类:您的主类

或者您可以使用 eclipse 来执行此操作:Java: export to an .jar file in eclipse

于 2012-04-19T09:35:44.130 回答