Java代码电子邮件无法使用密码

时间:2017-07-14 05:55:31

标签: java email

如何修复此问题,以下代码用于发送电子邮件 - 我收到如下错误消息,我希望java代码只是向我的Outlook帐户发送电子邮件

  

DEBUG:setDebug:JavaMail 1.4.3版

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
javax.mail.AuthenticationFailedException: failed to connect, no password specified?
    at javax.mail.Service.connect(Service.java:325)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at JavaMailTest.main(JavaMailTest.java:45)

failed to connect, no password specified?

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMailTest {
    public static void main(String[] args) {
        String host="host";  
        final String user="username@domain.com";//change accordingly  
        String to="username@domain.com";//change accordingly  

        //Get the session object  
        Properties props = new Properties();  
        props.put("mail.smtp.host",host);  
        props.put("mail.smtp.auth", "false");

        Session session=Session.getDefaultInstance(props, null);
        session.setDebug(true);

        //Compose the message  
        try {
            MimeMessage message = new MimeMessage(session);
            message.saveChanges();
            message.setFrom(new InternetAddress(user));  
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
            message.setSubject("Test mail");  
            message.setText("This is test mail.");  

            //send the message
            Transport.send(message);

            System.out.println("message sent successfully...");
        }
        catch (MessagingException e) {e.printStackTrace();}

    }
}

1 个答案:

答案 0 :(得分:0)

身份验证策略由SMTP服务器设置。大多数服务器不允许在没有身份验证的情您需要与系统管理员联系以验证这一点。