2015-07-20 30 views
1

我正在使用SendMail,它允许我发送电子邮件。如何从另一个类(SendMail)运行main(String [] args)

如果我右键单击班级,并选择运行'SendMail()。主',然后该类编译并正确运行。它给我发了一封电子邮件。

如何运行的SendMail()。从另一个类主?

我已经试过什么:

  1. 添加活动形式的SendMail,并试图从另一个类的意图初始化。

代码:

startActivity(new Intent(anotherClass.this, SendMail.class)); 
  • 给anotherClass上运行的SendMail空隙。
  • anotherClass代码:

    public void loginAs() 
    { 
    SendMail class2 = new SendMail(); 
    class2.doSomething(); 
    } 
    

    的SendMail代码:

    public static void doSomething() { 
        SendMail.main(new String[] {"main"}); 
    } 
    

    似乎没有任何工作。我的SendMail类发出

    "Oops something has gone pearshaped!" 
    

    07-20 16:32:29.507 11953-11953/blabla.bla I/System.out﹕ android.os.NetworkOnMainThreadException 
    

    SendMail.java

    import... 
    
    public class SendMail extends Object { 
    
    public static void doSomething(String sUser, String sPass) { 
    SendMail.main(new String[] {"main"}) 
    } 
    
    public static void main(String [] args) 
    { 
        for (String s : args) 
         System.out.println(s); 
        try{ 
         Properties props = new Properties(); 
         props.put("mail.smtp.host", "smtp.gmail.com"); // for gmail use smtp.gmail.com 
         props.put("mail.smtp.auth", "true"); 
         props.put("mail.debug", "true"); 
         props.put("mail.smtp.starttls.enable", "true"); 
         props.put("mail.smtp.port", "465"); 
         props.put("mail.smtp.socketFactory.port", "465"); 
         props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
         props.put("mail.smtp.socketFactory.fallback", "false"); 
         Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { 
          protected PasswordAuthentication getPasswordAuthentication() { 
           return new PasswordAuthentication("[email protected]", "foo"); } }); 
         mailSession.setDebug(true); // Enable the debug mode 
         Message msg = new MimeMessage(mailSession); 
         //--[ Set the FROM, TO, DATE and SUBJECT fields 
         msg.setFrom(new InternetAddress("[email protected] ")); 
         msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("[email protected]")); 
         msg.setSentDate(new Date()); 
         msg.setSubject("Hello World!"); 
         //--[ Create the body of the mail 
         msg.setText("Hello from my first e-mail sent with JavaMail"); 
         //--[ Ask the Transport class to send our mail message 
         Transport.send(msg); 
        }catch(Exception E){ 
         System.out.println("Oops something has gone pearshaped!"); 
         System.out.println(E); 
         return; 
        } 
    } 
    } 
    

    回答

    1

    你得到一个NetworkOnMainThreadException因为您正尝试在主UI线程中执行网络操作。

    在Android中,所有网络操作都必须在后台线程中执行。

    一种方式做到这一点是使用一个的AsyncTask只是一个doInBackground()方法,像这样:

    public class SendMail extends Object { 
    
        public void doSomething(String sUser, String sPass) { 
         new SendMailAsync().execute(sUser, sPass); 
    
        } 
    
        class SendMailAsync extends AsyncTask<String, Void, Void> { 
    
         @Override 
         protected Void doInBackground(String... params) { 
    
          String username = params[0]; 
          String password = params[1] 
    
          for (String s : args) 
           System.out.println(s); 
          try{ 
           Properties props = new Properties(); 
           props.put("mail.smtp.host", "smtp.gmail.com"); // for gmail use smtp.gmail.com 
           props.put("mail.smtp.auth", "true"); 
           props.put("mail.debug", "true"); 
           props.put("mail.smtp.starttls.enable", "true"); 
           props.put("mail.smtp.port", "465"); 
           props.put("mail.smtp.socketFactory.port", "465"); 
           props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
           props.put("mail.smtp.socketFactory.fallback", "false"); 
           Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() { 
            protected PasswordAuthentication getPasswordAuthentication() { 
             return new PasswordAuthentication("[email protected]", "foo"); } }); 
           mailSession.setDebug(true); // Enable the debug mode 
           Message msg = new MimeMessage(mailSession); 
           //--[ Set the FROM, TO, DATE and SUBJECT fields 
           msg.setFrom(new InternetAddress("[email protected] ")); 
           msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("[email protected]")); 
           msg.setSentDate(new Date()); 
           msg.setSubject("Hello World!"); 
           //--[ Create the body of the mail 
           msg.setText("Hello from my first e-mail sent with JavaMail"); 
           //--[ Ask the Transport class to send our mail message 
           Transport.send(msg); 
          }catch(Exception E){ 
           System.out.println("Oops something has gone pearshaped!"); 
           System.out.println(E); 
           return null; 
          } 
    
          return null; 
         } 
        } 
    
    } 
    

    当你需要调用它,实例化一个对象首先:

    SendMail sendMail = new SendMail(); 
    sendMail.doSomething(username, password); 
    
    +0

    辉煌!!谢谢你,先生 – Machina

    0
    String[] yourArgumentsForSendMailMain = new String[] {"argument"}; 
        SendMail.main(yourArgumentsForSendMailMain); 
    

    应该工作。如果没有,你能告诉我哪一行导致异常吗? (即:步骤通过与调试try块,直到找到问题行)

    0

    你为什么不从你的主要方法把整个代码,并把它变成一个非静态run方法?

    void run(String[] args) { 
        YOUR CODE HERE 
    } 
    

    ,当你在另一个类使用此方法类似

    SendMail sm = new SendMail(); 
    sm.run(new String[] {"xyz"}); 
    

    ,你甚至可以在其他类中使用非私有属性...

    相关问题