이 기사는 Java Web STMP의 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
공개 클래스 mailfilesendutils {개인 속성 소품; // 시스템 속성 개인 세션 세션; // 메일 세션 객체 개인 mimemessage mimemsg; // Mime Mail Object Private Multipart MP; // Multipart 객체, 이메일 내용, 제목, 첨부 및 기타 컨텐츠가 모두 추가 된 다음 MimemEsage 객체가 생성됩니다./** * 생성자 * @param */public mailfilesendutils () {props = system.getProperties (); props.put ( "mail.smtp.auth", "false"); 세션 = 세션 .getDefaultInstance (props, null); 세션 .SetDeBug (true); mimemsg = 새로운 mimemessage (세션); mp = 새로운 mimemultipart (); } /** * 생성자 * @param smtp mail sendutils (문자열 smtp, 문자열 사용자 이름, 문자열 암호) {props = system.getProperties (); props.put ( "mail.smtp.auth", "true"); props.put ( "mail.smtp.host", smtp); props.put ( "사용자 이름", 사용자 이름); props.put ( "비밀번호", 비밀번호); 세션 = 세션 .getDefaultInstance (props, null); 세션 .SetDeBug (true); mimemsg = 새로운 mimemessage (세션); mp = 새로운 mimemultipart (); } /*** 이메일 보내기* /public boolean sendmail (문자열에서, 문자열 [] to, 문자열 주제, 문자열 내용, 문자열 filename) {try {// 발신자 mimemsg.setfrom (new InternetAddress (from)); // (int i = 0; i <to.length; i ++) {mimemsg.setRecipients (message.recipientType.to, InternetAddress.parse (to [i]))에 대한 수신자를 설정합니다. } // cc // for (int i = 0; i <copyto.length; i ++) {// mimemsg.setRecipients (message.recipienttype.cc, internetAddress.parse (copyto [i])); //} // 주제를 설정하여 mimemsg.setsubject (주제); // 텍스트 설정 BodyPart BP = New MimeBodyPart (); bp.setContent (content, "text/html; charset = utf-8"); mp.addbodypart (bp); // 첨부 설정 설정 bp = new MimeBodyPart (); FileDataSource 파일 = New FileDatasource (Filename); bp.setdatahandler (New Datahandler (제출)); bp.setfilename (mimeUtility.encodetext (fileds.getName (), "utf-8", "b")); mp.addbodypart (bp); mimemsg.setContent (mp); mimemsg.sevechanges (); // if (props.get ( "mail.smtp.auth"). Equals ( "true")) {전송 전송 = session.getTransport ( "smtp"); ranittin.connect ((문자열) props.get ( "mail.smtp.host"), (string) props.get ( "username"), (string) props.get ( "password")); 송신 .sendMessage (mimemsg, mimemsg.getRecipients (message.recipienttype.to)); // rangplity.sendmessage (mimemsg, mimemsg.getRecipients (message.recipienttype.cc)); 송신 .close (); } else {운송 .send (mimemsg); } system.out.println ( "성공적인 메일 보내기"); } catch (messagingException e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } catch (UnsupportedEncodingException e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } true를 반환합니다. } // public void tosendmail (sendmailparam sendmailparam) {// mailfilesendutils email = new mailfilesendutils (sendmailparam.getsmtp (), sendmailparam.getusername (), sendmailparam.getpassword ()); // email.sendmail () sendmailparam.getSubject (), sendmailparam.getContent (), sendMailParam.getFilePath ()); //} public static void main (String [] args) {String smtp = "smtp.exmail.qq.com"; 문자열 username = "이메일 계정 보내기"; 문자열 비밀번호 = "전자 메일 비밀번호 보내기"; 문자열에서 "이메일 보내기"; String [] to = { "이메일을 받기위한 이메일 주소"}; // string [] copyto = { "CCC 이메일 주소"}; 문자열 주제 = "주제 6"; 문자열 내용 = "메일 컨텐츠 6"; 문자열 filename = "첨부 파일"; mailfilesendutils email = new MailFilesEndutils (smtp, username, password); // email.sendmail (from, to, to, to, intured, content, filename); email.sendmail (From, ~, 주제, 내용, 파일 이름); }}(첨부 파일 : SSL 버전)
공개 클래스 mailfilesendutils {개인 속성 소품; // 시스템 속성 개인 세션 세션; // 메일 세션 객체 개인 mimemessage mimemsg; // Mime Mail Object Private Multipart MP; // Multipart 객체, 이메일 내용, 제목, 첨부 및 기타 컨텐츠가 모두 추가 된 다음 MimemEsage 객체가 생성됩니다./** * 생성자 * @param */public mailfilesendutils () {props = system.getProperties (); props.put ( "mail.smtp.auth", "false"); 세션 = 세션 .getDefaultInstance (props, null); 세션 .SetDeBug (true); mimemsg = 새로운 mimemessage (세션); mp = 새로운 mimemultipart (); } /** * 생성자 * @param smtp mail sendutils (문자열 smtp, 문자열 사용자 이름, 문자열 암호) {security.addprovider (new com.sun.net.ssl.internal.ssl.provider ()); 최종 문자열 ssl_factory = "javax.net.ssl.sslsocketfactory"; props = system.getProperties (); MailsslSocketFactory SF = NULL; try {sf = new MailsSlSocketFactory (); } catch (GeneralsEcurityException e) {} sf.setTrustAllHosts (true); props.put ( "mail.smtp.auth", "true"); props.put ( "mail.smtp.host", smtp); props.put ( "mail.smtp.socketfactory.class", ssl_factory); props.put ( "mail.smtp.socketfactory.fallback", "false"); props.put ( "mail.smtp.ssl.enable", "true"); props.put ( "mail.smtp.port", "465"); props.put ( "mail.smtp.ssl.socketfactory", sf); // props.put ( "username", username); // props.put ( "password", password); Session = session.getInstance (propss, new authenticator () {@override prossected passwordauthentication getPasswordAuthentication () {새 passwordAuthentication (username, password);}}); 세션 .SetDeBug (true); mimemsg = 새로운 mimemessage (세션); mp = 새로운 mimemultipart (); } /*** 이메일 보내기* /public boolean sendmail (문자열에서, 문자열 [] to, 문자열 주제, 문자열 내용, 문자열 filename) {try {// 발신자 mimemsg.setfrom (new InternetAddress (from)); // 수신기를 설정합니다 (int i = 0; i <to.length; i ++) {mimemsg.setRecipients (message.recipientType.to, InternetAddress.parse (to [i])); } // cc // for (int i = 0; i <copyto.length; i ++) {// mimemsg.setrecipients (message.recipienttype.cc, internetAddress.parse (copyto [i])); //} // set topic mimemsg.setsubject (subject); // 텍스트 설정 BodyPart BP = New MimeBodyPart (); bp.setContent (content, "text/html; charset = utf-8"); mp.addbodypart (bp); // 첨부 설정 설정 bp = new MimeBodyPart (); FileDataSource 파일 = New FileDatasource (Filename); bp.setdatahandler (New Datahandler (제출)); bp.setfilename (mimeUtility.encodetext (fileds.getName (), "utf-8", "b")); mp.addbodypart (bp); mimemsg.setContent (mp); mimemsg.sevechanges (); // if (props.get ( "mail.smtp.auth"). Equals ( "true")) {전송 전송 = session.getTransport ( "smtp"); ranittin.connect ((문자열) props.get ( "mail.smtp.host"), (string) props.get ( "username"), (string) props.get ( "password")); 송신 .sendMessage (mimemsg, mimemsg.getRecipients (message.recipienttype.to)); // ranittin.SendMessage (mimemsg, mimemsg.getRecipients (message.recipienttype.cc)); 송신 .close (); } else {운송 .send (mimemsg); } system.out.println ( "메일이 성공적으로 전송"); } catch (MessagingException e) {e.printstacktrace (); } catch (UnsupportedEncodingException e) {e.printstacktrace (); } true를 반환합니다. } public boolean tosendmail (sendmailparam sendmailparam) {mailfilesendutils email = new mailfilesendutils (sendmailparam.getsmtp (), sendmailparam.getusername (), sendmailparam.getpassword ()); email.sendmail (sendmailparam.getfrom (), sendmailparam.getto (), sendmailparam.getSubject (), sendmailparam.getContent (), sendMailParam.getFilePath ()); 진실을 반환하십시오. } // public static void main (string [] args) {// string smtp = "smtp.mxhichina.com"; // String username = "emailbox"; // string password = "email password"; // string from = "send"; // string [] to = "send to"; // scound "; "huawei"; // string content = "이메일 내용 6666"; // string filename = "gdt-3583118353-ad-ad-ad-ad-ad-ad-ad-ad-ad-ad-filesendutils email = new mailfilesendutils (smtp, username, password); // conflice (from,, coply,,,, coply,,,, coply); email.sendmail (부터, 주제, 내용, 파일 이름); //}} 이 프로젝트에서 나는 주요 방법을 언급 한 다음 tosendmail (sendmailparam sendmailparam)을 사용합니다.
여기에 정의 된 sendmailparam은 다음과 같습니다.
공개 클래스 sendmailparam {private String smtp; 개인 문자열 사용자 이름; 개인 문자열 비밀번호; ; // sender private string [] to; // string [] copyright = { "[email protected]"}; 개인 문자열 주제; // 이메일 주제 개인 문자열 내용; // 이메일 내용 개인 문자열 filepath; // 파일을 가져 오는 경로 public sendmailparam () {this.smtp = "smtp.exmail.qq.com"; // example this.username = "이메일 계정"; this.password = "이메일 비밀번호"; this.from = "이메일"; this.subject = ""; this.content = ""; this.filepath = ""; } public String getSmtp () {return smtp; } public void setsmtp (문자열 smtp) {this.smtp = smtp; } public String getUserName () {return username; } public void setusername (String username) {this.username = username; } public String getPassword () {return password; } public void setpassword (문자열 비밀번호) {this.password = password; } public String getFrom () {return from; } public void setfrom (문자열) {this.from = from; } public String [] getto () {return to; } public void setto (string [] to) {this.to = to; } public string getSubject () {return subject; } public void setSubject (문자열 주제) {this.subject = subject; } public String getContent () {return 컨텐츠; } public void setContent (문자열 내용) {this.content = content; } public String getFilePath () {return filepath; } public void setFilePath (String FilePath) {this.FilePath = FilePath; }}Maven 의존성 패키지
<pectionency> <groupid> javax.mail </groupid> <artifactid> mail </artifactid> <bersion> 1.4.7 </version> </fectionency>
Gradle 종속성 패키지
"javax.mail : mail : 1.4.7"를 컴파일합니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.