2016-11-17 10 views
0

SMTP経由でメールを送信しようとしているときに、以下の行の構文を取得しています。私はgoogleで試しましたが、関連する回答は得られませんでした。property.setProperty( "mail.smtp.host"、host)で構文エラーが発生しました。

property.setProperty( "mail.smtp.host"、host);

CODE:

public class SendMail { 

//Recipient Mail id 
String to = "Receiver Mail ID"; 
//Sender Mail Id 
String from = "Sender Mail ID"; 
//Sending email from the localhost 
String host = "localhost"; 
//Get System Properties 
Properties property = System.getProperties(); 
//Setup the mail server 
property.setProperty("mail.smtp.host",host); 
property.setProperty("mail.smtp.port" , "465"); 
//property.put("mail.smtp.auth", "true"); 
//Get the default session object 
Session session = Session.getDefaultInstance(property); 


try 
{ 
    //Create a default MimeMessage object. 
    MimeMessage message = new MimeMessage(session); 
    //Set From: header field of the header 
    message.setFrom(new InternetAddress(from)); 
    //Set To : header field of the header 
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 

    //Set Subject : header field 
    message.setSubject("Automation Testing Report"); 

    //Create the Message part 
    BodyPart messageBodypart = new MimeBodyPart(); 

    //Enter the message in the Mail Body 
    messageBodypart.setText("***********Find the below for the Report****************"); 

    //Create a Multipart Message 
    Multipart multipart = new MimeMultipart(); 

    //Set Text message part 
    multipart.addBodyPart(messageBodypart); 


    //Part two is attachment 
    /*create the Message part*/ 
    messageBodypart = new MimeBodyPart(); 
    String filename = "E:\\Project\\jar\\Selenium  Scripts\\Hybrid_Driven\\test-output\\emailable-report.html"; 
    DataSource source = new FileDataSource(filename); 
    messageBodypart.setDataHandler(new DataHandler(source)); 
    messageBodypart.setFileName(filename); 
    //set the text message part 
    multipart.addBodyPart(messageBodypart); 

    //Send the complete message part 
    message.setContent(multipart); 


    //Send message 
    Transport.send(message); 
    System.out.println("Mail has sent successfully"); 

} 

catch(MessagingException mex) 
{ 
    mex.printStackTrace(); 
} 

} 

} 

私はこの問題を解決するのに役立ちます。

答えて

1

クラスに直接Javaコードを挿入することはできません。メソッド内にある必要があります。以下はコンパイラがうれしく受け入れている:

public class SendMail { 

    ... 

    //Get System Properties 
    java.util.Properties property = System.getProperties(); 

    void doSomething() 
    { 
    property.setProperty("mail.smtp.host", host); 
    property.setProperty("mail.smtp.port", "465"); 
    ... 

    } 
} 
+0

ああ、それは修正されました。しかし、その理由は何ですか?私たちはクラス定義でそれを言及すべきではありませんか? –

+0

クラス定義はJavaでコードを実行するためのものではありません。これは、他の言語では異なるようです。スカラ。しかし、スカラーのクラス定義のコードは暗黙的にデフォルトのコンストラクターの一部です。基本的に、クラス定義は、クラスが持つメンバフィールドとメソッドを定義します。コンストラクタとメソッドは、これらのフィールドとメソッドを使用して作業することができます –

+0

説明をありがとうございます。このクラスを実行してもう1つは出力として電子メールを受け取っていません.i実行中かどうかわかりません。私はPOMモデルを使用しています –

関連する問題