• 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     @Override
    15     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 block
    12             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         // 初始化props
    23         props.put("mail.transport.protocol", "smtp");
    24         props.put("mail.smtp.auth", "true");
    25         //qq是smtp.qq.com
    26         props.put("mail.smtp.host", smtpHostName);
    27         //ssl邮箱端口
    28         props.put("mail.smtp.socketFactory.port", 465);//465
    29         //开启ssl
    30         props.put("mail.smtp.starttls.enable","true");
    31         // 验证
    32         authenticator = new MailAuthenticator(username, password);
    33         // 创建session
    34         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 block
    41 //            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 args
    71      * @throws AddressException
    72      * @throws MessagingException
    73      */
    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 }
  • 相关阅读:
    C语言指针专题——如何理解指针
    C语言指针专题——序
    Java 第14章 字符串
    Java截图笔记
    Java 第五章 循环结构1
    Java 第六章 循环结构2
    Java 第13章 带参数的方法
    Java 第11章 类的无参方法
    Java 第四章 选择结构2
    Java 第三章 选择结构1
  • 原文地址:https://www.cnblogs.com/ppli/p/5662317.html
Copyright © 2020-2023  润新知