i using sendmail allows me send email message.
if right click class , choose run 'sendmail().main', class compiles , correctly runs. sends me email.
how can run sendmail().main class?
what i've tried:
- adding activity form sendmail, , tried initializing intent class.
code:
startactivity(new intent(anotherclass.this, sendmail.class)); - tell anotherclass run void on sendmail.
anotherclass code:
public void loginas() { sendmail class2 = new sendmail(); class2.dosomething(); } sendmail code:
public static void dosomething() { sendmail.main(new string[] {"main"}); } nothing seems work. sendmail class sends out
"oops has gone pearshaped!" and
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) { (string s : args) system.out.println(s); try{ properties props = new properties(); props.put("mail.smtp.host", "smtp.gmail.com"); // 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 debug mode message msg = new mimemessage( mailsession ); //--[ set from, to, date , 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 body of mail msg.settext( "hello first e-mail sent javamail" ); //--[ ask transport class send our mail message transport.send( msg ); }catch(exception e){ system.out.println( "oops has gone pearshaped!"); system.out.println( e ); return; } } }
you're getting networkonmainthreadexception because you're attempting network operation in main ui thread.
in android network operations must executed in background thread.
one way use asynctask doinbackground() method, this:
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] (string s : args) system.out.println(s); try{ properties props = new properties(); props.put("mail.smtp.host", "smtp.gmail.com"); // 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 debug mode message msg = new mimemessage( mailsession ); //--[ set from, to, date , 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 body of mail msg.settext( "hello first e-mail sent javamail" ); //--[ ask transport class send our mail message transport.send( msg ); }catch(exception e){ system.out.println( "oops has gone pearshaped!"); system.out.println( e ); return null; } return null; } } } when need call it, instantiate object first:
sendmail sendmail = new sendmail(); sendmail.dosomething(username, password);
Comments
Post a Comment