使用javamail 测试163发送邮件动能时
在测试发送邮件时与几个问题
代码如下:
- public void sendMail() {
- String host = "smtp.163.com";
- String from = "*****@163.com";
- String to = "*****@163.com";
- String userName = "*******";
- String pwd = "***************";
-
- Properties properties = new Properties();
- properties.put("mail.smtp.host", host);
- properties.put("mail.smtp.auth", "true");
-
- Session session = Session.getDefaultInstance(properties);
- session.setDebug(true);
-
- MimeMessage message = new MimeMessage(session);
- try {
- message.setFrom(new InternetAddress(from));
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
- message.setSubject("just a test .....");
- message.setText("this is content...");
- message.saveChanges();
-
- Transport transport = session.getTransport("smtp");
- transport.connect(host, userName, pwd);
- transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
- System.out.println("Send ok ......");
-
- } catch (AddressException e) {
-
- e.printStackTrace();
- }catch (MessagingException e) {
-
- e.printStackTrace();
- }
运行程序出现问题:
550 User has no permission
网上查了下 是 说 smtp,pop3 等 没开启。
手机认证后开启服务,并给力一个客户端授权码。
然后在测试程序,又有错误:
535 Error: authentication failed
又在网上查了下,最最终找到问题,开启smtp,pop3后,客户端登陆邮箱的话 要使用客户端授权码,将程序中的密码改成客户端授权码,程序运行正常