使用阿里云邮箱发送邮件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LONG729564606/article/details/79353130

代码部分

/**
     * 发送邮件 (阿里云邮箱企业版)
     *
     * @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());
    }

所需依赖

   <dependency>  
            <groupId>javax.mail</groupId>  
            <artifactId>mail</artifactId>  
            <version>1.4</version>  
     </dependency>

猜你喜欢

转载自blog.csdn.net/LONG729564606/article/details/79353130
今日推荐