2012-02-02 71 views
0

我已經使用JavaMail-android庫在後臺發送郵件。爲了從用戶名和密碼中釋放用戶,我決定使用在Android手機中添加賬戶/配置賬戶時生成的身份驗證令牌。因此,我可以獲得令牌以及如何我可以像使用smtp(javamail-android)庫一樣使用令牌在後臺發送郵件。如何獲取驗證令牌並在後臺發送電子郵件?

回答

4

嗨,它很容易通過Service打電話給你的電子郵件發送課程它將自動工作電子郵件將在特定時間發送。

我在這裏發佈了示例代碼: 您應該在服務中編寫代碼,因爲GMailSender sender = new GMailSender(send_id,send_pass,imgpath); 它僅用於通過您的gmail id發送電子郵件。

現在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。並確保你有進口所有庫需要..

+1

謝謝。我需要使用authtoken進行身份驗證,當用戶添加帳戶到帳戶經理和使用此帳戶通過身份驗證和發送電子郵件通過gmail在後臺 – RanjitRock 2012-02-02 10:43:33

相關問題