導航:首頁 > 編程語言 > 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哪個好 瀏覽:457
安卓外放聲音怎麼解決 瀏覽:194
脈脈app干什麼用的 瀏覽:357
拽姐是哪個app 瀏覽:858
雲伺服器刪除了還有嗎 瀏覽:232
macbook可以用單片機嘛 瀏覽:307
南陽php招聘 瀏覽:814
去哪裡找按摩師很漂亮的app 瀏覽:818
86x99用簡便演算法計算 瀏覽:830
php截圖flash 瀏覽:274
卸載聯想app哪個好 瀏覽:721
php文字轉圖片 瀏覽:332
豆客後台怎麼加密碼 瀏覽:576
jpg轉換pdf破解版 瀏覽:979
php基礎書籍推薦 瀏覽:779
伺服器與外網不通如何驗證 瀏覽:353
電子版是不是就是文件夾 瀏覽:52
游戲屬性文件加密 瀏覽:464
如何讓安卓手機桌面圖標下移 瀏覽:530
ubuntuphp5環境搭建 瀏覽:101