无法使用javax从android使用gmail帐户发送电子邮件 [英] cannot send email using gmail account from android using javax
本文介绍了无法使用javax从android使用gmail帐户发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
发送电子邮件时出错:
返回451 4.5.0
SMTP protocol
违规,请参阅RFC 2821 g5sm13340466pfg.0-gsmtp.
returns 451 4.5.0
SMTP protocol
violation, see RFC 2821 g5sm13340466pfg.0 - gsmtp.
在获取oauth token
后,键入和oauth从gmail
到期.
After getting the oauth token
, type and oauth expire from gmail
.
此处是以下代码,用于测试使用oauth2
发送电子邮件.
here the following code used to test send email using oauth2
.
private String GenerateOAuth2String(boolean base64_encode){
String OAuthString = "";
Log.e("SendTestActivity", "AuthToken: " + authToken);
OAuthString = String.format("user=%s\1auth=Bearer %s\1\1", userName, authToken);
Log.e("SendTestActivity", "non base 64: " + OAuthString);
if (base64_encode)
OAuthString = Base64.encodeToString(OAuthString.getBytes(), Base64.DEFAULT);
Log.e("SendTestActivity", "base 64: " + OAuthString);
return OAuthString;
}
private synchronized void sendMail(String subject, String body, String user, String recipients) {
try {
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(true);
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("smtp.gmail.com", 587, user, emptyPassword);
transport.issueCommand("AUTH XOAUTH2 " + GenerateOAuth2String(true),
235);
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(user));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(body);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
Log.e("SendTestActivity", "email sent");
} catch (Exception e) {
Log.e("SendTestActivity", e.getMessage());
}
}
设置有问题吗?
user =%s \ 1auth = Bearer%s \ 1 \ 1中的\ 1也是字符串"或标题开始"字符
also does the \1 in user=%s\1auth=Bearer %s\1\1 a "string" or a "start of heading" character
推荐答案
您正在这样做. 让JavaMail为您做到这一点.
这篇关于无法使用javax从android使用gmail帐户发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文