你的位置:首页 > Java教程

[Java教程]javamail 利用qq邮箱做邮箱服务器,简单小demo

首先maven:

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

 

用户名密码验证:

 1 public class MailAuthenticator extends Authenticator{ 2    3   //邮箱账号 4   private String username; 5    6   //邮箱密码 7   private String password; 8    9   public MailAuthenticator(String username,String password){10     this.username=username;11     this.password=password;12   }13   14   @Override15   protected PasswordAuthentication getPasswordAuthentication() {16     return new PasswordAuthentication(username, password);17   }18   19   public String getUsername() {20     return username;21   }22   public void setUsername(String username) {23     this.username = username;24   }25   public String getPassword() {26     return password;27   }28   public void setPassword(String password) {29     this.password = password;30   }31 }

 1 public class SimpleMailSender { 2   private final transient Properties props = System.getProperties(); 3   private transient MailAuthenticator authenticator; 4   private transient Session session; 5    6   public SimpleMailSender(final String smtpHostName, final String username, 7       final String password){ 8     try { 9       init(username, password, smtpHostName);10     } catch (NoSuchProviderException e) {11       // TODO Auto-generated catch block12       e.printStackTrace();13     }14   }15   public SimpleMailSender(final String username, final String password) throws NoSuchProviderException {16     //通过邮箱地址解析出smtp服务器,对大多数邮箱都管用17     final String smtpHostName = "smtp." + username.split("@")[1];18     init(username, password, smtpHostName);19   20     }21   private void init(String username, String password, String smtpHostName) throws NoSuchProviderException {22     // 初始化props23     props.put("mail.transport.protocol", "smtp");24     props.put("mail.smtp.auth", "true");25     //qq是smtp.qq.com26     props.put("mail.smtp.host", smtpHostName);27     //ssl邮箱端口28     props.put("mail.smtp.socketFactory.port", 465);//46529     //开启ssl30     props.put("mail.smtp.starttls.enable","true");31     // 验证32     authenticator = new MailAuthenticator(username, password);33     // 创建session34     session = Session.getInstance(props, authenticator);35     session.setDebug(true);36 //    Transport transport = session.getTransport();37 //    try {38 //      transport.connect("smtp.qq.com", 25, "530486639@qq.com", "llg9004_d");39 //    } catch (MessagingException e) {40 //      // TODO Auto-generated catch block41 //      e.printStackTrace();42 //    }43   }44   public void send(String recipient, String subject, Object content)45       throws AddressException, MessagingException {46     // 创建mime类型邮件47     final MimeMessage message = new MimeMessage(session);48     // 设置发信人49     message.setFrom(new InternetAddress(authenticator.getUsername()));50     // 设置收件人51     message.setRecipient(RecipientType.TO, new InternetAddress(recipient));52     // 设置主题53     message.setSubject(subject);54     // 设置邮件内容55     System.out.println();56     message.setContent(content.toString(), "text/html;charset=utf-8");57     // 发送58     Transport.send(message);59     }60 //  public void send(String recipient, SimpleMail mail)61 //      throws AddressException, MessagingException {62 //    send(recipient, mail.getSubject(), mail.getContent());63 //    }64   65   /**66    * 1.重点现在客户端掉http端口25,腾讯好像不让掉了,必须https调用67    * 2.腾讯使用这种方式连接的话,需要重新申请独立密码,不是qq邮箱的密码,还需开启pop/smtp功能68    * 3.用腾讯的邮局发邮件好像有限制,发送过多,好像直接给你连接断开了69    * 4.如果需要做发邮件的功能的话,最好自己搭建邮局70    * @param args71    * @throws AddressException72    * @throws MessagingException73   */74   public static void main(String[] args) throws AddressException, MessagingException {75     SimpleMailSender sms= new SimpleMailSender("10000@qq.com",76         "dfvasdasdasd");77     //发送过多的话会断开连接78 //    for(int i=0;i<100;i++){79       sms.send("11111@qq.com", "hello", "hello");80 //      System.out.println("#######:"+i);81 //    }82   }83 }