jsp 메일 발송 실현

5988 단어
프로그램에 454 Authentication failed가 나타나면 please open smtp flag first!오류, 그러면 일반적으로 메일박스가 POP3/sMTP 서비스를 개통하지 않고 메일박스에 로그인하여 설정 중에 이 서비스를 켜면 됩니다!
또 필요한jar 패키지는 다음과 같다:imap.jar, mail.jar, smtp.jar, 인터넷에서 직접 다운로드할 수 있습니다.
package com.hzml.serve;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/MailResponse.servlet")
public class MailResponse extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public MailResponse() {
        super();
    }
    
    private void process(HttpServletRequest request, HttpServletResponse response) {
        try{
            String pwd = "hjzgg5211314";//       
             String mailfrom = request.getParameter("emailFrom"); //     
             String wangyiFrom = mailfrom.substring(0, mailfrom.indexOf('@'));//        
            String tu = "163.com"; //        
            String tto= request.getParameter("emailTo"); //       
            String ttitle= "           !";
            String tcontent = request.getParameter("messageTo");
    
            Properties props=new Properties();
    
            props.put("mail.smtp.host","smtp."+tu);//  SMTP       
    
            props.put("mail.smtp.auth","true");//        
    
            Session s=Session.getInstance(props);
    
            s.setDebug(true);
    
            MimeMessage message=new MimeMessage(s);
    
            //          /   /  /    
    
            InternetAddress from=new InternetAddress(mailfrom); //    qq  
    
            message.setFrom(from);
    
            InternetAddress to=new InternetAddress(tto);//      
    
            message.setRecipient(Message.RecipientType.TO,to);
    
            message.setSubject(ttitle);
    
            message.setSentDate(new Date());
    
            //         
    
            BodyPart mdp=new MimeBodyPart();//           BodyPart  
    
            mdp.setContent(tcontent,"text/html;charset=gb2312");// BodyPart         /    
    
            Multipart mm=new MimeMultipart();//    MimeMultipart      BodyPart 
    
            // (         )
    
            mm.addBodyPart(mdp);// BodyPart   MimeMultipart   (      BodyPart)
    
            message.setContent(mm);// mm         
    
            message.saveChanges();
    
            Transport transport=s.getTransport("smtp");
    
            transport.connect("smtp."+tu, wangyiFrom, pwd); //   wangyiFrom        
    
            transport.sendMessage(message,message.getAllRecipients());
    
            transport.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        process(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        process(request, response);
    }

}

좋은 웹페이지 즐겨찾기