2012-11-17 99 views
0

我正在創建一個需要發送電子郵件到特定郵件ID的Android應用程序。我嘗試使用intent(ACTION_SEND)。因爲它需要用戶交互,我沒有使用這種方法。我想要的是通過後端發送電子郵件(沒有用戶的知識)。任何人都可以告訴我最適合的方式嗎?在此先感謝...直接從Android應用程序發送電子郵件

+0

你可以使用這個從後臺發送電子郵件。 http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a –

回答

0

使用這個類來發送電子郵件。 使用用戶名和密碼作爲參數創建此類的對象。

public class MailSender { 
private String mailhost = "smtp.gmail.com"; 
private String user; 
private String password; 
private Session session; 
private Multipart _multipart; 
public BodyPart messageBodyPart; 
DataSource source; 
static { 
    Security.addProvider(new com.PackageName.JSSEProvider()); 
} 

public MailSender(String user, String password) { 
    this.user = user; 
    this.password = password; 

    Properties props = new Properties(); 
    props.setProperty("mail.transport.protocol", "smtp"); 
    props.setProperty("mail.smtp.host", mailhost); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class", 
      "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 
    props.setProperty("mail.smtp.quitwait", "false"); 
    _multipart = new MimeMultipart(); 
    session = Session.getInstance(props, new MailAuthenticator(user, 
      password)); 

} 

public synchronized void sendMail(final String subject, final String uuid, 
     final String address, final double latitude, 
     final double longitude, final String recipients, 
     final String filepath, final Context context, final int i, final int j) 
     throws Exception { 

    Thread thread = new Thread() { 
     public void run() { 
      Looper.prepare(); 
      try { 
       System.out.println("SENDING  MAIL"); 
       Message message = new MimeMessage(session); 
       messageBodyPart = new MimeBodyPart(); 
       DatabaseAdapter db = new DatabaseAdapter(context); 
       String eId = db.getYourId(); 
       message.setFrom(new InternetAddress(eId)); 
       message.setSubject(subject); 
       SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
       String currentDate = sdf.format(new Date()); 
       SimpleDateFormat sTf = new SimpleDateFormat("hh:mm:ss"); 
       String currentTime = sTf.format(new Date()); 
       System.out.println(i); 
       messageBodyPart 
         .setText("Someone tried to unlock your device(ID : "+uuid+") at: " 
           + currentTime + " on " + currentDate + "\n" 
           + "Device Location : " + address + "\n" 
           + "Map: " + "http://maps.google.com/?q=" 
           + latitude + "," + longitude); 
       if (recipients.indexOf(',') > 0) 
        message.setRecipients(Message.RecipientType.TO, 
          InternetAddress.parse(recipients)); 
       else 
        message.setRecipient(Message.RecipientType.TO, 
          new InternetAddress(recipients)); 

       Transport transport = session.getTransport("smtp"); 
       System.out.println("CONECTING....."); 
       transport.connect(mailhost, user, password); 
       message.saveChanges(); 
       message.setContent(_multipart); 
       _multipart.addBodyPart(messageBodyPart); 
       message.setContent(_multipart); 
       if (source != null) { 

        message.setFileName("image"); 
       } 
       Transport.send(message); 
       System.out.println("Mail sent ..."); 

       } 

       transport.close(); 


      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 
    }; 
    thread.start(); 

} 

public void addAttachment(String filename) throws Exception { 
    if (!filename.equals("")) { 
     System.out.println("Ataching  file  :)"); 
     messageBodyPart = new MimeBodyPart(); 
     source = new FileDataSource(filename); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setFileName(filename); 
     _multipart.addBodyPart(messageBodyPart); 
     System.out.println("FILE ATTACHED  :)"); 
    } 
} 


class MailAuthenticator extends Authenticator { 
    String us; 
    String pw; 

    public MailAuthenticator(String username, String password) { 
     super(); 
     this.us = username; 
     this.pw = password; 
    } 

    public PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(us, pw); 
    } 
} 

}

而且在其他文件中添加這一點。

public final class JSSEProvider extends Provider { 
private static final long serialVersionUID = 1L; 

public JSSEProvider() { 
    super("HarmonyJSSE", 1.0, "Harmony JSSE Provider"); 
    AccessController 
      .doPrivileged(new java.security.PrivilegedAction<Void>() { 
       public Void run() { 
        put("SSLContext.TLS", 
          "org.apache.harmony.xnet.provider.jsse.SSLContextImpl"); 
        put("Alg.Alias.SSLContext.TLSv1", "TLS"); 
        put("KeyManagerFactory.X509", 
          "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl"); 
        put("TrustManagerFactory.X509", 
          "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl"); 
        return null; 
       } 
      }); 
} 

}

+0

感謝您的回答...有沒有其他的方式可以在沒有提供密碼的情況下實現同樣的效果? – user456

+0

我已經搜索了很多,但我認爲這是從後臺有效地發送電子郵件的唯一方法。 –

+1

DatabaseAdapter無法解析爲獲取此錯誤的類型 – John

0
Intent emailIntent = new Intent(Intent.ACTION_SEND); 
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Message body"); 
emailIntent.setType("application/octet-stream"); 
startActivity(emailIntent); 

也可以使用HTTP POST並在您的服務器端寫入特定的方法。

0

您可以將Java郵件API用於特定的電子郵件ID。你可以修復它。 See this link

相關問題