java 邮件操作

public  boolean sendMail(Map<String,String> senderInfo,String[] mail_to, String subject, String content)
    {
        try
        {
            // 获取系统环境
            Properties props = new Properties(); 
            Authenticator auth = new Email_Autherticator(senderInfo.get("account"),senderInfo.get("password"));
            
            props.put("mail.smtp.host", senderInfo.get("smtp"));
            props.put("mail.smtp.auth", "true");
            Session session = Session.getDefaultInstance(props, auth);
            // 设置session,和邮件服务器进行通讯。
            MimeMessage message = new MimeMessage(session);
            
            // fileEncode 当前文件编码,gb2312转换的编码
            // 获取当前文件编码
            String fileEncode = GetComputer.getPropertieByName("file.encoding");
            //content = new String(content.getBytes(fileEncode),"gb2312");
            //message.setContent(content, "text/html;charset=gb2312");
            message.setSubject(subject); // 设置邮件主题
            //message.setText(content); // 设置邮件正文
            message.setHeader("DropBox", "Authenticate"); // 设置邮件标题
            message.setSentDate(new Date()); // 设置邮件发送日期
            // 邮件地址,发件人name
            Address address = new InternetAddress(senderInfo.get("addr"), "MashupMail");
            message.setFrom(address); // 设置邮件发送者的地址
            
            // 设置邮件接收方的地址
            Address[] toAddress = new Address[mail_to.length]; 
            for(int i=0;i<mail_to.length;i++)
            {
                toAddress[i] = new InternetAddress(mail_to[i]);
            }
            
            // 设置要显示的收件人
            message.setRecipients(Message.RecipientType.TO, toAddress);
            
            MimeMultipart mm = new MimeMultipart(); 
            
            this.setMMPWithImgSource(mm);
            // 这句很重要,千万不要忘了
            mm.setSubType("related");
           
            // 新建一个存放信件内容的BodyPart对象
            BodyPart mdp = new MimeBodyPart(); 
            // 给BodyPart对象设置内容和格式/编码方式
            mdp.setContent(content.toString(), "text/html;charset=GBK");
            
            mm.addBodyPart(mdp);
            
            message.setContent(mm);
            Transport.send(message,toAddress); // 发送邮件
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }
/** 
    * 用来进行服务器对用户的认证 
    */  
   private class Email_Autherticator extends Authenticator  
   {  
       private String account;  
       private String password;  
       public Email_Autherticator(String account,String password)  
       {  
           super();  
           this.account = account;  
           this.password = password;  
       }  
         
       public PasswordAuthentication getPasswordAuthentication()  
       {  
           return new PasswordAuthentication(account,password);  
       }  
   }  
     
   /** 
    * 加入图片资源 
    * @param mm 
    * @throws MessagingException 
    */  
   private void setMMPWithImgSource(MimeMultipart mm) throws MessagingException  
   {  
       String path = Constants.HUDSON_ROOT_PATH + "/images/sonarImgs/";  
       FileDataSource blocker_fds = new FileDataSource(path+"BLOCKER.png");  
       FileDataSource critical_fds = new FileDataSource(path+"CRITICAL.png");  
       FileDataSource info_fds = new FileDataSource(path+"INFO.png");  
       FileDataSource major_fds = new FileDataSource(path+"MAJOR.png");  
       FileDataSource minor_fds = new FileDataSource(path+"MINOR.png");  
         
       mm.addBodyPart(getMbp(blocker_fds, "BLOCKER"));  
       mm.addBodyPart(getMbp(critical_fds, "CRITICAL"));  
       mm.addBodyPart(getMbp(info_fds, "INFO"));  
       mm.addBodyPart(getMbp(major_fds, "MAJOR"));  
       mm.addBodyPart(getMbp(minor_fds, "MINOR"));         
   }  
     
   private MimeBodyPart getMbp(FileDataSource fds, String imgId) throws MessagingException  
   {  
       MimeBodyPart mbp = new MimeBodyPart();  
       mbp.setDataHandler(new DataHandler(fds));  
       mbp.setContentID(imgId);  
       return mbp;  
   }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值