我在Android应用程序中使用GmailSender。我想发送背景邮件而无需用户交互。发件人邮件ID和密码是硬编码的。我可以通过WIFI网络发送邮件,但不能通过移动网络发送邮件。
我收到以下错误:
06-23 13:49:02.483 12617-12686/com.android.sync.client.view W/System.err﹕ javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtI
06-23 13:49:02.483 12617-12686/com.android.sync.client.view W/System.err﹕ 534-5.7.14 qg52WWNjNZOHmj7IBmGfCHrdfoS-4LdRTeBtNAAQnY4tqO4SAXeycCq6qpkW
06-23 13:49:02.483 12617-12686/com.android.sync.client.view W/System.err﹕ 534-5.7.14 9ucShyDKruN9FT10S3BOkCLwhbC5ERe-rGgzc01CQTSGQ6OjGvGX_DITw24wqGtqJH_1zw
06-23 13:49:02.483 12617-12686/com.android.sync.client.view W/System.err﹕ 534-5.7.14 hy_a1mL3D4ekerSFekwYj42iRo-6ZUNwQait3VzJXJyNwoC3HEr8sV8-oZuHJFNGl6mHF9
06-23 13:49:02.483 12617-12686/com.android.sync.client.view W/System.err﹕ 534-5.7.14 XapTjLkcfsCwcjelLFsZITtPABkoE> Please log in via your web browser and
06-23 13:49:02.483 12617-12686/com.android.sync.client.view W/System.err﹕ 534-5.7.14 then try again.
06-23 13:49:02.483 12617-12686/com.android.sync.client.view W/System.err﹕ 534-5.7.14 Learn more at
06-23 13:49:02.483 12617-12686/com.android.sync.client.view W/System.err﹕ 534 5.7.14 https://support.google.com/mail/answer/78754 o22sm5125389pfa.15 - gsmtp
06-23 13:49:02.483 12617-12686/com.android.sync.client.view W/System.err﹕ [ 06-23 13:49:02.501 12617:12623 W/art ]
Suspending all threads took: 16.916ms
06-23 13:49:02.503 12617-12686/com.android.sync.client.view W/System.err﹕ at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:809)
06-23 13:49:02.503 12617-12686/com.android.sync.client.view W/System.err﹕ at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:752)
06-23 13:49:02.503 12617-12686/com.android.sync.client.view W/System.err﹕ at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:669)
06-23 13:49:02.504 12617-12686/com.android.sync.client.view W/System.err﹕ at javax.mail.Service.connect(Service.java:317)
06-23 13:49:02.504 12617-12686/com.android.sync.client.view W/System.err﹕ at javax.mail.Service.connect(Service.java:176)
06-23 13:49:02.504 12617-12686/com.android.sync.client.view W/System.err﹕ at javax.mail.Service.connect(Service.java:125)
06-23 13:49:02.504 12617-12686/com.android.sync.client.view W/System.err﹕ at javax.mail.Transport.send0(Transport.java:194)
06-23 13:49:02.504 12617-12686/com.android.sync.client.view W/System.err﹕ at javax.mail.Transport.send(Transport.java:124)
06-23 13:49:02.505 12617-12686/com.android.sync.client.view W/System.err﹕ at com.android.sync.mail.GMailSender.sendMail(GMailSender.java:116)
06-23 13:49:02.505 12617-12686/com.android.sync.client.view W/System.err﹕ at com.android.sync.ui.SyncFragment$MailAsyncTask.doInBackground(SyncFragment.java:476)
06-23 13:49:02.505 12617-12686/com.android.sync.client.view W/System.err﹕ at com.android.sync.ui.SyncFragment$MailAsyncTask.doInBackground(SyncFragment.java:431)
06-23 13:49:02.505 12617-12686/com.android.sync.client.view W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:295)
06-23 13:49:02.505 12617-12686/com.android.sync.client.view W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-23 13:49:02.505 12617-12686/com.android.sync.client.view W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
06-23 13:49:02.505 12617-12686/com.android.sync.client.view W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
06-23 13:49:02.505 12617-12686/com.android.sync.client.view W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
06-23 13:49:02.505 12617-12686/com.android.sync.client.view W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
根据这个我允许访问该Gmail设置中的所有应用程序。但仍然没有运气。
这是我的GmailSender类:
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
String ContentType = "";
static {
Security.addProvider(new JSSEProvider());
}
public GMailSender(String user, String password,IVYApplication ivyapp) {
this.user = user;
this.password = password;
C_MailBO mailBO = ivyapp.configHelper.getMailBO();
Properties props = new Properties();
props.setProperty("mail.transport.protocol", mailBO.getProtocol());
props.setProperty("mail.host", mailBO.getHost());
props.put("mail.smtp.auth", mailBO.getAuth());
props.put("mail.smtp.port", mailBO.getSmtpport());
props.put("mail.smtp.socketFactory.port", mailBO.getSocketfactoryport());
props.put("mail.smtp.socketFactory.class", mailBO.getSocketfactoryclass());
props.put("mail.smtp.socketFactory.fallback", mailBO.getSocketfactoryfallback());
props.setProperty("mail.smtp.quitwait", mailBO.getQuitwait());
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipient, String ccrecipent, File file) throws Exception {
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.addRecipient(Message.RecipientType.CC, new InternetAddress(ccrecipent));
message.setSubject(subject);
// 3) create MimeBodyPart object and set your message content
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
List<Uri> uriList = new ArrayList<Uri>();
uriList.add(Uri.fromFile(file));
for (int i = 0; i < uriList.size(); i++) {
// 4) create new MimeBodyPart object and set DataHandler object
// to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = uriList.get(i).getPath().substring(uriList.get(i).getPath().lastIndexOf("/") + 1, uriList.get(i).getPath().length());// change
// accordingly
System.out.println("filename " + filename);
FileDataSource source = new FileDataSource(uriList.get(i).getPath());
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);
// 5) create Multipart object and add MimeBodyPart objects to
// this object
multipart.addBodyPart(messageBodyPart2);
}
// 6) set the multiplart object to the message object
message.setContent(multipart);
// 7) send message
Transport.send(message);
System.out.println("message sent....");
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
@Override
public PrintWriter getLogWriter() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public int getLoginTimeout() throws SQLException {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setLogWriter(PrintWriter arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public void setLoginTimeout(int arg0) throws SQLException {
// TODO Auto-generated method stub
}
@Override
public boolean isWrapperFor(Class<?> arg0) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public <T> T unwrap(Class<T> arg0) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Connection getConnection() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Connection getConnection(String arg0, String arg1)
throws SQLException {
// TODO Auto-generated method stub
return null;
}
}
}