不能引用非最终变量密码
我正在编写一个用于从 java 发送邮件的独立代码。在此程序中,我在控制台上获取用户的所有信息。但这里的问题是身份验证部分。 我传递的用户名和密码实际上是发件人的邮件 ID 和密码。但它显示错误,不能引用非最终变量密码和发件人。
如果我最终完成,那么我就不能从用户那里拿走它。请帮帮我,我该怎么办?
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);
}
}
}
你的帮助对我来说非常宝贵。 提前致谢。
I am writing a standalone code for Sending mail from java. In this program I am taking all info by user on console . but here problem is with Authentication part.
I am passing user name and password which is actually mail id and passwrd of sender. but it is showing error that can.t refer to non final variable Password and From.
if I do it final then I can't take it from user. Plz help me what should I do?
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);
}
}
}
Your help would be precious for me.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,
final
关键字的作用在于,它使变量只能写入一次/只读。这也意味着内部类可以引用它们。您应该做的是修改读取信息的代码,如下所示:然后删除匿名
Authenticator
实例中的变量声明。现在它将读取我们刚刚在外部代码块中声明的最终变量。有道理吗?顺便问一下,您更习惯用 C 语言编程吗?看起来让你烦恼的是试图在开始逻辑之前声明所有变量,但这在 java 中根本没有必要。事实上,它通常会使您的代码更难阅读!
So the thing about the
final
keyword is that it makes your variables write-once/read-only. It also means that inner classes can reference them. What you should do is modify the code that reads the info as follows:Then remove the variable declarations in your anonymous
Authenticator
instance. It will now be reading the final variables we just declared in the outer code block. Make sense?By the way, are you more accustomed to programming in C? It looks like what's fouling you up is trying to have all your variables declared before starting in on your logic, but that's not at all necessary in java. In fact, it often makes your code harder to read!