java邮件接口实现

来源:互联网 发布:linux wget 下载目录 编辑:程序博客网 时间:2024/06/02 04:19

最近公司在开发一个项目,用到了以前没接触的接口。现在在这里做下记录。

       1:引入jar包:mail.jar。

       2:配置邮件地址(mail.properties)

mail.smtp.auth = true

mail.smtp.host = smtp.sina.com
mail.transport.protocol = smtp
mail.user =
********@sina.com
mail.password =
*******

     3:邮件模板

package com.jyt.util;


import java.text.SimpleDateFormat;
import java.util.Date;


import javax.servlet.http.HttpServletRequest;


/**
 * 
 * @author ganjing
 * @date 2016年5月16日
   @Description: 邮件模板
 */
public class MailTempUtil {
/**

* @Description:找回密码邮件模板
* @param @return   
* @return String  
* @throws
* @author ganjing
* @date 2016年5月16日
*/
public static  String  getLostPasswordTemp(String username){
//生成随机码
String code = CheckCode.createCheckCodeNoFour();

String email = "";
email += "<div>亲爱的用户 "+username+":您好!</div>";
email += "<div>您收到这封这封电子邮件是因为您 (也可能是某人冒充您的名义) 申请了一个新的密码。假如这不是您本人所申请, 请不用理会这封电子邮件, 但是如果您持续收到这类的信件骚扰, 请您尽快联络管理员。</div>";   
email += "<div> 要使用新的密码, 请使用以下链接启用密码。</div>";  
//md5加密字符串
email += "<div><a href=www.baidu.com'</a></div>";       
email += "<div> 注意:请您在收到邮件1个小时内使用,否则该链接将会失效。</div>"; 
return email;
}
}

4:发送邮件

package com.jyt.util;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;


import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;


/**
 * 发送邮件的测试程序
 * 
 * @author lwq
 * 
 */
public class MailUtil {
final static String  mailUrl  =  "\\mail.properties";
final static Properties props = new Properties();//配置文件对象
static String rootPath=  "";  
/**

* @Description: 找回密码邮件
* @param @param InternetAddress
* @param @return   
* @return String  
* @throws
* @author ganjing
* @date 2016年5月13日
*/
    public static boolean getPasswordMail(String InternetAddress,String username){
    try {
    MimeMessage message = commonMailConfig(InternetAddress);
    // 设置邮件标题
    message.setSubject("**************");
            // 设置邮件的内容体
    String str = MailTempUtil.getLostPasswordTemp(username);
            message.setContent(str, "text/html;charset=UTF-8");
Transport.send(message);
       return true;
} catch (Exception e) {
e.printStackTrace();
return false;

    }
    /**
     * 
     * @Description: 邮件参数的设置
     * @param @param InternetAddress
     * @param @return   
     * @return MimeMessage  
     * @throws
     * @author ganjing
     * @date 2016年5月13日
     */
    public static MimeMessage commonMailConfig(String InternetAddress){
    MimeMessage message = null;
        // 发送邮件
        try {
            /*
             * 可用的属性: mail.store.protocol / mail.transport.protocol / mail.host /
             * mail.user / mail.from
             */
            // 表示SMTP发送邮件,需要进行身份验证
            rootPath = MailUtil.class.getResource("").toString();
String url = rootPath.substring(6, rootPath.length()-1).split("/com")[0];

props.load(new BufferedInputStream(new FileInputStream(url+mailUrl)));


            // 构建授权信息,用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息,创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            message = new MimeMessage(mailSession);
            // 设置发件人
            InternetAddress form = new InternetAddress(
                    props.getProperty("mail.user"));
            message.setFrom(form);


            // 设置收件人
            InternetAddress to = new InternetAddress(InternetAddress);
            message.setRecipient(RecipientType.TO, to);


            // 设置抄送
            InternetAddress cc = new InternetAddress(InternetAddress);
            message.setRecipient(RecipientType.CC, cc);


            // 设置密送,其他的收件人不能看到密送的邮件地址
            InternetAddress bcc = new InternetAddress(InternetAddress);
            message.setRecipient(RecipientType.CC, bcc);
            return message;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return message;
}
        
    }
 
    
    
}

1 0
原创粉丝点击