导航:首页 > 编程语言 > javaauthenticator

javaauthenticator

发布时间:2022-08-27 20:29:06

java mail中Authenticator有什么作用,怎么用

java发送邮件的jar包.
JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。我们可以基于JavaMail开发出类似于Microsoft Outlook的应用程序。

㈡ 怎么在java中直接得到google authenticator 的6位的验证码,而不用第三方app来实现

public String get(){String str="";for (int i=0;i<6;i++) { int itmp = random.nextInt(26) + 65; char ctmp = (char)itmp; str+=ctmp; } return str;}

㈢ java如何获取网页中的文字

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

public class URLTest {
// 一个public方法,返回字符串,错误则返回"error open url"
public static String getContent(String strUrl) {
try {
URL url = new URL(strUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url
.openStream()));
String s = "";
StringBuffer sb = new StringBuffer("");
while ((s = br.readLine()) != null) {
sb.append(s + "/r/n");
}
br.close();
return sb.toString();
} catch (Exception e) {
return "error open url:" + strUrl;
}
}

public static void initProxy(String host, int port, final String username,
final String password) {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,
new String(password).toCharArray());
}
});
System.setProperty("http.proxyType", "4");
System.setProperty("http.proxyPort", Integer.toString(port));
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxySet", "true");
}

public static void main(String[] args) throws IOException {
String url = "https://www.jb51.net";
String proxy = "http://192.168.22.81";
int port = 80;
String username = "username";
String password = "password";
String curLine = "";
String content = "";
URL server = new URL(url);
initProxy(proxy, port, username, password);
HttpURLConnection connection = (HttpURLConnection) server
.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
while ((curLine = reader.readLine()) != null) {
content = content + curLine+ "/r/n";
}
System.out.println("content= " + content);
is.close();
System.out.println(getContent(url));
}
}

㈣ java 编译时出现cannot resolve symbol symbol:class PopupAuthenticator 问题是出在哪里呢

PopupAuthenticator在这个java文件中没有定义,也没有import。所以会报错。

㈤ java 发送邮件,内容是要从数据库中读取的数据并列成表格的状态发送出去

