如何获取身份令牌并在后台发送电子邮件? [英] how to getAuth token and send email in background?

查看:93
本文介绍了如何获取身份令牌并在后台发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JavaMail-android库在后台发送邮件。为了从用户名和密码中释放用户,我决定在Android手机中添加帐户/配置帐户时使用Auth令牌。我可以获取令牌和如何使用令牌在后台发送邮件,就像使用smtp(javamail-android)库一样。

解决方案

通过服务,您可以简单轻松地发送电子邮件发送课程自动发送电子邮件将在特定的时间发送。



这里我已经发布了示例代码:
您应该在服务中编写代码为 GMailSender sender =新的GMailSender(send_id,send_pass,imgpath);
仅用于通过您的Gmail邮件发送电子邮件。



现在GmailSender。 java如下:

  public class GMailSender extends javax.mail.Authentic ator 

{

 code> private String mailhost =smtp.gmail.com; 
private String user;
private String password;
私人会话会话;
private String path_img;

static {
// AppLogger.LogError(到达Step1.1);
Security.addProvider(new JSSEProvider());
}

public GMailSender(String user,String password,String path)
{
path_img = path;
// AppLogger.LogError(到达Step1.2);
this.user = user;
this.password = password;

属性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(接近Step1.3);
session = Session.getDefaultInstance(props,this);
//AppLogger.LogError(\"接近Step1.4);
}





protected PasswordAuthentication getPasswordAuthentication(){
返回新的PasswordAuthentication(用户,密码);
}

public synchronized void sendMail(String subject,String body,String sender,String recipients)throws异常{
try {

// AppLogger .LogError(到达Step1.5);
MimeMessage message = new MimeMessage(session);
// AppLogger.LogError(到达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(接近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(){
returnimage / jpg;
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(image.jpg);

multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachmentPart);

message.setContent(multipart);

//AppLogger.LogError(\"接近Step1.8);
if(recipients.indexOf(',')> 0)
{
//AppLogger.LogError(\"接近Step1.9);
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipients));
Transport.send(message);
// AppLogger.LogError(到达Step2.1);
}
else
{
//AppLogger.LogError(\"接近Step2.2);
message.setRecipient(Message.RecipientType.TO,新的InternetAddress(收件人));
Transport.send(message);
//AppLogger.LogError(\"接近Step2.3);
}
// Transport.send(message);
// AppLogger.LogError(到达Step2.4);
} catch(Exception e)
{
throw new FileNotFoundException();
}





}

public class ByteArrayDataSource实现DataSource {
private byte []数据;
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)
返回application / octet-stream;
else
返回类型;
}

public InputStream getInputStream()throws IOException {
return new ByteArrayInputStream(data);
}

public String getName(){
returnByteArrayDataSource;
}

public OutputStream getOutputStream()throws IOException {
throw new IOException(Not Supported);
}
}

尝试此示例并尝试所有电子邮件ID。并确保您导入所有需要的库。


I have used JavaMail-android libraries to send mail in background .In order to free user from username and password I decided to use Auth token generated when adding an account /configuring account in android phone .So can i get the token and how could i use the token to send mail in background as a did using smtp (javamail-android) libraries .

解决方案

Hi its so simple and easy call your email sending class through Service it will work automatically email will send on particular time.

Here i have posted sample code: you should write code in service as GMailSender sender = new GMailSender(send_id,send_pass,imgpath); its for sending email through your gmail id only.

and now GmailSender.java as follows:

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");   
    }   
}   

try this sample and try for all email ids. and make sure that you have import all libraries needed..

这篇关于如何获取身份令牌并在后台发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