2017-01-24 6 views
0

테스트를 통해 이메일을 보내려고하면이 클래스를 주 클래스로 호출합니다.Java 생성자가 정의되지 않았습니다.

public class SendMail{ 
public SendMail(String fromMail,String tomail, Exception e) 
{ 
Properties props = new Properties(); 
props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.socketFactory.port", "465"); 
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.port", "465"); 
Session session = Session.getDefaultInstance(props, 
     new javax.mail.Authenticator() { 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication("[email protected]","testpw"); 
    } 
}); 
try { 
    Message message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(fromMail)); 
    message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(tomail)); 
    message.setSubject("xxxxx"); 
    message.setText("xxxxx"+ ExceptionUtils.getStackTrace(e)); 
    Transport.send(message); 
    System.out.println("Done"); 
} 
    catch (MessagingException ex) { 
    throw new RuntimeException(ex); 
} 
} 
public SendMail(String string, String line) { 

그래서 나는이 목록에있는 모든 이메일을 보내드립니다 있도록 txt 파일에서 이메일 목록을 당길 수 있도록, 내 다른 클래스에서이 전화를합니다. 그 코드는 다음과 같습니다

String fileName = "/xxx/xxx/xxx/text.txt"; 

    try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { 
    String line; 
while ((line = br.readLine()) !=null) { 
    new SendMail("[email protected]", line); 
} 

    } 
catch (IOException e) { 
    e.printStackTrace(); 
} 

문제는이 생성자 센드 (문자열, 문자열) 정의되지 않은 나에게 말하고 있다는 것입니다.

내 고유 코드가 작동하지만 하드 코딩 된 이메일 만 보낼 수 있습니다.

try{ 
} 
catch(Exception e) 
{ 
e.printStackTrace(); 
new SendMail("senderemail","reciveremail",e); } 
} 

왜 내 생성자를 허용하지 않습니까?

편집 : 제작자가 잘못되지 않았습니다. 나는 ~였다! 나는 그것이 작동하지 않을 것이고 나의 문제를 해결할 이유를 발견했다. 이것은 내가 지금 사용하고있는 코드입니다.

String fileName = "/Users/cdolan/Desktop/liness.txt"; 
String Email = ""; 

try(BufferedReader br = new BufferedReader(new FileReader(fileName)))  { 

    while ((Email = br.readLine()) !=null) 

    { 

<Test code here goes here> 

} 
} 

catch(Exception e) 
{ 
    e.printStackTrace(); 
    new SendMail("[email protected]",Email,e); } 
} 
+3

"문제는 SendMail (String, String) 생성자가 정의되지 않았 음을 알려주는 것입니다." 이는 아마도 SendMail (String, String) 생성자를 정의하지 않았기 때문일 것입니다. – resueman

답변

2

문제는 정의되지 않은 메서드를 호출 할 수 없도록 SendMail(String fromMail,String tomail, Exception e) 메서드 만 정의한다는 것입니다.

SendMail(String fromMail,String tomail,) 메서드를 정의하고 구현해야 호출 할 수 있습니다.

또 다른 질문은 : 어떤 이유로 입력 매개 변수로 예외를 전달해야합니까?

+0

내 용서를 용서해. 나는 이것에 대해 처음이지만 새로운 생성자를 만들 때마다 public SendMail (String, string, String line) {그것은 단순히 이메일을 보내지 않는다. 나는 조금 잃었다. – Christian

+0

신경 쓰지 마세요! 이메일을 보내려고 할 때 오류가 표시됩니까? –

+0

원본 코드에서 콘솔 로그를 전자 메일로 보내길 원할 경우 오류가있는 전자 메일 만 트리거합니다. 이 일을하려고 할 때 단순히 아무것도 보내지 않습니다. 내가 잘못된 장소에 생성자를 만들었습니까? 두 번째 문자열 (문자열 문자열, 문자열 줄)은 대괄호 뒤에 새로운 RuntimeException을 던진 후에 나온다. – Christian

0

지정한 생성자가 예외를 인수로 취합니다 (어떤 이유로 ??). 새 개체를 만들 때이 예외 인수가 없습니다.

관련 문제