我已经使用JavaMail-androID库在后台发送邮件.为了从用户名和密码中释放用户,我决定使用在Android手机中添加帐户/配置帐户时生成的Auth令牌.所以我可以获得令牌,我怎么能使用令牌在后台发送邮件,就像使用smtp(javamail-androID)库一样.
解决方法:
嗨,如此简单易用的电子邮件发送课程通过Service它将自动工作电子邮件将发送特定的时间.
这里我发布了示例代码:
你应该把服务代码写成GMailSender sender = new GMailSender(send_ID,send_pass,imgpath);
它仅用于通过您的Gmail密码发送电子邮件.
现在GmailSender.java如下:
public class GMailSender extends javax.mail.Authenticator
{
private String mailhost = "smtp.gmail.com"; private String user; private String password; private Session session; private String path_img; static { // AppLogger.LogError("Reached to Step1.1"); Security.addProvIDer(new JsSEProvIDer()); } public GMailSender(String user, String password,String path) { path_img = path; // AppLogger.LogError("Reached to Step1.2"); this.user = user; this.password = password; PropertIEs props = new PropertIEs(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); //AppLogger.LogError("Reached to Step1.3"); session = Session.getDefaultInstance(props, this); //AppLogger.LogError("Reached to Step1.4");} protected PasswordAuthentication getpasswordAuthentication() { return new PasswordAuthentication(user, password);} public synchronized voID sendMail(String subject, String body, String sender, String recipIEnts) throws Exception { try{ // AppLogger.LogError("Reached to Step1.5"); MimeMessage message = new MimeMessage(session); // AppLogger.LogError("Reached to Step1.6"); DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/HTML")); message.setSender(new InternetAddress(sender)); message.setRecipIEnts(Message.RecipIEntType.TO,InternetAddress.parse(recipIEnts)); //AppLogger.LogError("Reached to Step1.7"); message.setSubject(subject); message.setDataHandler(handler); MimeMultipart multipart = new MimeMultipart("related"); String HTMLText=null; BodyPart messageBodyPart = new MimeBodyPart(); HTMLText = body+ ""; messageBodyPart.setContent(HTMLText, "text/HTML"); MimeBodyPart attachmentPart = new MimeBodyPart(); fileDataSource fileDataSource = new fileDataSource(path_img) { @OverrIDe public String getContentType() { return "image/jpg"; } }; attachmentPart.setDataHandler(new DataHandler(fileDataSource)); attachmentPart.setfilename("image.jpg"); multipart.addBodyPart(messageBodyPart); multipart.addBodyPart(attachmentPart); message.setContent(multipart); //AppLogger.LogError("Reached to Step1.8"); if (recipIEnts.indexOf(',') > 0) { //AppLogger.LogError("Reached to Step1.9"); message.setRecipIEnts(Message.RecipIEntType.TO, InternetAddress.parse(recipIEnts)); Transport.send(message); // AppLogger.LogError("Reached to Step2.1"); } else { //AppLogger.LogError("Reached to Step2.2"); message.setRecipIEnt(Message.RecipIEntType.TO, new InternetAddress(recipIEnts)); Transport.send(message); //AppLogger.LogError("Reached to Step2.3"); } // Transport.send(message); // AppLogger.LogError("Reached to Step2.4"); }catch (Exception e) { throw new fileNotFoundException(); }} 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"); } }
尝试此示例并尝试所有电子邮件ID.并确保您已导入所有需要的库..
总结以上是内存溢出为你收集整理的android – 如何获取getAuth令牌并在后台发送电子邮件?全部内容,希望文章能够帮你解决android – 如何获取getAuth令牌并在后台发送电子邮件?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)