java - SMTP mail code is working fine in Android ICS but not in Gingerbread? -
i have written code send mail through smtp, mail sent android ics devices, gingerbread device mail not sent. can go through code , me...
firstly m calling sendmail() method of gmailoauthsender class.
class gmailoauthsender { private session session; public smtptransport connecttosmtp(string host, int port, string useremail, string oauthtoken, boolean debug) throws exception { properties props = new properties(); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true"); props.put("mail.smtp.sasl.enable", "false"); session = session.getinstance(props); session.setdebug(debug); final urlname unusedurlname = null; smtptransport transport = new smtptransport(session, unusedurlname); // if password non-null, smtp tries auth login. final string emptypassword = null; transport.connect(host, port, useremail, emptypassword); byte[] response = string.format("user=%s\1auth=bearer %s\1\1", useremail, oauthtoken).getbytes(); response = base64encoderstream.encode(response); transport.issuecommand("auth xoauth2 " + new string(response), 235); return transport; } public synchronized void sendmail(string subject, string body, string user, string oauthtoken, string recipients, string cc) { try { spinner = progressdialog.show(feedbackactivity.this, "", getresources().getstring(r.string.sending_mail), false, true); new sendmailtask().execute(subject, body, user, oauthtoken, recipients, cc); } catch (exception e) { log.d("exception inside send mail method", e.tostring()); } } class sendmailtask extends asynctask<object, object, object> { @override protected object doinbackground(object... params) { string subject = (string) params[0]; string body = (string) params[1]; string user = (string) params[2]; string oauthtoken = (string) params[3]; string recipients = (string) params[4]; string cc = (string) params[5]; try { smtptransport smtptransport = connecttosmtp( "smtp.gmail.com", 587, user, oauthtoken, false); for(int = 0; i< attachedfiles.size(); i++){ file file = new file(attachedfiles.get(i).getfilepath()); addattachment(multipart, file); } mimemessage message = new mimemessage(session); message.setsender(new internetaddress(user)); message.setsubject(subject); mimebodypart mbp1 = new mimebodypart(); mbp1.settext(body + "\nthanks\n"+user+"\n\n"+getresources().getstring(r.string.signature_text)); multipart.addbodypart(mbp1); message.setcontent(multipart); if (recipients.indexof(',') > 0){ message.setrecipients(message.recipienttype.to, internetaddress.parse(recipients)); }else{ message.setrecipient(message.recipienttype.to, new internetaddress(recipients)); } if(cc != null && cc.length() >0){ if (cc.indexof(',') > 0){ message.addrecipients(message.recipienttype.cc, internetaddress.parse(cc)); }else{ message.addrecipient(message.recipienttype.cc, new internetaddress(cc)); } } smtptransport.sendmessage(message, message.getallrecipients()); spinner.dismiss(); finish(); return new object[] { smtptransport, message }; } catch (exception e) { spinner.dismiss(); finish(); } return null; } @override protected void onpostexecute(object result) { super.onpostexecute(result); if(result != null){ toast.maketext(feedbackactivity.this, getresources().getstring(r.string.mail_sent), toast.length_long).show(); }else{ toast.maketext(feedbackactivity.this, getresources().getstring(r.string.mail_not_sent), toast.length_long).show(); } } } }
Comments
Post a Comment