메시지를 보내 JavaWeb

 

우리는 타사 전자 메일 서버를 사용하여 메시지를 보낼 수 있습니다.

 

메일 전송 프로토콜은 일반적으로 두 가지가 같습니다 POP3 /를 SMTP의 IMAP /를 SMTP .

POP와 IMAP의 차이 : 운영 메일 클라이언트, 같은 당신이 POP를 사용하는 경우, 서버의 사서함에 동기화되지 않은 모바일 전자 메일, 읽은 등이, 당신은 IMAP를 사용하는 경우, 이러한 작업은 서버의 사서함에 동기화됩니다.

 

 

항아리는 두 개의 패킷을 필요로

  • javax.mail.jar
  • activation.jar을

 

 

QQ 메일 서버의 사용의 예는 메일을 보내

여기에 IMAP을 사용합니다. 설정에서 수 -> 계정 -> POP3 / IMAP / SMTP / 교환 / CardDAV를 / CalDAV를 서비스 구성.

@WebServlet ( "/ sendMailServlet" )
 공개  수업 SendMailServlet가 확장 HttpServlet을 {
     보호  무효 의 doPost (HttpServletRequest 요청, HttpServletResponse 응답)가 발생 ServletException을을, IOException가 { 
        속성 속성 = 속성 (); 
        properties.put ( "mail.transport.protocol", "SMTP"); // 连接协议 
        properties.put ( "mail.smtp.host", "smtp.qq.com"); // 邮箱服务器主机名 
        properties.put ( "mail.smtp.port", 465); // 端口号 
        properties.put ( "mail.smtp.auth"
        properties.put ( "true로", "mail.smtp.ssl.enable"); // 보안 접속 SSL을 사용할지 여부를 
        properties.put ( "true로" "해, mail.debug을"); // 콘솔에 정보를 표시할지 여부를 

        // 세션 객체를 가져 
        세션 = 세션 Session.getInstance (속성);
         // 가져 오기 메시지 오브젝트를 
        메시지 메시지 = 새로운 새로운 의 MimeMessage (세션) 

        은 try {
             // 설정 보낸 사람의 메일 주소 
            message.setFrom ( 새로운 새로운 InternetAddress ( " XXX의 @의 QQ .COM " ));
             // 받는 사람의 이메일 주소 설정 
            message.setRecipient을 (Message.RecipientType.TO, 새로운 새로운InternetAddress ( " [email protected] " )); 

            // 여러받는 배열의 형태로 기록
             // InternetAddress [] = {새로운 새 receiverArr InternetAddress ( "[email protected]")에서, 새로운 InternetAddress 새로운 ( "XXX qq.com @ "), 새로운 새로운 InternetAddress ("[email protected] ")};
             // message.setRecipients (Message.RecipientType.TO, receiverArr); 

            // 메시지 헤더 설정 
            message.setSubject (" 메시지 헤더를 " );
             // 메시지 내용 설정 
            message.setText ( " 메일 내용 " ) 

            // 우체부 개체를 얻을 
            전송 전송 = (Session.getTransport를);
            // 자신의 이메일 계정에 연결하고 두 번째 매개 변수는 인증 코드입니다
            transport.connect ( " [email protected] ", " xxxxxxxxxxx " );
            // 发送邮件
            transport.sendMessage (메시지, message.getAllRecipients ()); 
            transport.close (); 
        } 캐치 (MessagingException가 전자) { 
            e.printStackTrace (); 
        } 

    } 

    보호  공극 의 doGet (HttpServletRequest의 요청 HttpServletResponse를 대응) 발생 ServletException을을 IOException을 { 
        의 doPost (요청, 응답); 
    } 
}

 

빨간색 부분은 사용하는 수정합니다.

 

추천

출처www.cnblogs.com/chy18883701161/p/11448310.html