java发送邮件

java发送邮件


public static boolean sendEmail(){  

        try {  
        String host = "smtp.163.com";   //发件人使用发邮件的电子信箱服务器
            String from = "17623009513@163.com";    //发邮件的出发地(发件人的信箱)
            String to = "1790183406@qq.com";   //发邮件的目的地(收件人信箱)


            // Get system properties
            Properties props = System.getProperties();


            // Setup mail server
            props.put("mail.smtp.host", host);


            // Get session
            props.put("mail.smtp.auth", "true"); //这样才能通过验证


            MyAuthenticator myauth = new MyAuthenticator("17623009513@163.com", "qweasdzxc123");
            Session session = Session.getDefaultInstance(props, myauth);


//            session.setDebug(true);


            // Define message
            MimeMessage message = new MimeMessage(session);
           


            // Set the from address
            message.setFrom(new InternetAddress(from));


            // Set the to address
            message.addRecipient(Message.RecipientType.TO,
              new InternetAddress(to));


            // Set the subject
            message.setSubject("小汪汪你好!");


            // Set the content
            message.setText("这是用java写的发送电子邮件的测试程序!");


            message.saveChanges();


              for(int i=0;i<100;i++){
             System.out.println(i);
             Transport.send(message);
              }
              return true;  
            
          }
        catch(Exception e){  
            e.printStackTrace();  
            return false;  
        }  

    }  



下面是自己建的邮箱内容类

package aa;


import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


public class MyAuthenticator extends Authenticator{
String userName=null;   
    String password=null;   
          
     public MyAuthenticator(){   
     }   
     public MyAuthenticator(String username, String password) {    
         this.userName = username;    
         this.password = password;    
     }    
     protected PasswordAuthentication getPasswordAuthentication(){   
         return new PasswordAuthentication(userName, password);   
     } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值