0
大家好!
用JavaMail API的工作过程中,我有一个问题,我在尝试如下发送邮件时异常:javax.mail.AuthenticationFailedException:未能连接,未指定密码?但我知道密码是好的
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);
}
}
我遇到过这样的问题,因为谷歌不会让你登录“不安全的应用程序”,除非你在你的账户上启用它。我得到它的唯一方法是使用OAuth2,即使ssl完全“安全”,我认为他们只是担心有人偷走了您的密码。这可能与......无关...... –
修复所有这些[常见JavaMail错误](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes),并且很可能会解决您的问题。 –
我将代码发送给我的朋友,它可以工作,但他使用Windows和Linux。所以,这可能是操作系统的问题。 –