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

    import java.io.UnsupportedEncodingException;
    import java.util.Date;
    import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    
    public class sendEmail {
        
        static int port = 587;
         
        static String server = "smtp.qq.com";//邮件服务器mail.cpip.net.cn
     
        static String from = "**";//发送者,显示的发件人名字
     
        static String user = "**";//发送者邮箱地址
     
        static String password = "**";//密码
        
        public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
            
            System.out.println("ceshi");
            
            sendEmail("***","哈哈哈哈","哈哈哈");//收件人
            System.out.println("ok");
        }
        
        public static void sendEmail(String email, String subject, String body) throws UnsupportedEncodingException {
            try {
                Properties props = new Properties();
                props.put("mail.smtp.host",server);
                props.put("mail.smtp.port",port);
                props.put("mail.smtp.auth","true"); 
                Transport transport = null;
                Session session = Session.getDefaultInstance(props, null);
                transport = session.getTransport("smtp");
                transport.connect(server, user, password);
                MimeMessage msg = new MimeMessage(session);
                msg.setSentDate(new Date());
                InternetAddress fromAddress = new InternetAddress(user,from,"UTF-8");
                msg.setFrom(fromAddress);
                InternetAddress[] toAddress = new InternetAddress[1];
                toAddress[0] = new InternetAddress(email);
                msg.setRecipients(Message.RecipientType.TO, toAddress);
                msg.setSubject(subject, "UTF-8");   
                msg.setText(body, "UTF-8");
                msg.saveChanges();
                transport.sendMessage(msg, msg.getAllRecipients());
                transport.close();
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    Vue--vue-Router
    Vue--vue中常用的ECMAScript6语法
    Vue-- vue-preview(图片查看器)的使用步骤:
    Vue--moment时间格式插件安装和使用
    Vue--通过button跳转到其他组件并携带id参数
    css3中的box-sizing属性的使用
    JS-取出字符串中重复次数最多的字符并输出
    JSON.parse()和JSON.stringify()用法
    sql server 获取当前日期前三十天的日期
    SQL SERVER查询本周数据,无数据补0
  • 原文地址:https://www.cnblogs.com/lishuo/p/5787686.html
Copyright © 2011-2022 走看看