SMTP 経由でメールを送信するコードを作成しました。メールは Android ICS デバイスから送信されますが、Gingerbread デバイスからはメールが送信されません。誰でもコードを調べて助けてもらえますか...
まず、GMailOauthSender クラスの sendmail() メソッドを呼び出しています。
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 the password is non-null, SMTP tries to do 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 i = 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();
}
}
}
}