JavaMail使用阿里云企业版邮箱发送邮件

第一步:添加依赖

     <dependency>  
            <groupId>javax.mailgroupId>  
            <artifactId>mailartifactId>  
            <version>1.4version>  
     dependency>

第二步:复制粘贴、运行

/**
         * 发送邮件 (阿里云邮箱企业版)
         * 
         * @param fromEmail
         *            发送邮箱
         * @param toEmail
         *            接收邮箱
         * @param emailName
         *            阿里云邮箱登录名
         * @param emailPassword
         *            密码
         * @param title
         *            发送主题
         * @param centent
         *            发送内容
         * @throws Exception
         */
        public static void sendMail(String fromEmail, String toEmail, String emailName, String emailPassword, String title,
                String centent) throws Exception
        {
            Properties prop=new Properties();  
            prop.put("mail.host","smtp.mxhichina.com" );  
            prop.put("mail.transport.protocol", "smtp");  
            prop.put("mail.smtp.auth", "true");  
            Session session=Session.getInstance(prop);  
            session.setDebug(true);
            Transport ts=session.getTransport();  
            ts.connect(emailName, emailPassword);  
            Message message=new MimeMessage(session);  
            message.setFrom(new InternetAddress(fromEmail));  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));  
            message.setSubject(title);  
            message.setContent(centent, "text/html;charset=utf-8");    
            ts.sendMessage(message, message.getAllRecipients()); 
        }

你可能感兴趣的:(J2SE)