2012-10-09 62 views
6
import javax.mail.BodyPart; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.SendFailedException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart;  

private void sendMail() throws MessagingException{ 

    String host = "smtp.gmail.com"; 
    String password = "abcde12345"; 
    String from = "[email protected]"; 
    String toAddress = email; 
    String filename = Environment.getExternalStorageDirectory() + "/jam.jpg"; 

    Properties properties = System.getProperties(); 
    properties.put("mail.smtp.host", host); 
    properties.put("mail.smtps.auth", true); 
    properties.put("mail.smtp.starttls.enable", true); 
    Session session = Session.getInstance(properties, null); 

    MimeMessage message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(from)); 
    message.setRecipients(Message.RecipientType.TO, toAddress); 
    message.setSubject("Anti-Theft Attachment"); 

    BodyPart messageBodyPart = new MimeBodyPart(); 
    messageBodyPart.setText(smsMessageString); 

    Multipart multipart = new MimeMultipart(); 
    multipart.addBodyPart(messageBodyPart); 
    message.setContent(multipart); 

    try{ 
     Transport transport = session.getTransport("smtps"); 
     transport.connect(host, from, password); 
     transport.sendMessage(message, message.getAllRecipients()); 
     System.out.println("Mail Sent Successfully"); 
     transport.close(); 
    } catch (SendFailedException sfe){ 
     System.out.println(sfe); 
    } 
}; 

我开发的应用程序,一旦手机被盗或丢失的应用程序会自动发送一封电子邮件给用户,告知用户当前手机的状态。但是我在导入javax.mail时遇到问题“导入的javax.mail无法解析”。我该怎么办?谢谢...进口javax.mail不能得到解决

+1

http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a – dira

+1

ADD 3 jars found in the following链接到您的Android项目。 http://stackoverflow.com/a/2033124/375953 – dira

+0

感谢您的帮助:) –

回答

相关问题