zoukankan      html  css  js  c++  java
  • Java发送邮件

    /**
     * 
     */
    package javaSocket;
    
    import java.util.Date;
    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    /**
     * @author Acheron
     *
     */
    public class EmailJ {
        private String from; 
        private String to;
        private String subject;
        private String body;
        private String password; //目前使用明文,此demo中不做加密处理
        /**
         * @return from
         */
        public String getFrom() {
            return from;
        }
        /**
         * @param from 要设置的 from
         */
        public void setFrom(String from) {
            this.from = from;
        }
        /**
         * @return to
         */
        public String getTo() {
            return to;
        }
        /**
         * @param to 要设置的 to
         */
        public void setTo(String to) {
            this.to = to;
        }
        /**
         * @return subject
         */
        public String getSubject() {
            return subject;
        }
        /**
         * @param subject 要设置的 subject
         */
        public void setSubject(String subject) {
            this.subject = subject;
        }
        /**
         * @return body
         */
        public String getBody() {
            return body;
        }
        /**
         * @param body 要设置的 body
         */
        public void setBody(String body) {
            this.body = body;
        }
        
        
        
        /**
         * @return password
         */
        public String getPassword() {
            return password;
        }
        /**
         * @param password 要设置的 password
         */
        public void setPassword(String password) {
            this.password = password;
        }
        public boolean sendEmail(){
            if ( getTo() == null || getFrom() == null || getBody() == null || getSubject() == null ) {
                return false;
            }
            
            
            try{
                
                Properties props = new Properties();
                props.put("username", getFrom()); //邮箱账户
                props.put("password",getPassword()); //邮箱密码
                props.put("mail.transport.protocol","smtp" ); //邮件协议
                props.put("mail.smtp.host", "smtp.163.com"); //协议主机
                props.put("mail.smtp.port","25"); //端口号
                
                Session mailSession = Session.getDefaultInstance(props);
                Message msg = new MimeMessage(mailSession);
                
                msg.setFrom( new InternetAddress(getFrom()));
                msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(getTo()));
                msg.setSentDate(new Date());
                msg.setSubject(getSubject());
                msg.setText(getBody());
                msg.saveChanges();
                
                Transport transport = mailSession.getTransport("smtp");
                transport.connect(props.getProperty("mail.smtp.host"), props.getProperty("username"), props.getProperty("password"));
                transport.sendMessage(msg, msg.getAllRecipients());
                transport.close();
                
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
        
        //测试邮件发送功能
        public static void main(String[] args) {
            EmailJ email = new EmailJ();
            email.setTo("****@163.com"); //设置接收方邮箱地址
            email.setFrom("****@163.com"); //设置发送方邮箱地址
            email.setPassword("****"); //发送方邮箱密码
            email.setSubject("北京夏天的胡思乱想"); //邮件的主题
            email.setBody("那里有个班干部,快跑"); //邮件的主体内容
            email.sendEmail();
        }
    }

    测试结果:

  • 相关阅读:
    Python socket
    JavaScript中的数组创建
    前端开发者常用的9个JavaScript图表库
    【JavaScript】回流(reflow)与重绘(repaint)
    JavaScript-数据类型和变量
    前端常见算法的JS实现
    CSS 小技巧
    calc() 计算CSS属性值
    面试时如何展示自己非技术方面的能力?
    React的diff算法(译文)
  • 原文地址:https://www.cnblogs.com/fangpengchengbupter/p/7405938.html
Copyright © 2011-2022 走看看