メールボックスの検証--javamailの簡単な使用


今回のプロジェクトでは、メールボックスの検証を行い、パスワードを取り戻す問題に遭遇しました.これはjavamailコンポーネントを使用する必要があります.私たちは機能が簡単で、送信するだけで、添付ファイルがないので、コードも簡単です.
 
まずjavamailを導入したjarパッケージは添付ファイルを参照
 
 
送信詳細の設定
package mail;

import java.util.Properties;

public class MailSenderInfo {

	//          IP   
	private String mailServerHost;
	private String mailServerPort = "25";
	//         
	private String fromAddress;
	//         
	private String toAddress;
	//                 
	private String userName;
	private String password;
	//         
	private boolean validate = false;
	//     
	private String subject;
	//        
	private String content;
	//         
	private String[] attachFileNames;

	/**
	 *         
	 */
	public Properties getProperties() {
		Properties p = new Properties();
		p.put("mail.smtp.host", this.mailServerHost);
		p.put("mail.smtp.port", this.mailServerPort);
		p.put("mail.smtp.auth", validate ? "true" : "false");

		// gmail        :1:SSL   ; 2: 465  ;
		//   :http://www.blogjava.net/hwpok/archive/2009/03/20/261097.html
		if (this.mailServerHost.indexOf("smtp.gmail.com") >= 0) {
			p.setProperty("mail.smtp.socketFactory.class",
					"javax.net.ssl.SSLSocketFactory");
			p.setProperty("mail.smtp.socketFactory.fallback", "false");
			p.setProperty("mail.smtp.port", "465");
			p.setProperty("mail.smtp.socketFactory.port", "465");
		}
		return p;
	}

	public String getMailServerHost() {
		return mailServerHost;
	}

	public void setMailServerHost(String mailServerHost) {
		this.mailServerHost = mailServerHost;
	}

	public String getMailServerPort() {
		return mailServerPort;
	}

	public void setMailServerPort(String mailServerPort) {
		this.mailServerPort = mailServerPort;
	}

	public boolean isValidate() {
		return validate;
	}

	public void setValidate(boolean validate) {
		this.validate = validate;
	}

	public String[] getAttachFileNames() {
		return attachFileNames;
	}

	public void setAttachFileNames(String[] fileNames) {
		this.attachFileNames = fileNames;
	}

	public String getFromAddress() {
		return fromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String textContent) {
		this.content = textContent;
	}
}

 
2.送信者情報設定、口座とパスワード
package mail;
import javax.mail.*; 
public class MyAuthenticator extends Authenticator{   
    String userName=null;   
    String password=null;   
        
    public MyAuthenticator(){   
    }   
    public MyAuthenticator(String username, String password) {    
        this.userName = username;    
        this.password = password;    
    }    
    protected PasswordAuthentication getPasswordAuthentication(){   
        return new PasswordAuthentication(userName, password);   
    }   
}   
   

 3.メール送信エンティティ
 
package mail;
import java.util.Date;    
import java.util.Properties;   
import javax.mail.Address;    
import javax.mail.BodyPart;    
import javax.mail.Message;    
import javax.mail.MessagingException;    
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;   


public class SimpleMailSender {
	 
		/**   
		  *             
		  * @param mailInfo             
		  */    
		    public boolean sendTextMail(MailSenderInfo mailInfo) {    
		      //               
		      MyAuthenticator authenticator = null;    
		      Properties pro = mailInfo.getProperties();   
		      if (mailInfo.isValidate()) {    
		      //         ,              
		        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());    
		      }   
		      //                        session    
		      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);    
		      try {    
		      //   session            
		      Message mailMessage = new MimeMessage(sendMailSession);    
		      //              
		      Address from = new InternetAddress(mailInfo.getFromAddress());    
		      //               
		      mailMessage.setFrom(from);    
		      //           ,             
		      Address to = new InternetAddress(mailInfo.getToAddress());    
		      mailMessage.setRecipient(Message.RecipientType.TO,to);    
		      //              
		      mailMessage.setSubject(mailInfo.getSubject());    
		      //                
		      mailMessage.setSentDate(new Date());    
		      //                
		      String mailContent = mailInfo.getContent();    
		      mailMessage.setText(mailContent);    
		      //         
		      Transport.send(mailMessage);   
		      return true;    
		      } catch (MessagingException ex) {    
		          ex.printStackTrace();    
		      }    
		      return false;    
		    }    
		       
		    /**   
		      *  HTML         
		      * @param mailInfo            
		      */    
		    public static boolean sendHtmlMail(MailSenderInfo mailInfo){    
		      //               
		      MyAuthenticator authenticator = null;   
		      Properties pro = mailInfo.getProperties();   
		      //        ,               
		      if (mailInfo.isValidate()) {    
		        authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());   
		      }    
		      //                        session    
		      Session sendMailSession = Session.getDefaultInstance(pro,authenticator);    
		      try {    
		      //   session            
		      Message mailMessage = new MimeMessage(sendMailSession);    
		      //              
		      Address from = new InternetAddress(mailInfo.getFromAddress());    
		      //               
		      mailMessage.setFrom(from);    
		      //           ,             
		      Address to = new InternetAddress(mailInfo.getToAddress());    
		      // Message.RecipientType.TO           TO    
		      mailMessage.setRecipient(Message.RecipientType.TO,to);    
		      //              
		      mailMessage.setSubject(mailInfo.getSubject());    
		      //                
		      mailMessage.setSentDate(new Date());    
		      // MiniMultipart       ,  MimeBodyPart         
		      Multipart mainPart = new MimeMultipart();    
		      //       HTML   MimeBodyPart    
		      BodyPart html = new MimeBodyPart();    
		      //   HTML      
		      html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");    
		      mainPart.addBodyPart(html);    
		      //  MiniMultipart             
		      mailMessage.setContent(mainPart);    
		      //         
		      Transport.send(mailMessage);    
		      return true;    
		      } catch (MessagingException ex) {    
		          ex.printStackTrace();    
		      }    
		      return false;    
		    }    
	}



 
4.テスト使用
package mail;

public class MyMail {

	public static void main(String[] args) {
		//           
		MailSenderInfo mailInfo = new MailSenderInfo();

		//   163       
		mailInfo.setMailServerHost("smtp.163.com");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		mailInfo.setUserName("[email protected]");
		mailInfo.setPassword("*******");//       
		mailInfo.setFromAddress("[email protected]");
		mailInfo.setToAddress("[email protected]");


		mailInfo.setSubject("      ");
		mailInfo.setContent("just test");
		//           
		SimpleMailSender sms = new SimpleMailSender();
		sms.sendTextMail(mailInfo);//       
		// sms.sendHtmlMail(mailInfo);//   html  
		System.out.print("  ");
	}

}

 
 
注意、使用を要求するメールボックスはsmtpがあり、
接続できない場合は、ネットワークポートとファイアウォールsinaの無料メールボックスが使用できることに注意してください.