publicbooleansendTextMail(MailSenderInfomailInfo){
//判断是否需要身份认证
MyAuthenticatorauthenticator=null;
Propertiespro=mailInfo.getProperties();
if(mailInfo.isValidate()){
//如果需要身份认证,则创建一个密码验证器
authenticator=newMyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
//根据邮件会话属性和密码验证器构造一个发送邮件的session
SessionsendMailSession=null;


//sendMailSession=Session.getDefaultInstance(pro,authenticator);//获取默认可能报错
sendMailSession=Session.getInstance(pro,authenticator);//新创建一个session
if(sendMailSession==null){
System.out.println("无法获取邮件邮件Session");
}
try{
//根据session创建一个邮件消息
MessagemailMessage=newMimeMessage(sendMailSession);
//创建邮件发送者地址
Addressfrom=newInternetAddress(mailInfo.getFromAddress());
//设置邮件消息的发送者
mailMessage.setFrom(from);
//创建邮件的接收者地址,并设置到邮件消息中
Addressto=newInternetAddress(mailInfo.getToAddress());

mailMessage.setRecipient(Message.RecipientType.TO,to);

//设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
//设置邮件消息发送的时间
mailMessage.setSentDate(newDate());
//设置邮件消息的主要内容
StringmailContent=mailInfo.getContent();
mailMessage.setText(mailContent);//添加附件
//if(mailInfo.getAttachFileNames()!=null||mailInfo.getAttachFileNames().length>0){
//Multipartmp=newMimeMultipart();
//MimeBodyPartmbp=null;
//for(StringfileName:mailInfo.getAttachFileNames()){
//mbp=newMimeBodyPart();
//FileDataSourcefds=newFileDataSource(fileName);//得到数据源
//mbp.setDataHandler(newDataHandler(fds));//得到附件本身并至入BodyPart
//mbp.setFileName(fds.getName());//得到文件名同样至入BodyPart
//mp.addBodyPart(mbp);
//}
//mailMessage.setContent(mp);
//}

//发送邮件
Transport.send(mailMessage);
returntrue;
}catch(MessagingExceptionex){
ex.printStackTrace();

}
returnfalse;
}
publicclassMailSenderInfo{
//发送邮件的服务器的IP和端口
privateStringmailServerHost;
privateStringmailServerPort="25";
//邮件发送者的地址
privateStringfromAddress;
//邮件接收者的地址
privateStringtoAddress;
//登陆邮件发送服务器的用户名和密码
privateStringuserName;
privateStringpassword;
//是否需要身份验证
privatebooleanvalidate=false;
//邮件主题
privateStringsubject;
//邮件的文本内容
privateStringcontent;
//邮件附件的文件名
privateString[]attachFileNames;

//邮件抄送人

privateList<String>ccUserList;
/***//**
*获得邮件会话属性
*/
publicPropertiesgetProperties(){
Propertiesp=newProperties();
p.put("mail.smtp.host",this.mailServerHost);
p.put("mail.smtp.port",this.mailServerPort);
p.put("mail.smtp.auth",validate?"true":"false");
returnp;
}
publicStringgetMailServerHost(){
returnmailServerHost;
}
publicvoidsetMailServerHost(StringmailServerHost){
this.mailServerHost=mailServerHost;
}
publicStringgetMailServerPort(){
returnmailServerPort;
}
publicvoidsetMailServerPort(StringmailServerPort){
this.mailServerPort=mailServerPort;
}
publicbooleanisValidate(){
returnvalidate;
}
publicvoidsetValidate(booleanvalidate){
this.validate=validate;
}
publicString[]getAttachFileNames(){
returnattachFileNames;
}
publicvoidsetAttachFileNames(String[]fileNames){
this.attachFileNames=fileNames;
}
publicStringgetFromAddress(){
returnfromAddress;
}
publicvoidsetFromAddress(StringfromAddress){
this.fromAddress=fromAddress;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicStringgetToAddress(){
returntoAddress;
}
publicvoidsetToAddress(StringtoAddress){
this.toAddress=toAddress;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetSubject(){
returnsubject;
}
publicvoidsetSubject(Stringsubject){
this.subject=subject;
}
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(StringtextContent){
this.content=textContent;
}
publicList<String>getCcUserList(){
returnccUserList;
}
publicvoidsetCcUserList(List<String>ccUserList){
this.ccUserList=ccUserList;
}


}

publicstaticvoidmain(String[]args){
//这个类主要是设置邮件
MailSenderInfomailInfo=newMailSenderInfo();
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("[email protected]");
mailInfo.setPassword("zzzong0828");//您的邮箱密码
mailInfo.setFromAddress("");
//接受方信息
mailInfo.setToAddress("");
mailInfo.setSubject("邮箱标题");
mailInfo.setContent("设置邮箱内容如http://www.guihua.org中国桂花网是中国最大桂花网站==");

String[]files=newString[]{"D:/1.txt","D:/2.txt","D:/3.txt"};
mailInfo.setAttachFileNames(files);
//这个类主要来发送邮件
SimpleMailSendersms=newSimpleMailSender();
sms.sendTextMail(mailInfo);//发送文体格式
//sms.sendHtmlMail(mailInfo);//发送html格式
}



这样发

㈥ java邮件中验证的解释

JAVAMAIL结合邮件服务的时候 用的
Properties props = System.getProperties();// 创建Properties 对象
// 添加smtp服务器属性
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true"); // 需要验证
props.put("mail.transport.protocol", "smtp");
首先是先由这个props 对象 然后根据这个对象的host找到你要用的邮件服务 如果配163的域名 就找163的邮件服务 如果是本地的 就找本地的 然后打开Session

下一步就可以传 用户名和密码到邮件服务里面去了
returnnew PasswordAuthentication(userName, password); 就是这里

如果邮件服务里面有你的这个用户名 密码也是对的 就成功打开Session
// 定义邮件信息
MimeMessage message = new MimeMessage(session);
吧Session和邮件服务绑定
开始在 message里面写入你的邮件 。。。
session.getTransport("smtp").send(message); 最后 指定SMTP 的方式 吧你的邮件发送出去
整个过程就是这样 关于原理 你可以看你的发送邮件的类 一步一个过程 很容易就明白了

㈦ java mail中Authenticator这个类到底有什么作用

首先,要矫正你的一个错误,Authenticator这个类是在Java.net.*;中的。
Authenticator 类表示懂得如何获得网络连接验证的对象。通常,它通过提示用户输入信息来完成此操作。

㈧ Java收发邮件过程中具体的功能是怎么实现的

1.SMTP协议

用户连上邮件服务器后,要想给它发送一封电子邮件,需要遵循一定的通迅规则,SMTP协议就是用于定义这种通讯规则的。

因而,通常我们也把处理用户smtp请求(邮件发送请求)的邮件服务器称之为SMTP服务器。(25)

2.POP3协议

同样,用户若想从邮件服务器管理的电子邮箱中接收一封电子邮件的话,他连上邮件服务器后,也需要遵循一定的通迅格式,POP3协议用于定义这种通讯格式。

因而,通常我们也把处理用户pop3请求(邮件接收请求)的邮件服务器称之为POP3服务器。(110)


下图用于演示两帐户相互发送邮件的过程

packagecn.e.dlmu.send;

importjava.util.Properties;

importjavax.activation.DataHandler;
importjavax.activation.FileDataSource;
importjavax.mail.Message;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeBodyPart;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeMultipart;
importjavax.mail.internet.MimeUtility;

publicclassSendMail{

publicstaticvoidmain(String[]args)throwsException{



Propertiesprop=newProperties();
//连接的邮件服务器的主机名
prop.setProperty("mail.smtp.host","smtp.sina.com.cn");
//发送邮件的协议
prop.setProperty("mail.transport.protocol","smtp");
//是否向邮件服务器提交认证
prop.setProperty("mail.smtp.auth","true");

//创建session
Sessionsession=Session.getInstance(prop);
session.setDebug(true);
//得到transport
Transportts=session.getTransport();
//连接邮件服务器
ts.connect("smtp.sina.com.cn","[email protected]","xxxxx");
//发送邮件
MimeMessagemessage=createMessage(session);
ts.sendMessage(message,message.getAllRecipients());
ts.close();
}

(Sessionsession)throwsException{

MimeMessagemessage=newMimeMessage(session);

//设置邮件的基本信息
message.setFrom(newInternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO,newInternetAddress("[email protected]"));
message.setSubject("test");

//正文
MimeBodyParttext=newMimeBodyPart();
//设置charaset可以解决中文正文的乱码问题,内嵌可下载的图片
text.setContent("你好xxx,<imgsrc='c:/dog.jpg'/>测试成功!<br/><imgsrc='cid:aaa.jpg'/>","text/html;charset=gbk");
//图片1
MimeBodyPartimage=newMimeBodyPart();
image.setDataHandler(newDataHandler(newFileDataSource("src/88.jpg")));
image.setContentID("aaa.jpg");
//附件
MimeBodyPartattach=newMimeBodyPart();
DataHandlerdh=newDataHandler(newFileDataSource("src/javamail架包.jar"));
attach.setDataHandler(dh);
//解决文件中文乱码问题
attach.setFileName(MimeUtility.encodeText(dh.getName()));

//描述正文和图片的关系
MimeMultipartmp=newMimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(image);
mp.setSubType("related");

//描述正文和附件
MimeMultipartmp2=newMimeMultipart();
mp2.addBodyPart(attach);
//将正文封装为一个body
MimeBodyPartcontent=newMimeBodyPart();
content.setContent(mp);

mp2.addBodyPart(content);
mp2.setSubType("mixed");

message.setContent(mp2);
message.saveChanges();

returnmessage;
}

}

㈨ Java 中怎样在程序中设置代理服务器

importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.net.Authenticator;
importjava.net.HttpURLConnection;
importjava.net.InetSocketAddress;
importjava.net.PasswordAuthentication;
importjava.net.Proxy;
importjava.net.URL;

publicclassProxyDemo2{
publicstaticvoidmain(String[]args)throwsException{
URLurl=newURL("http://www.3lai8.com");
///创建代理服务器
InetSocketAddressaddr=newInetSocketAddress("192.168.0.254",8080);
//Proxyproxy=newProxy(Proxy.Type.SOCKS,addr);//Socket代理
Proxyproxy=newProxy(Proxy.Type.HTTP,addr);//http代理
Authenticator.setDefault(newMyAuthenticator("username","password"));//设置代理的用户和密码
HttpURLConnectionconnection=(HttpURLConnection)url.openConnection(proxy);//设置代理访问
InputStreamReaderin=newInputStreamReader(connection.getInputStream());
BufferedReaderreader=newBufferedReader(in);
while(true){
Strings=reader.readLine();
if(s!=null){
System.out.println(s);
}
}
}

{
privateStringuser="";
privateStringpassword="";

publicMyAuthenticator(Stringuser,Stringpassword){
this.user=user;
this.password=password;
}

(){
(user,password.toCharArray());
}
}

}

㈩ Exception in thread "Thread-0" java.lang.NoClassDefFoundError: javax/mail/Authenticator at MyEmai

楼主, java的编译原理是一样的, 用到的类必须告诉jvm在哪. jvm是通过classpath变量 和 相对目录层次去找相应的类. 没找到就会报NoClassDefFoundError 如果是报jre中的Class没找到 那么应该检查下环境变量, 如果是报自己写的Class 那么应该检查下目录层次是否是 import 中写的那样

当然也可以在 javac 使用 -classpath 设置类所在的路径, 但是一般是设置环境变量classpath
例子 : javac -classpath \C \exam\JavaTest.java

阅读全文

与javaauthenticator相关的资料

热点内容
卸载联想app哪个好 浏览:719
php文字转图片 浏览:328
豆客后台怎么加密码 浏览:574
jpg转换pdf破解版 浏览:978
php基础书籍推荐 浏览:775
服务器与外网不通如何验证 浏览:351
电子版是不是就是文件夹 浏览:50
游戏属性文件加密 浏览:462
如何让安卓手机桌面图标下移 浏览:528
ubuntuphp5环境搭建 浏览:99
赌瘾解压视频 浏览:917
晋城移动dns服务器地址 浏览:294
php开源文库系统 浏览:134
android记事本源码 浏览:407
安卓11小游戏怎么玩法 浏览:190
gif有损压缩 浏览:937
windows下安装linux命令操作 浏览:844
米家app怎么设置进门亮灯 浏览:652
任我行服务器为什么会影响截图 浏览:296
安卓留言板怎么删除 浏览:18