在Android中发送背景电子邮件获取例外

时间:2015-02-12 08:02:50

标签: java android

我在发送背景电子邮件时收到authentication Exception。过去两天,我正在搜索并尝试不同的事情。但无法解决。请帮帮我

例外

02-12 12:59:22.093: W/System.err(7862): javax.mail.AuthenticationFailedException
02-12 12:59:22.093: W/System.err(7862):     at javax.mail.Service.connect(Service.java:319)
02-12 12:59:22.093: W/System.err(7862):     at javax.mail.Service.connect(Service.java:169)
02-12 12:59:22.093: W/System.err(7862):     at javax.mail.Service.connect(Service.java:118)
02-12 12:59:22.093: W/System.err(7862):     at javax.mail.Transport.send0(Transport.java:188)
02-12 12:59:22.093: W/System.err(7862):     at javax.mail.Transport.send(Transport.java:118)
02-12 12:59:22.093: W/System.err(7862):     at com.formdev.android.Mail.GMailSender.sendMail(GMailSender.java:102)
02-12 12:59:22.101: W/System.err(7862):     at com.fromdev.android.androidqa.FeedbackActivity$4.run(FeedbackActivity.java:132)
02-12 12:59:22.101: W/System.err(7862):     at java.lang.Thread.run(Thread.java:856)

GMailSender

public class GMailSender extends javax.mail.Authenticator {
// ===========================================================
// Constants
// ===========================================================

// ===========================================================
// Fields
// ===========================================================
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private javax.mail.Session session;

// ===========================================================
// Constructors
// ===========================================================

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

static {
    Security.addProvider(new JSSProvider());
}

// ===========================================================
// Methods
// ===========================================================
public GMailSender(final String user, final String password) {
    this.user = user;
    this.password = password;

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");

    props.setProperty("mail.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");

    session = javax.mail.Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, password);// Specify
                                                                        // the
                                                                        // Username
                                                                        // and
                                                                        // the
                                                                        // PassWord
                }
            });
}

protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(user, password);
}

public synchronized void sendMail(String subject, String body,
        String sender, String recipients) throws Exception {
    try {
        MimeMessage message = new MimeMessage(session);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(
                body.getBytes(), "text/plain"));
        message.setSender(new InternetAddress(sender));
        message.setSubject(subject);
        message.setDataHandler(handler);
        if (recipients.indexOf(',') > 0)
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(recipients));
        else
            message.setRecipient(Message.RecipientType.TO,
                    new InternetAddress(recipients));
        Transport.send(message);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
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");
    }
}

}

JSSProvider

public class JSSProvider extends Provider {

    public JSSProvider() {
         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;
                }
            });
    }

MainActivity

GMailSender sender = new GMailSender("sender, "password");
                    sender.sendMail(subject, message,
                            "sender","receiver");

1 个答案:

答案 0 :(得分:0)

可能是由Gmail帐户保护导致此问题。只需点击下面的链接并禁用安全设置即可。 https://www.google.com/settings/security/lesssecureapps