我无法使用Java Mail API发送电子邮件,但电子邮件已发送但未收到或未显示在已发送的电子邮件中

卡洛斯·莫埃莫(Carlos Maemo)

当我单击发送按钮时,将发送电子邮件,并且不会显示任何错误,但是未收到电子邮件,也不会出现在已发送的电子邮件中。欢迎任何帮助。

我的代码配置电子邮件类别:

public class Email {

    public static void sendEmail(){
        final String username = "[email protected]";;
        final String password = "my_password";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }});
        try {

            final Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
            message.setSubject("Subject");
            message.setText("Message");

            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try  {
                        Transport.send(message);
                        System.out.println("Done");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            thread.start();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}

我的发送按钮的代码:

btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "SEND", Toast.LENGTH_SHORT).show();
                new Email();
                Email.sendEmail();   
            }
        });
Skype狗

这是我使用的课程:

public class GMailSender extends javax.mail.Authenticator {

    private String mailhost = "smtp.gmail.com";
    private String user;
    private String password;
    private Session session;

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

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

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        if (host != null) mailhost = host;
        props.setProperty("mail.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.socketFactory.port", "587");

        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);
    }

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

    public synchronized void sendMail(String subject, String body, String sender, String recipients, String absolutePath, String fileName) 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.setContent(body, "text/plain");
           // message.setDataHandler(handler);
            MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(body);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(mbp1);


            if (absolutePath != null) {
                MimeBodyPart mbp2 = new MimeBodyPart();

                String file = absolutePath;
                String name = "podpis";
                if (fileName != null) name = fileName;
                name += ".jpg";
                DataSource source = new FileDataSource(file);
                mbp2.setDataHandler(new DataHandler(source));
                mbp2.setFileName(name);
                multipart.addBodyPart(mbp2);

            }
            message.setContent(multipart);

            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){
            Log.e("GMAIL SENDER", e.getMessage());
        }
    }

    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");
        }
    }
}

我要附加一个文件,所以您不需要两个 MimeBodyParts

那就是电话:

String email = "email_adress_TO";

            if (email != null && email.length() > 2 && email.contains("@")) {
                final GMailSender sender = new GMailSender("your_email/name", "password", "hostname");
                try {
                    sender.sendMail("Title",
                                    "message",
                                    "email_adress_FROM",
                                    email,
                                    null,
                                    null);
                } catch (Exception e) {
                    Log.e("SendMail", e.getMessage(), e);
                }

当然在里面AsyncTask

抱歉,我完全忘记了JSSEProvider你去了:

import java.security.AccessController;
import java.security.Provider;

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章