이 기사에서는 참조를 위해 Java 이메일을 보내는 상세한 프로세스에 대해 설명합니다. 특정 내용은 다음과 같습니다
1. 이메일 계약
이메일 : SMTP (Simple Mail Transport Protocal)
이메일 리셉션 프로토콜 : POP3 (우체국 프로토 세일)
새로운 IMAP 프로토콜을 보내면 이메일을받을 수도 있습니다.
(단계별 서버와 상호 작용)
smtp :
2. 이메일 보내기 및 수신 과정 :
일반적으로 SMTP 및 POP3은 2 개의 서버 (호스트)입니다.
SMTP 메일 포트는 25입니다.
POP3 포트는 110입니다.
이메일 예제
1) Foxmail 설치 :
2) 이메일을 보낼 때 Base64 사용자 이름 및 비밀번호 인코딩
// base64 사용자 이름과 암호 인코딩 @Test public void base64 () {String name = "wj_leaf12345"; 문자열 pwd = "1qaz2wsx"; Base64encoder en = 새로운 Base64encoder (); name = en.encode (name.getBytes ()); pwd = en.encode (pwd.getBytes ()); System.err.println (이름); System.err.println (Pwd); }3) Java 코드를 통해 이메일을 보내십시오
Java로 이메일 보내기 새 패키지를 가져와야합니다.
Mail.jar Core 패키지 이메일 전송
Activation.jar 사용자 및 비밀번호를 암호화합니다.
Mail.jar에는 세 가지 핵심 클래스가 있습니다.
javax.mail.session은 메일 서버와의 세션을 말합니다. 전체 프로젝트에는 하나만 있습니다.
javax.mail.message (인터페이스) - 데이터 정보를 보내기 위해 준비하십시오.
Mimemessage- 데이터 정보 유형을 설정할 수 있습니다.
전송 메시지를 보내는 메소드가 있습니다.
1 단계 : 두 개의 항아리 패키지를 가져옵니다
2 단계 : 간단한 이메일을 보냅니다
public void sendmail ()은 예외를 던집니다 {// 1 단계 : 속성 객체를 표시하여 정보 속성을 넣을 수 있습니다. // prop.setProperty에 연결할 서버를 설정합니다 ( "mail.host", "smtp.126.com"); // prop.setProperty를 확인할지 여부를 설정합니다 ( "mail.smtp.auth", "true"); // 2 단계 : 사용자 이름과 암호 Authenticator Auth = new Authenticator () {// 사용자와 암호를 반환하는이 액세스 개체 public packessauthentication getPasswordAuthentication () {passwordAuthentication pa = new PasswordAuthentication ( "AAA", "SSS"); PA; }}; //// 2 단계 : 세션 객체 세션을 가져옵니다. 세션 = 세션 .getDefaultInstance (prop, auth); // 세션 세션의 디버그 모드를 설정합니다 .SetDeBug (true); // 3 단계 : 정보를 선언하면 MM1 = New Mimemessage (세션); // 4 단계 : = 새 InternetAddress에서 발신자 이메일 주소를 설정합니다 ( "[email protected]"); mm1.setfrom (from); // 5 단계 : 수신자 MM1.SetRecipient (ReceperType.to, New InternetAddress ( "[email protected]")를 설정합니다. MM1.SetRecipient (ReceperType.cc, New InternetAddress ( "[email protected]")); mm1.setRecipient (ReceperType.bcc, new InternetAddress ( "[email protected]")); // 6 단계 : 주제를 설정합니다. mm1.setContent ( "안녕하세요, 이것은 Java로 전송 된 이메일입니다. 다시 시도하십시오. // 7 단계 : 송신 .send (mm1); }3 단계 : v 하이퍼 연결과 함께 이메일을 보냅니다
mm1.setsubject ( "이것은 Java sfasdf3로 전송 된 이메일"); mm1.setContent ( "안녕하세요, 이것은 java로 보낸 이메일입니다. <a href = 'http : //www.baidu.com'> baidu </a>", "text/html; charset = utf-8"); // 7 단계 : 송신 .send (mm1);
4 단계 : 기호의 이메일
public void sendfile ()은 예외를 {properties p = new Properties (); P.SetProperty ( "mail.host", "smtp.163.com"); p.setProperty ( "mail.smtp.auth", "true"); 세션 s = session.getDefaultInstance (p, new authenticator () {@override public passwordauthentication getpasswordauthentication () {return new passwordauthentication ( "ww", "123");}}); S.SetDebug (True); // mimemessage 선언 mimemessage msg = new Mimemessage (s); msg.setfrom (new Internetaddress ( "[email protected]")); msg.setRecipient (RecepientType.to, New InternetAddress ( "[email protected]")); msg.setsubject ( "그림"); // 1 단계 : 다중 프로세싱 선언 부품 mimemultipart mm = new mimemultipart (); // 2 단계 : mimebodypart body1 = new MimebodyPart (); // 3 단계 : 문자 DataSource DS = New FileDatasource를 설정합니다 (새 파일 ( "./ IMG/A.JPG"); Datahandler DH = 새로운 Datahandler (DS); Body1. Setdatahandler (DH); // 이름은 set body1.setfilename (mimeUtility.encodetext ( "beauty.jpg"))이어야합니다. MimebodyPart body2 = new MimebodyPart (); // 3 단계 : 문자 DataSource DS2 = New FileDatasource (새 파일 ( "./ IMG/B.JPG")); Datahandler dh2 = 새로운 Datahandler (DS2); body2.setdatahandler (dh2); // 이름은 set body2.setfilename (mimeUtility.encodetext ( "beauty 2.jpg"))로 설정해야합니다. MimebodyPart Body3 = New MimeBodyPart (); // 3 단계 : 문자 DataSource DS3 = New FileDatasource (새 파일 ( "./ IMG/M.MP3")); Datahandler dh3 = 새로운 Datahandler (DS3); body3.setdatahandler (dh3); // 이름은 set body3.setfilename (mimeUtility.encodetext ( "end of the Century.mp3"))입니다. // body1을 mm mm mm.addbodypart (body1); mm.addbodypart (body2); mm.addbodypart (body3); msg.setContent (mm); // 송신 .send (msg); }위의 내용은이 기사에 관한 모든 것입니다. 모든 사람의 학습에 도움이되기를 바랍니다.