Java 在Android中发送后台电子邮件获取异常

Java 在Android中发送后台电子邮件获取异常,java,android,Java,Android,我在发送后台电子邮件时遇到身份验证异常。从过去两天开始,我一直在搜索和尝试不同的事情。但无法解决。请帮帮我 例外情况 Gmail发送器 JSSProvider 这个问题可能是由Gmail帐户保护引起的。只需单击下面的链接并禁用安全设置。它将正常工作 为什么您认为这是一个代码问题?您的SMTP服务器用户名和/或密码不正确。@Simon我已验证用户名和密码,没有问题 02-12 12:59:22.093: W/System.err(7862): javax.mail.AuthenticationFa

我在发送后台电子邮件时遇到身份验证异常。从过去两天开始,我一直在搜索和尝试不同的事情。但无法解决。请帮帮我

例外情况

Gmail发送器

JSSProvider


这个问题可能是由Gmail帐户保护引起的。只需单击下面的链接并禁用安全设置。它将正常工作

为什么您认为这是一个代码问题?您的SMTP服务器用户名和/或密码不正确。@Simon我已验证用户名和密码,没有问题
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)
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");
    }
}

}
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;
                }
            });
    }
GMailSender sender = new GMailSender("sender, "password");
                    sender.sendMail(subject, message,
                            "sender","receiver");