2011-07-20 47 views
-1

我寫從Java發送郵件一個獨立的代碼。在這個程序中,我在控制檯上獲取用戶的所有信息。但這裏的問題與認證部分。 我傳遞的是用戶名和密碼,它實際上是發件人的郵件ID和密碼。但它顯示錯誤,can.t可能引用非最終變量Password和From。不能引用非最終變量密碼

如果我去做了最後的話,我無法把它從用戶。 Plz幫助我,我該怎麼做?

package mypackage; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendMailSSL { 
    public static void main(String[] args) throws IOException { 
     Properties props = new Properties(); 
     String host=""; 
     String port=""; 
     String s_port=""; 
     String to =""; 
     final String from=""; 
     final String password=""; 
     String subject=""; 
     String context=""; 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     System.out.println("CONFIGURATION.... "); 
     System.out.println("mail.smtp.host="); 
     host = in.readLine(); 
     System.out.println("mail.smtp.socketfactoryport="); 
     s_port=in.readLine(); 
     props.put("mail.smtp.host", host); 
     props.put("mail.debug", "true"); 
     props.put("mail.smtp.socketFactory.port", s_port); 
     props.put("mail.smtp.socketFactory.class", 
       "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.auth", "true"); 
     System.out.println("mail.smtp.port="); 
     port=in.readLine(); 
     props.put("mail.smtp.port", port); 

     System.out.println("AUTHENTICATION...."); 
     System.out.println("Username="); 
     from=in.readLine(); 
     System.out.println("Password="); 
     password = in.readLine(); 
     Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() 
     { 
       String from = ""; 
       String password=""; 
       protected PasswordAuthentication getPasswordAuthentication() 
       { 
        return new PasswordAuthentication(from,password); 
       } 
      }); 

     try { 
      System.out.println("Mail Sending Process.."); 
      System.out.println("To="); 
      to=in.readLine(); 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(from)); 
      message.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse(to)); 
      System.out.println("Subject="); 
      subject=in.readLine(); 
      message.setSubject(subject); 
      System.out.println("Context="); 
      context = in.readLine(); 
      message.setText(context); 

      Transport.send(message); 
      Transport.send(message); 

      System.out.println("Done"); 

     } catch (MessagingException e) { 
      System.out.println("in catch blk"); 
      throw new RuntimeException(e); 
     } 
    } 
} 

你的幫助對我來說是寶貴的。 在此先感謝。

回答

0

所以對final關鍵字的事情是,它使你的變量一次性寫入/只讀。這也意味着內部類可以引用它們。你應該做的是修改,讀取信息的代碼如下:

System.out.println("AUTHENTICATION...."); 
System.out.println("Username="); 
final String from = in.readLine(); 
System.out.println("Password="); 
final String password = in.readLine(); 

然後取出變量聲明在您的匿名Authenticator實例。現在將讀取我們在外部代碼塊中聲明的最終變量。合理?

順便問一下,你更習慣於編程用C?它看起來像是什麼污染你試圖讓你的邏輯開始之前聲明所有的變量,但這在java中並不是必要的。事實上,它經常會讓你的代碼更難閱讀!

+0

偉大的先生它現在正在工作.....非常感謝::) – Atul

+0

是你寫我更習慣於編程在C和新的Java – Atul

+0

很高興聽到它!如果答案有幫助,請考慮加註和/或接受答案。這讓我們這些回答問題的人感到高興。 ;) – stevevls

-1

你不能從一個匿名內部類中引用非final的變量。

您應該設置final String from到任何這使得在一成不變的,它不能改變,當匿名內部類實例化它知道什麼from值是這樣的,它知道它永遠無法改變它是指至。

然後,只需使用from變量,而無需重新宣佈它在匿名內部類。

相關問題