transport.connect没有做任何javamail

时间:2014-03-24 19:00:34

标签: android javamail

我想使用JavaMail发送电子邮件。我已经拥有的是一个能够(几个月前发送电子邮件)的课程,但现在程序在 transport.connect(emailHost,from,fromPassword)上停止;

SendMails活动:

public class SendMails {

    final String smtpAuth = "true";
    final String starttls = "true";
    final String emailHost = "smtp.gmail.com";

    String fromEmail;
    String fromPassword;
    List<String> toEmailList;
    String emailSubject;
    String emailBody;
    String emailPort;

    Properties emailProperties;
    Session mailSession;
    MimeMessage emailMessage;

    private EmailAccount account;
    private EmailAuthenticator authenticator;

    public SendMails() {

    }

    public SendMails(String fromEmail, String fromPassword,
            List<String> toEmailList, String emailSubject, String emailBody,
            String emailPort) {
        this.fromEmail = fromEmail;
        this.fromPassword = fromPassword;
        this.toEmailList = toEmailList;
        this.emailSubject = emailSubject;
        this.emailBody = emailBody;
        this.emailPort = emailPort;

        String[] splited = fromEmail.split("@");
        Log.w("SEND MAILS", splited[0] + "+" + splited[1]);
        account = new EmailAccount(splited[0], fromPassword, splited[1]);
        authenticator = new EmailAuthenticator(account);
        Log.w("SENDMAILS", " " + emailPort);
        emailProperties = System.getProperties();
        emailProperties.put("mail.smtp.port", this.emailPort);
        emailProperties.put("mail.smtp.auth", smtpAuth);
        emailProperties.put("mail.smtp.starttls.enable", starttls);
    }

    public MimeMessage createEmailMessage() throws AddressException,
            MessagingException, UnsupportedEncodingException {

        mailSession = Session.getDefaultInstance(emailProperties, null);
        emailMessage = new MimeMessage(mailSession);

        emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
        for (String toEmail : toEmailList) {
            Log.i("GMail", "toEmail: " + toEmail);
            emailMessage.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(toEmail));
        }

        emailMessage.setSubject(emailSubject);
        // emailMessage.setContent(emailBody, "text/html");// for a html email
        emailMessage.setText(emailBody);// for a text email
        Log.i("GMail", "Email Message created.");
        return emailMessage;
    }

    public void sendEmail() throws AddressException, MessagingException {
        Transport transport = mailSession.getTransport("smtp");
        Log.i("GMail", "All: " + emailHost + " " + fromEmail + " "
                + fromPassword + " " + emailMessage.toString()+ " " + fromEmail.split("@")[0].toString()); 
        String from = fromEmail.split("@")[0].toString();
        Log.i("GMail", "From: " + from); 
        try {
            transport.connect(emailHost, from, fromPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("GMail", "allrecipients: " + emailMessage.getAllRecipients());
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        Log.i("GMail", "Email sent successfully.");
    }

}

最后一个日志是 Log.i(&#34; GMail&#34;,&#34;来自:&#34; +来自); Log.i(& #34; GMail&#34;,&#34; allrecipients:&#34; + emailMessage.getAllRecipients()); 永远不会显示...也没有异常被抛出..

非常感谢:)

//修改

03-24 22:49:00.090: I/System.out(12018): DEBUG: setDebug: JavaMail version 1.4.7
03-24 22:49:00.190: I/System.out(12018): DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.7]
03-24 22:49:00.220: I/System.out(12018): DEBUG SMTP: useEhlo true, useAuth true
03-24 22:49:00.220: I/System.out(12018): DEBUG SMTP: trying to connect to host "smtp.tuke.sk", port 465, isSSL false

1 个答案:

答案 0 :(得分:1)

添加mailSession.setDebug(true);输出显示了什么?

请参阅JavaMail FAQ entries on debugging connection problems