【问题标题】:sending mail using javamail getting authentication issue使用 javamail 发送邮件获取身份验证问题
【发布时间】:2014-11-01 14:44:06
【问题描述】:

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

这是我的代码:

    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

我该如何解决这个问题?

【问题讨论】:

    标签: jakarta-mail


    【解决方案1】:

    以下代码可能对您有所帮助

    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 文件

    【讨论】:

      【解决方案2】:

      我告诉所有需要 JavaMail 帮助的人同样的事情。 (我想知道他们为什么称它为FAQ?)

      修复这些common mistakes

      关注this example for Gmail

      打开session debugging 以获取有关问题的更多详细信息,如果您仍然无法弄清楚,请发布输出。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-17
        • 2019-06-01
        • 2011-08-04
        • 2013-11-17
        • 2017-09-22
        • 1970-01-01
        • 2011-06-18
        • 2015-11-23
        相关资源
        最近更新 更多