我正在使用SendMail,它允许我发送电子邮件。如何从另一个类(SendMail)运行main(String [] args)
如果我右键单击班级,并选择运行'SendMail()。主',然后该类编译并正确运行。它给我发了一封电子邮件。
如何运行的SendMail()。从另一个类主?
我已经试过什么:
- 添加活动形式的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;
}
}
}
辉煌!!谢谢你,先生 – Machina