加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Javamail在公??司中使用outlook

发布时间:2020-12-15 04:18:55 所属栏目:Java 来源:网络整理
导读:我第一次使用 javamail并且有一些例外,我不明白我在其他问题中也看到了一些错误,但是它们的答案对我没有帮助.这是我的代码. final String username = "imsan1@cdcpk.com"; final String password = "**********"; Properties props = System.getProperties()
我第一次使用 javamail并且有一些例外,我不明白我在其他问题中也看到了一些错误,但是它们的答案对我没有帮助.这是我的代码.

final String username = "imsan1@cdcpk.com";
        final String password = "**********";
        Properties props = System.getProperties();
        props.put("mail.smtp.auth","true");
        props.put("mail.smtp.host","10.1.136.26");
        props.put("mail.smtp.port","25");
        props.put( "mail.smtp.user",username );
        props.put( "mail.smtp.password",password );

        Session session = Session.getInstance(props,new SmtpAuthenticator(username,password)
          );


        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("imsan1@cdcpk.com"));
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("immni1@cdcpk.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "nn No spam to my email,please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

SmtpAuthenticator

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


class SmtpAuthenticator extends Authenticator {
     String user;
     String pw;
     public SmtpAuthenticator (String username,String password)
     {
        super();
        this.user = username;
        this.pw = password;
     }
    public PasswordAuthentication getPasswordAuthentication()
    {
       return new PasswordAuthentication(user,pw);
    }
}

错误日志是

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 250-CDC-HO-CAS1.cdcpk.com Hello [10.1.34.74]
250-SIZE 37748736
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 XRDST

    at org.cdc.eipo.bean.investorsetup.EmailController.main(EmailController.java:64)
Caused by: javax.mail.AuthenticationFailedException: 250-CDC-HO-CAS1.cdcpk.com Hello [10.1.34.74]
250-SIZE 37748736
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 XRDST

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
    at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at org.cdc.eipo.bean.investorsetup.EmailController.main(EmailController.java:59)

任何帮助非常感谢

解决方法

代码工作正常它给出错误的原因是因为我没有获得SMTP服务器的权限,因此显示异常,在配置访问后发送邮件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读