使用javamail发送邮件获取身份验证问题

时间:2014-11-01 06:01:44

标签: javamail

当我尝试使用以下代码发送邮件时,我遇到了身份验证问题。

这是我的代码:

    public class NewSendMail {

    String to = "********";
    String subject = "subject";
    String msg ="email text....";
    final String from ="*******";
    final  String password ="******";


    public NewSendMail(){

    }

    public boolean sendMymail(){

        Properties props = new Properties();  
        props.setProperty("mail.transport.protocol", "smtp");     
        props.setProperty("mail.host", "smtp.gmail.com");  
        props.put("mail.smtp.auth", "true");  
        props.put("mail.smtp.port", "465");  
        props.put("mail.debug", "true");  
        props.put("mail.smtp.starttls.enable", "true"); 
        props.put("mail.smtp.socketFactory.port", "465");  
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");  
        props.put("mail.smtp.socketFactory.fallback", "false");  
        Session session = Session.getDefaultInstance(props,  
                new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {  
                return new PasswordAuthentication(from,password);  
            }  
        });  

//      Session session = Session.getDefaultInstance(props);

        //session.setDebug(true);  
        Transport transport;
        InternetAddress addressFrom = null;
        try {
            transport = session.getTransport();
            addressFrom = new InternetAddress(from);
            MimeMessage message = new MimeMessage(session);  
            message.setSender(addressFrom);  
            message.setSubject(subject);  
            message.setContent(msg, "text/plain");  
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
            transport.connect();  
            Transport.send(message);  
            transport.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  



        return true;
    }  


}

投掷javax.mail.AuthenticationFailedException

如何解决问题?

2 个答案:

答案 0 :(得分:0)

以下代码可以帮助您

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Email {

 private static String USER_NAME = "username";  // GMail user name (just the part before "@gmail.com")
 private static String PASSWORD = "password"; // GMail password

 private static String RECIPIENT = "xxxxx@gmail.com";

public static void main(String[] args) {
String from = USER_NAME;
String pass = PASSWORD;
String[] to = { RECIPIENT }; // list of recipient email addresses
String subject = "Java send mail example";
String body = "hi ....,!";

sendFromGMail(from, pass, to, subject, body);
}

 private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.ssl.trust", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");


Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);

try {


    message.setFrom(new InternetAddress(from));
    InternetAddress[] toAddress = new InternetAddress[to.length];

    // To get the array of addresses
    for( int i = 0; i < to.length; i++ ) {
        toAddress[i] = new InternetAddress(to[i]);
    }

    for( int i = 0; i < toAddress.length; i++) {
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
    }



    message.setSubject(subject);
    message.setText(body);


    Transport transport = session.getTransport("smtp");


    transport.connect(host, from, pass);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

}
catch (AddressException ae) {
    ae.printStackTrace();
}
catch (MessagingException me) {
    me.printStackTrace();
  }
  }
 } 

此代码对我有用,如果它不适合您,请检查您的jar文件

答案 1 :(得分:0)

我告诉所有要求JavaMail帮助的人。 (我想知道为什么他们称之为FAQ?)

修复这些common mistakes

关注this example for Gmail

启用session debugging以获取有关错误的详细信息,如果仍然无法解决问题,请发布输出。

您要么发送错误的用户名或密码,要么服务器因其他原因拒绝您的登录尝试,可能是因为您尚未启用less secure apps