0

我有这段代码:

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import android.telephony.gsm.GsmCellLocation;

public class EmailSender {

    public void sendEmail(String personId, GsmCellLocation location) {
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", true); // added this line
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        props.put("mail.smtp.user", "*******");
        props.put("mail.smtp.password", "******");
        props.put("mail.smtp.port", "25");
        props.put("mail.smtp.auth", true);
        Session session = Session.getInstance(props, new GMailAuthenticator("*****", "*******"));

        MimeMessage message = new MimeMessage(session);

        System.out.println("Port: " + session.getProperty("mail.smtp.port"));

        // Create the email addresses involved
        try {
            InternetAddress from = new InternetAddress("*****@gmail.com");
            message.setSubject("Yes we can");
            message.setFrom(from);
            message.addRecipients(Message.RecipientType.TO, InternetAddress.parse("****@gmail.com"));

            // Create a multi-part to combine the parts
            Multipart multipart = new MimeMultipart("alternative");

            // Create your text message part
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText("some text to send");

            // Add the text part to the multipart
            multipart.addBodyPart(messageBodyPart);

            // Create the html part
            messageBodyPart = new MimeBodyPart();
            String htmlMessage = "Person " + personId + " at location: " + location + " might be in trouble, please check! ";
            messageBodyPart.setContent(htmlMessage, "text/html");


            // Add html part to multi part
            multipart.addBodyPart(messageBodyPart);

            // Associate multi-part with message
            message.setContent(multipart);

            // Send message
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.gmail.com", "username", "password");
            System.out.println("Transport: " + transport.toString());
            transport.sendMessage(message, message.getAllRecipients());


        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

向自己发送电子邮件。

当我尝试在我的 Android 设备上运行它时,它一直在说:

http://pastebin.com/C6vCuSke

因此,mail.jar 和 Activation.jar 文件的导入似乎出现了问题。然而,我正确地导入了它们,并将它们添加到“订购和导出”选项卡的列表中,当我将它作为普通 Java 应用程序运行时它正在工作。谁能告诉我出了什么问题?

4

2 回答 2

0

可能您错过了导出包含java.awt.datatransfer.Transferable接口的 jar。

我认为它包含在rt.jar中(包含在 JRE 中)。可能应该可以将它导出到 apk,但它很重(那是 Java 运行时!)并且方法的数量可能超过单个 apk 限制(65536)。因此,您可能应该更改电子邮件库。

于 2013-10-08T12:17:31.533 回答
0

我已经发现了问题,我似乎使用了一个以某种方式损坏或损坏的 jar 文件!很高兴我发现了问题:)

于 2013-10-08T13:41:59.757 回答