我正在使用SendMail,它允许我发送电子邮件。
如果我右键单击该课程并选择运行' SendMail()。main' ,则该课程将编译并正确运行。它给我发了一封电子邮件。
如何从另一个类运行SendMail()。main?
我尝试过的事情:
代码:
startActivity(new Intent(anotherClass.this, SendMail.class));
anotherClass Code:
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("foo@gmail.com", "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( "foo@gmail.com " ) );
msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse("1111111111@messaging.sprintpcs.com") );
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;
}
}
}
答案 0 :(得分:1)
您正在获取NetworkOnMainThreadException,因为您正尝试在主UI线程中执行网络操作。
在Android中,所有网络操作都必须在后台线程中执行。
一种方法是使用只有doInBackground()
方法的AsyncTask,如下所示:
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("foo@gmail.com", "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( "foo@gmail.com " ) );
msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse("1111111111@messaging.sprintpcs.com") );
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);
答案 1 :(得分:0)
String[] yourArgumentsForSendMailMain = new String[] {"argument"};
SendMail.main(yourArgumentsForSendMailMain);
应该有效。如果没有,你能告诉我导致异常的是什么行吗? (即:使用调试器逐步执行try块,直到找到问题行)
答案 2 :(得分:0)
为什么不从主方法中获取整个代码并将其放入非静态运行方法中?
void run(String[] args) {
YOUR CODE HERE
}
当你在另一个类中使用此方法时,如
SendMail sm = new SendMail();
sm.run(new String[] {"xyz"});
你甚至可以在另一个类中使用非私人属性......