使用JavaMail API时没有得到任何响应

时间:2017-08-03 10:41:22

标签: java email java-ee javamail

我正在尝试使用Gmail smtp服务器从网页向我的电子邮件ID发送电子邮件。我在stackoverflow上尝试了各种答案。不幸的是,他们都没有工作。

   `public void sendMail(String msg){

    Properties props= System.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtps.debug", "true");
    props.put("mail.smtp.socketFactory.fallback", "false");
    //props.put("mail.smtp.ssl.enable", "true");
    //props.put("mail.transport.protocol", "smtp");

    Authenticator authenticate= new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication(username, password);
            }
            };
    @SuppressWarnings("deprecation")
    Session mailSession= Session.getInstance(props, authenticate);
    mailSession.setDebug(true);

    Message mailMessage= new MimeMessage(mailSession);

    try {
        mailMessage.setFrom(new InternetAddress(mailFrom));
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        mailMessage.setSubject(subject);
        mailMessage.setText(msg);

        Transport trans= mailSession.getTransport("smtp");
        trans.connect("smtp.gmail.com", username, password);
        trans.sendMessage(mailMessage, mailMessage.getAllRecipients());
        trans.close();

    } catch (Exception ex) {
        Logger.getLogger(MailSender.class.getName()).log(Level.SEVERE, null, ex);
    }

}` 

我正在尝试上面编写的代码

DEBUG: setDebug: JavaMail version 1.5.4 Info: DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] Info: DEBUG SMTP: useEhlo true, useAuth true Info: DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false

而且,这是我作为一个输出,没有例外,没有消息,甚至没有电子邮件到我的电子邮件地址。 我将JavaMail API 1.5.4 Java 8和GlassFish Server用作本地主机服务器,并参考了在线教程。

请帮助!!

1 个答案:

答案 0 :(得分:0)

不是我用于SendGrid帐户的JavaMail,而是一些较旧的apache http代码,可以帮助您进行调试。我不知道它是否仍然有效,但应该这样做。注意端口差异。

    SimpleSMTPHeader header;
    AuthenticatingSMTPClient client;
    server = "smtp.gmail.com";

    client = new AuthenticatingSMTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(
                                          new PrintWriter(System.out), true));

    client.connect(server, 587);
    client.ehlo( client.getLocalAddress().getHostName() );
    client.execTLS();

    if (!SMTPReply.isPositiveCompletion(client.getReplyCode()))
    {
        client.disconnect();
        System.err.println("SMTP server refused connection.");
        System.exit(1);
    }

    client.auth(AuthenticatingSMTPClient.AUTH_METHOD.LOGIN,  "gmail-email@gmail.com", "password");


    sender = "sender@email.com";
    subject = "A subject";

    BufferedReader reader = new BufferedReader( new FileReader( "html/DocumentReport.html") );
    client.mail("<email@email.com>");
    String receipient = "recipient@email.com;

    client.addRecipient(receipient);

    writer = client.sendMessageData();

    header = new SimpleSMTPHeader(sender, receipient, subject);
    header.addHeaderField("Mime-Version", "1.0");
    header.addHeaderField("Content-Type", "text/html; charset=\"ISO-8859-1\"");
    header.addHeaderField("Content-Transfer-Encoding", "7bit");


    writer.write(header.toString());
    try {
        Util.copyReader(reader, writer);
    } finally {
        reader.close();             
    }

    writer.close();

    boolean completed = client.completePendingCommand();

    client.logout();

    client.disconnect();