简体   繁体   English

我想将主方法转换为新的非主方法类

[英]I want to turn a main method into a new non-main method class

I have a program that I can send emails through. 我有一个可以通过其发送电子邮件的程序。 However, this needs to be a part of a much bigger program. 但是,这需要成为更大的程序的一部分。 The Email class is under a different package whereas my other 2 classes (The driver class/main program, as well as another object class) are both in the default package. Email类位于不同的包中,而我的其他2个类(驱动程序类/主程序以及另一个对象类)都位于默认包中。 Can I access the email class despite it being in a different package or do I need them all to be in one package? 我可以将电子邮件课程放在一个不同的程序包中,但仍可以访问该电子邮件课程吗? And how do I go about doing either of these? 而我该如何去做这两个呢? Currently, I tried removing the main method part of the email class and putting it in the default package with my driver class, this resulted in many syntax errors. 目前,我尝试删除email类的main方法部分,并将其与驱动程序类一起放入默认包中,这导致了许多语法错误。 Below are some photos showing my classes and some code. 以下是显示我的课程和代码的一些照片。 The SendMail class is the same as SendMailTLS just with the main method being removed and put into the default package. SendMail类与SendMailTLS相同,只是删除了main方法并将其放入默认包中。 The SendMailTLS class works perfectly, I just need to be able to access it from the IA class. SendMailTLS类可以完美地工作,我只需要能够从IA类访问它。

SendMail Class: SendMail类别:

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 SendMail {


        final String username = "treybyroncollier@gmail.com";
        final String password = "13october";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.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("treybyroncollier@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("treycollier@live.co.uk"));
            message.setSubject("THIS EMAIL IS A TEST");
            message.setText("Hello Trey, just to let you know that this email is a test and everything is working with Java.");

            Transport.send(message);

            System.out.println("Done");

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

SendMailTLS Class: SendMailTLS类别:

package com.mkyong.common;

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 SendMailTLS {

    public static void main(String[] args) {

        final String username = "treybyroncollier@gmail.com";
        final String password = "13october";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.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("treybyroncollier@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("treycollier@live.co.uk"));
            message.setSubject("THIS EMAIL IS A TEST");
            message.setText("Hello Trey, just to let you know that this email is a test and everything is working with Java.");

            Transport.send(message);

            System.out.println("Done");

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

在此处输入图片说明

You should learn the basics first and then start with such a comlicated program. 您应该首先学习基础知识,然后再从这种复杂的程序开始。

In your sendMail class, you added all of your code directly into the class body, that's not going to work. 在sendMail类中,您将所有代码直接添加到了类主体中,这是行不通的。 Instead, create a method in that class and paste your code in there. 而是在该类中创建一个方法,然后将代码粘贴到该类中。

Then you can call that method from your other class after you imported the package. 然后,您可以在导入包后从其他类中调用该方法。

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 SendMail {

        public static void start() {

            final String username = "treybyroncollier@gmail.com";
            final String password = "13october";

            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.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("treybyroncollier@gmail.com"));
                message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("treycollier@live.co.uk"));
                message.setSubject("THIS EMAIL IS A TEST");
                message.setText("Hello Trey, just to let you know that this email is a test and everything is working with Java.");

                Transport.send(message);

                System.out.println("Done");

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

You can then run that code from your new main method or any other method you like. 然后,您可以从新的main方法或您喜欢的任何其他方法运行该代码。

public class YourMainClass {

    public static void main(String[] args) {
        SendMail.start();
    }
}

In short, if you don't want your main method to execute when you start the program, just change its name from main to something of your choice and remove the parameter String[] args . 简而言之,如果您不希望在启动程序时执行main方法,只需将其名称从main更改为您选择的名称,然后删除参数String[] args

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

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