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(); 
} 

問題は、コンストラクタのSendMail(文字列、文字列)が定義されていない私に言っていますということです。

オリジナルのコードが動作しますが、これではハードコードされた電子メールのみを送信できます。

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,)というメソッドを定義して実装する必要があります。

もう1つの質問があります。どのような理由で例外を入力パラメータとして渡す必要がありますか?

+0

私の無知を許して私はこれに新しいですが、私は新しいコンストラクタを作成するたびに、public SendMail(String、string、String line){それは単に電子メールを送信しません。私は少し失われています。 – Christian

+0

心配しないでください!電子メールを送信しようとするとエラーが表示されますか? –

+0

元のコードでは、コンソールログを電子メールに送信したがっているエラーで電子メールがトリガーされるだけです。私がこれをしようとしているとき、単に何も送信しない。私は間違った場所にコンストラクタを作成しましたか? 2番目の文字列(String string、String line)は、大括弧の後に新しいRuntimeExceptionをスローした後です。 – Christian

0

あなたが指定したコンストラクタは、引数として例外をとります(何らかの理由で??)。この例外引数は、新しいオブジェクトを作成するときには表示されません。

関連する問題