javax.mail.AuthenticationFailedException:连接失败,没有指定密码?但我知道密码没问题

时间:2016-09-28 20:06:27

标签: javamail

大家好!
我在使用JavaMail API时遇到了问题,尝试按如下方式发送邮件时出现异常:

  

javax.mail.AuthenticationFailedException:连接失败,没有   密码指定   

以下是代码:

package sendmail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by caesar-84 on 9/28/16.
 */
public class SslGmailSender
{
    private String username;
    private String password;
    private Properties props;

    public SslGmailSender(String username, String password)
    {
        this.username = username;
        this.password = password;

        props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
    }

    public void send(String subject, String text, String from, String to)
    {
        Session thisSession = Session.getInstance(props, new Authenticator()
        {
            public PasswordAuthentication getPasswordAuthentification()
            {
                return new PasswordAuthentication(SslGmailSender.this.username, password);
            }
        });
        try
        {
            MimeMessage message = new MimeMessage(thisSession);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setText(text);

            Transport.send(message);
            System.out.println("Yor e-mail has been sent.");
        }
        catch (MessagingException ex)
        {
            System.out.println("Uuups... something went wrong...");
            System.out.println(ex.getMessage());
            System.exit(2);
        }
    }
}

主要:

package sendmail;

import java.io.*;

/**
 * Created by caesar-84 on 9/28/16.
 */
public class Main
{
    public static void main(String[] args)
    {
        String username = "";
        String password = "";
        String subject = "";
        String text = "";
        String to = "";
        String typedText = "";
        StringBuilder sb = new StringBuilder();

        try(
                BufferedReader console = new BufferedReader(new InputStreamReader(System.in)package sendmail;

import java.io.*;

/**
 * Created by caesar-84 on 9/28/16.
 */
public class Main
{
    public static void main(String[] args)
    {
        String username = "";
        String password = "";
        String subject = "";
        String text = "";
        String to = "";
        String typedText = "";
        StringBuilder sb = new StringBuilder();

        try(
                BufferedReader console = new BufferedReader(new InputStreamReader(System.in)))
        {
            System.out.print("1. Enter manually\n2. Load data form file?\nType 1 or 2: ");
            String response = console.readLine();
            System.out.println();
            switch (response)
            {
                case "1": {
                    System.out.print("Your e-mail: ");
                    username = console.readLine();
                    System.out.print("Password: ");
                    password = console.readLine();
                    System.out.print("Recipient: ");
                    to = console.readLine();
                    System.out.print("Message subject: ");
                    subject = console.readLine();
                    System.out.println("Text to send (type \"-END\" to finish):");
                    while (!typedText.contains("-END"))
                    {
                        typedText = console.readLine();
                        sb.append(typedText).append("\n");
                    }
                    sb.delete(sb.length() - 4, sb.length());
                }
                case "2":{
                    System.out.print("Enter the filename: ");
                    String input = console.readLine();
                    System.out.println();
                    //if (input.equals("def")) input = "/home/caesar-84/java_tries/learning/src/sendmail/mailtext.txt";
                    BufferedReader fr = new BufferedReader(new FileReader(input));
                    username = fr.readLine();
                    System.out.println("E-mail: " + username);
                    password = fr.readLine();
                    System.out.println("Password: " + password);
                    to = fr.readLine();
                    System.out.println("Recipient: " + to);
                    subject = fr.readLine();
                    System.out.println("Subject: " + subject);

                    while (fr.ready())
                    {
                        typedText = fr.readLine();
                        sb.append(typedText).append("\n");
                    }
                    fr.close();
                }
            }

        }
        catch (FileNotFoundException ex)
        {
            System.out.println("Such file does not exist. Terminating.");
            System.exit(1);
        }
        catch (IOException ex){ex.printStackTrace();}

        System.out.println();
        text = sb.toString();
        System.out.println(text);
        String from = username;
        SslGmailSender thisSender = new SslGmailSender(username, password);
        thisSender.send(subject, text, from, to);
    }
}))
        {
            System.out.print("1. Enter manually\n2. Load data form file?\nType 1 or 2: ");
            String response = console.readLine();
            System.out.println();
            switch (response)
            {
                case "1": {
                    System.out.print("Your e-mail: ");
                    username = console.readLine();
                    System.out.print("Password: ");
                    password = console.readLine();
                    System.out.print("Recipient: ");
                    to = console.readLine();
                    System.out.print("Message subject: ");
                    subject = console.readLine();
                    System.out.println("Text to send (type \"-END\" to finish):");
                    while (!typedText.contains("-END"))
                    {
                        typedText = console.readLine();
                        sb.append(typedText).append("\n");
                    }
                    sb.delete(sb.length() - 4, sb.length());
                }
                case "2":{
                    System.out.print("Enter the filename: ");
                    String input = console.readLine();
                    System.out.println();
                    //if (input.equals("def")) input = "/home/caesar-84/java_tries/learning/src/sendmail/mailtext.txt";
                    BufferedReader fr = new BufferedReader(new FileReader(input));
                    username = fr.readLine();
                    System.out.println("E-mail: " + username);
                    password = fr.readLine();
                    System.out.println("Password: " + password);
                    to = fr.readLine();
                    System.out.println("Recipient: " + to);
                    subject = fr.readLine();
                    System.out.println("Subject: " + subject);

                    while (fr.ready())
                    {
                        typedText = fr.readLine();
                        sb.append(typedText).append("\n");
                    }
                    fr.close();
                }
            }

        }
        catch (FileNotFoundException ex)
        {
            System.out.println("Such file does not exist. Terminating.");
            System.exit(1);
        }
        catch (IOException ex){ex.printStackTrace();}

        System.out.println();
        text = sb.toString();
        System.out.println(text);
        String from = username;
        SslGmailSender thisSender = new SslGmailSender(username, password);
        thisSender.send(subject, text, from, to);
    }
}

1 个答案:

答案 0 :(得分:-1)

这样解决了:

package sendmail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by caesar-84 on 9/28/16.
 */
public class SslSender
{
    private String username;
    private String password;
    private Properties props;
    private String smtpMailProvider;
    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

    public SslSender(String username, String password)
    {
        this.username = username;
        this.password = password;
        if (username.substring(username.indexOf("@") + 1, username.length()).equals("hotmail.com"))
        this.smtpMailProvider = "smtp.live.com";
        else this.smtpMailProvider = "smtp." + username.substring(username.indexOf("@") + 1, username.length());

        props = System.getProperties();

        props.put("mail.smtp.user", username);
        props.put("mail.smtp.host", smtpMailProvider);
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.put("mail.smtp.ssl.trust", smtpMailProvider);


    }

    public void send(String subject, String text, String from, String to)
    {
        Session thisSession = Session.getInstance(props, new Authenticator()
        {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(SslSender.this.username, SslSender.this.password);
            }
        });
        MimeMessage message = new MimeMessage(thisSession);

        try
        {
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setText(text);

            Transport transport = thisSession.getTransport();
            transport.connect(smtpMailProvider, username, password);
            Transport.send(message);
            System.out.println("Your e-mail has been sent.");
        }
        catch (MessagingException ex)
        {
            System.out.println("Uuups... something went wrong...");
            System.out.println(ex.getMessage());
            System.exit(2);
        }
    }
}

PS。仍然不适用于hotmail。

相关问题