2017-08-03 70 views
0

我有一個程序,我可以發送電子郵件通過。但是,這需要成爲更大程序的一部分。電子郵件類是在不同的包下,而我的其他兩個類(驅動程序類/主程序,以及另一個對象類)都在默認包中。我可以訪問電子郵件類,儘管它在不同的包中,或者我是否需要將它們全部放在一個包中?我怎麼去做這些?目前,我嘗試刪除電子郵件類的主要方法部分,並將其與我的驅動程序類一起放入默認包中,這導致了很多語法錯誤。以下是一些照片,顯示我的課程和一些代碼。 SendMail類與SendMailTLS相同,只是主要方法被刪除並放入默認包中。 SendMailTLS類完美工作,我只需要能夠從IA類訪問它。我想將一個主要的方法變成一個新的非主要方法類

的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 = "[email protected]"; 
     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("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
      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類:

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 = "[email protected]"; 
     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("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
      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); 
     } 
    } 
} 

enter image description here

+0

只需將其移動到不同的包,然後再次導入新的包路徑。它看起來像你使用Eclipse,所以你應該能夠將鼠標懸停在'語法錯誤'下,然後單擊'import package.newclasspath;' – Smittey

+0

在進入包的概念之前,我認爲你應該給出一個看看[類,方法和實例變量]的概念(https://docs.oracle.com/javase/tutorial/java/javaOO/index.html) – bracco23

回答

0

你應該首先學習的基礎知識,然後用這樣的comlicated程序啓動。

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

     public static void start() { 

      final String username = "[email protected]"; 
      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("[email protected]")); 
       message.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse("[email protected]")); 
       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); 
      } 
     } 
    } 

然後,您可以從您的新主方法或您喜歡的任何其他方法運行該代碼。

public class YourMainClass { 

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

總之,如果你不想讓你的主要方法,當你開始要執行的程序,剛剛從main其名稱更改爲您所選擇的東西,刪除參數String[] args