我在网上搜索了JavaMail代码,使用我从API检索的令牌发送电子邮件,我找到了以下我现在使用的代码:
package com.provIDer;import java.io.ByteArrayinputStream;import java.io.IOException;import java.io.inputStream;import java.io.OutputStream;import java.security.ProvIDer;import java.security.Security;import java.util.PropertIEs;import javax.activation.DataHandler;import javax.activation.DataSource;import javax.mail.Session;import javax.mail.Transport;import javax.mail.URLname;import javax.mail.Message;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.util.ByteArrayDataSource;import androID.util.Log;import com.sun.mail.smtp.SMTPTransport;import com.sun.mail.util.BASE64EncoderStream;public class GMailOauthSender {private Session session;private String mailhost = "smtp.gmail.com"; private int port = 587;private String user; private String password; 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","true"); props.put("mail.smtp.auth","true"); props.put("mail.imap.auth.login.disable","true"); props.put("mail.imap.auth.plain.disable","true"); 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=%sauth=Bearer %s",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 recipIEnts) { try { SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com",587,user,oauthToken,true); MimeMessage message = new MimeMessage(session); DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(),"text/plain")); message.setSender(new InternetAddress(user)); message.setSubject(subject); message.setDataHandler(handler); if (recipIEnts.indexOf(',') > 0) message.setRecipIEnts(Message.RecipIEntType.TO,InternetAddress.parse(recipIEnts)); else message.setRecipIEnt(Message.RecipIEntType.TO,new InternetAddress(recipIEnts)); smtpTransport.sendMessage(message,message.getAllRecipIEnts()); } catch (Exception e) { //Log.d("test",e.getMessage()); }}
不幸的是,代码根本不起作用.到目前为止,我已经坚持了三个多星期.有什么建议?
解决方法 当我第一次这样做并使其工作时真的很痛苦.遵循这些步骤首先,您需要在开发人员控制台中为您的应用设置OAuth2,有关详细信息,请转到此link
现在您需要添加这4个文件,这些将有助于在后台发送邮件.当用户打开应用程序时,将向用户显示同意屏幕(文件AUthActivity.java中的代码),并且必须允许该应用程序使用gmail,这是一次性活动,以后不再需要.执行此 *** 作时,用户从Google服务器请求令牌,并将保存在首选项中,以便不再询问用户(AuthPreferences.java).用户批准后,您可以使用以下方式发送邮件:
GMailSender gMailSender = new GMailSender();gMailSender.sendMail("hi","hi",authPreferences.getUser(),authPreferences.getToken(),"somemailID@gmail.com");
github:https://gist.github.com/ranjithnair02/1c6dab7dec51971abfec中文件的链接
您还需要将以下jar文件添加到项目中:
http://javamail-android.googlecode.com/files/mail.jar
http://javamail-android.googlecode.com/files/activation.jar
http://javamail-android.googlecode.com/files/additionnal.jar
您还需要在AndroIDmanifest.xml中添加以下内容
<uses-permission androID:name="androID.permission.GET_ACCOUNTS" /><uses-permission androID:name="androID.permission.INTERNET" /><uses-permission androID:name="androID.permission.USE_CREDENTIALS" /><uses-permission androID:name="androID.permission.INTERNET" /><uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />@H_403_2@ 总结
以上是内存溢出为你收集整理的Android – 使用JavaMail和OAuth2发送电子邮件全部内容,希望文章能够帮你解决Android – 使用JavaMail和OAuth2发送电子邮件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)