2012-04-04 2 views
4

좋아, 어떻게 해야할지 모르겠습니다. 일주일 전이 코드를 작성하고 테스트했을 때이 코드는 완벽하게 정상적으로 작동했습니다. 그런 다음 프로그램에 삽입하여 계속 예외가 발생한다는 사실을 깨달았습니다. 모든 것이 정상적으로 보인다. 보낸 사람 주소는 합법적입니다. 그것을 테스트하기 위해 사용한 수신자 주소는 합법적입니다. 뭐가 잘못 되었 니? 그래서 좌절 해요 :JavaMail - 보낸 사람 주소가 거부되었습니다. 액세스가 거부되었습니다.

private String outgoingMailServer = "smtp.mail.yahoo.com"; 

boolean debug = true; 

      //set the host outgoing mail smtp server. 
      Properties properties = new Properties(); 
      properties.put("mail.smtp.host", outgoingMailServer); 
      properties.put("mail.smtp.auth", "true"); 

      Authenticator authenticator = new SMTPAuthentication(); 
      Session session = Session.getDefaultInstance(properties, authenticator); 

      session.setDebug(debug); 

      //create a message session 
      Message msg = new MimeMessage(session); 

      //set the addresses, to and from 
      InternetAddress fromAddress; 
      fromAddress = new InternetAddress(emailFromAddress); 
      msg.setFrom(fromAddress); 

      //since mail can be sent to more than one recipient, create loop 
      //to add all addresses into InternetAddress, addressTo. 
      //InternetAddress[] toAddress = new InternetAddress[recipients.length]; 
      InternetAddress[] toAddress = new InternetAddress[recipients.size()]; 
      for (int i = 0; i < recipients.size(); i++) { 
       toAddress[i] = new InternetAddress(recipients.get(i)); 
      } 
      msg.setRecipients(Message.RecipientType.TO, toAddress); 

      //set the subject and content type 
      msg.setSubject(emailSubject); 
      msg.setContent(actualMessage, "text/html; charset=utf-8"); 

      //send the email 
      Transport.send(msg); 

예외는 따라서이다 :

javax.mail.SendFailedException: Invalid Addresses; 
    nested exception is: 
    com.sun.mail.smtp.SMTPAddressFailedException: 554 5.7.1 <[email protected]>: Sender address rejected: Access denied 

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1835) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1098) 
    at javax.mail.Transport.send0(Transport.java:195) 
    at javax.mail.Transport.send(Transport.java:124) 
    at internalLogicEngine.LogicEngine.sendReminder(LogicEngine.java:4282) 
    at testPackage.Test.main(Test.java:169) 
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 554 5.7.1 <[email protected]>: Sender address rejected: Access denied 

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1733) 
    ... 5 more 

어떤 도움이 가장 극명하게 될 것이다. 감사!

답변

4

마지막으로 (작동하는 데 사용되는 코드로보고, 처음부터 문제가 왜 아직도 이해할 수 있지만. 어쨌든 ...)-를 해결하려면 알아 냈

private String outgoingMailServer = "smtp.mail.yahoo.com";  
boolean debug = false; 

//set the host outgoing mail smtp server. 
Properties properties = new Properties(); 
properties.put("mail.smtp.host", outgoingMailServer); 
properties.put("mail.smtps.auth", "true"); 

Authenticator authenticator = new SMTPAuthentication(); 
Session session = Session.getDefaultInstance(properties, authenticator); 
session.setDebug(debug); 

//create a message session 
Message msg = new MimeMessage(session); 

//set the addresses, to and from 
InternetAddress fromAddress; 
fromAddress = new InternetAddress(emailFromAddress); 
msg.setFrom(fromAddress); 

//since mail can be sent to more than one recipient, create loop 
//to add all addresses into InternetAddress, addressTo. 
//InternetAddress[] toAddress = new InternetAddress[recipients.length]; 
InternetAddress[] toAddress = new InternetAddress[recipients.size()]; 
for (int i = 0; i < recipients.size(); i++) { 
    toAddress[i] = new InternetAddress(recipients.get(i)); 
} 
msg.setRecipients(Message.RecipientType.TO, toAddress); 

//set the subject and content type 
msg.setSubject(emailSubject); 
msg.setContent(actualMessage, "text/html; charset=utf-8"); 

//send the email 
Transport transport = session.getTransport("smtps"); 
transport.connect(outgoingMailServer, 465, emailUserName, emailPassword); 
transport.sendMessage(msg, msg.getAllRecipients()); 
transport.close(); 

//email sent 
//note, this does not necessarily mean the email was delivered. The 
//sysetm has no control over that 
emailSent = true; 

당신에게 ' LL 질문의 코드와이 사람 사이의 주요 차이점은 찾을 :

Transport.send(msg); 

Transport transport = session.getTransport("smtps"); 
transport.connect(outgoingMailServer, 465, emailUserName, emailPassword); 
transport.sendMessage(msg, msg.getAllRecipients()); 
transport.close(); 

밝혀졌습니다. 적절한 자격 증명 (포트 번호, 사용자 이름, 암호 및 메일 서버)을 사용하여 Transport 개체를 만들고 연결해야했습니다.

또한, 나는 제거 프로세스를했고, 같은 당신이 가지고있는이 있음을 발견 :

Authenticator authenticator = new SMTPAuthentication(); 
Session session = Session.getDefaultInstance(properties, authenticator); 

위뿐만 아니라 수 :

Transport transport = session.getTransport("smtps"); 
transport.connect(outgoingMailServer, 465, emailUserName, emailPassword); 
transport.sendMessage(msg, msg.getAllRecipients()); 
transport.close(); 

이 필요하지 않습니다 :

Session session = Session.getDefaultInstance(properties, null); 

어쨌든, 그 대답입니다. gmail에 대한이 대답을 변경할 수도 있습니다. 보내는 메일 서버를 gmail로 변경하고 전자 메일 주소, 사용자 이름 및 암호를 변경하십시오.

관련 문제