java发邮件实例完整版(类+方法)

1://发送邮件 方法  thw
   public Object commandButtonEmail(Map dataSetMap, Object parameter)
        throws Exception {
      System.out
           .println("_____________________________开始__________________________________________");
      DataSet queryresult = (DataSet) dataSetMap.get("datasetSelected");
      List<PolicyEO> List = (List) queryresult.getRecords();  
      PolicySetMealBlhImpl policySetMealBlhImpl = new PolicySetMealBlhImpl();   
      MailSend MailSend = new MailSend(); 
      List<String> operatorList = new ArrayList<String>();
      
        
      //发送邮件验证
      for (PolicyEO PolicyEO : List) {// start
        String operator = "";
        String operatorP = "";
        String applicationStatus = "";

        Map<String, String> map = new HashMap<String, String>();
        List<Object> list = new ArrayList<Object>();
        StringBuffer content = new StringBuffer();
        String t = "<br>"; // 内容
        String youjianneirong = "邮件内容:";
        String sendEmail = "xxx@163.com";// 发件人
         String email = "xxx@163.com";// 收件人 
        String subject = "待核保业务告知";// 主题
        String fuhao = ",";
            String baodanhao="投保单号:";
            
        StringBuffer t1 = new StringBuffer(t);
        StringBuffer youjianneirong1 = new StringBuffer(youjianneirong);
        StringBuffer baodanhao1 = new StringBuffer(baodanhao);
        Transport ts = null;
        // 邮件内容 thw
        map.put("subject", subject);
        map.put("sendEmail", sendEmail);
        map.put("email", email);
        map.put("CCemail", "xxx@outlook.com");    //ai-db@ms-ins.com.cn /tuhanwen1993@outlook.com
        operatorList = policySetMealBlhImpl.getAuthorized();// 得到登陆人
        Object operatorO = operatorList.get(0);
        if (operatorO != null) {
           operator = operatorO.toString(); // 登陆人姓名
        }
        Object operatorPo = PolicyEO.getInputorName();
        if (operatorPo != null) {
           operatorP = operatorPo.toString(); // 勾选记录的登陆人姓名
        }
        Object applicationStatusO = PolicyEO.getApplicationStatus();
        if (applicationStatusO != null) {
           applicationStatus = applicationStatusO.toString();
        }
        if (operator != "" && operatorP != "" && applicationStatus != "") {
           if (operator.equals(operatorP)
                 && applicationStatus.equals("15")) {
              String applicationNo =PolicyEO.getApplicationNo(); //投保单号
              StringBuffer applicationNo1 = new StringBuffer(applicationNo);
              String branchCode = PolicyEO.getBranchCode();//分支机构代码
              StringBuffer branchCode1 = new StringBuffer(branchCode);
              StringBuffer daihebao = new StringBuffer("待核保");
              StringBuffer operator1 = new StringBuffer(operator);
              StringBuffer fuhao1 = new StringBuffer(fuhao);
              content.append(youjianneirong1).append(t1).append(t1).
              append(baodanhao1).append(applicationNo1).append(fuhao1).append(t1).
              Append:(daihebao).append(fuhao1).append(t1).
              append(branchCode1).append(fuhao1).append(t1).
              append(operator1).append(fuhao1);
              String Content = content.toString();
              map.put("content", Content);   
              list=MailSend.initMail(session, ts);      
                 Session a =(Session) list.get(0);
                 Transport b = (Transport)list.get(1);
                 MailSend.createAndSendMail(a, b, map);
           }
        }
        if (operator != "" && operatorP != "" && applicationStatus != "") {
           if (operator.equals(operatorP) && applicationStatus.equals("2")) {
              String applicationNo =PolicyEO.getApplicationNo(); //投保单号
           }
        }
      }// end
      System.out
           .println("_______________________________结束________________________________________");
      return null;

   }  

2  
/**
* 新建一个 公用类MailSend  发邮件
*
 */
package com.newtouch.motor.business.mail.blh.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
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 MailSend {

   /**
   * @param args
   * @return
   * @throws Exception
   */
   public List<Object> initMail(Session session , Transport ts) throws Exception {

      Properties prop = new Properties();
      prop.setProperty("mail.smtp.host", "smtp.163.com");
      prop.setProperty("mail.transport.protocol", "smtp");
      prop.setProperty("mail.smtp.auth", "true");
      // 使用JavaMail发送邮件的5个步骤
      // 1、创建session
      session = Session.getInstance(prop);
      // 开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
      session.setDebug(true);
      // 2、通过session得到transport对象
      ts = session.getTransport();
      // 3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,
      // 用户名和密码都通过验证之后才能够正常发送邮件给收件人。
      ts.connect("smtp.163.com","xxx@163.com","");
      List<Object> list = new ArrayList<Object>();
      list.add(session);
      list.add(ts);
      return list;
      
      
   }

   public void createAndSendMail(Session session , Transport ts, Map<String,String> map) throws Exception {
      // 4、创建邮件
      Message message = createMail(session,map);
      //ArrayList<Object> list = new ArrayList<Object>();
      //list = message.getAllRecipients();
      // 5、发送邮件
      ts.sendMessage(message, message.getAllRecipients());
      ts.close();
      System.out.println("邮件发送成功");
   }
   /**
   * @Method: createSimpleMail
   * @Description: 创建一封只包含文本的邮件
   * @Anthor:
   * 
    * @param session
   * @return
   * @throws Exception
   */
   public static MimeMessage createMail(Session session , Map<String,String> map)
        throws Exception {
      // 创建邮件对象
      MimeMessage message = new MimeMessage(session);
      // 指明邮件的发件人
      message.setFrom(new InternetAddress(map.get("sendEmail").toString()));
      // 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
      message.setRecipient(Message.RecipientType.TO, new InternetAddress(
           map.get("email").toString()));
      message.setRecipient(Message.RecipientType.CC, new InternetAddress(
           map.get("CCemail").toString()));
      // 邮件的标题
      message.setSubject(map.get("subject").toString());
      // 邮件的文本内容
      message.setContent(map.get("content").toString(), "text/html;charset=UTF-8");
      
      message.saveChanges();
      
      // 返回创建好的邮件对象
      return message;
   }
}
 

 

接各种微信小程序,app,网站(价格议+qq763517800)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值