normal java send mail

In order to ensure that the mail is sent successfully, make sure that the mailbox has the POP3 service enabled.
write picture description here

Send text mail:


public class Email {
/**邮箱服务为:smtp.163.com,发送邮箱就必须是[email protected];接收邮箱随便*/
    private String host = "smtp.126.com";
    //发送邮件的固定端口
    private String port = "25";
    /**邮箱账号*/
    private String userName = "xxx";
    /**客户端授权密码非邮箱账号密码 这里我的是本人163的登录密码*/
    private String password = "xxx";
    /**发给谁*/
    private String to = "[email protected]";

    @Test
    public void sendTextMail() throws Exception
    {
        Properties pro = System.getProperties();
        pro.put("mail.smtp.host", host);
        pro.put("mail.smtp.port", port);
        pro.put("mail.smtp.auth", "true");

        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session sendMailSession = Session.getDefaultInstance(pro,
                new Authenticator()
                {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication()
                    {
                        return new PasswordAuthentication(userName, password);
                    }
                });
        // 根据session创建一个邮件消息
        Message mailMessage = new MimeMessage(sendMailSession);
        // 设置邮件消息的发送者
        mailMessage.setFrom(new InternetAddress(userName));
        // 创建邮件的接收者地址,并设置到邮件消息中
        mailMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
        // 设置邮件消息的主题
        mailMessage.setSubject("Test Email");
        // 设置邮件消息发送的时间
        mailMessage.setSentDate(new Date());
        // 设置邮件消息的主要内容
        mailMessage.setText("this is a test Text mail");
        // 发送邮件
        Transport.send(mailMessage);
    }


}

Send Html Mail

public class Email {
/**邮箱服务为:smtp.163.com,发送邮箱就必须是[email protected];接收邮箱随便*/
    private String host = "smtp.126.com";
    private String port = "25";
    /**邮箱账号*/
    private String userName = "xxx";
    /**客户端授权密码非邮箱账号密码 这里我的是本人163的登录密码*/
    private String password = "xxx";
    /**发给谁*/
    private String to = "[email protected]";

    @Test
    public void sendHtmlMail() throws Exception
    {
        Properties pro = System.getProperties();
        pro.put("mail.smtp.host", host);
        pro.put("mail.smtp.port", port);
        pro.put("mail.smtp.auth", "true");

        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session sendMailSession = Session.getDefaultInstance(pro,
                new Authenticator()
                {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication()
                    {
                        return new PasswordAuthentication(userName, password);
                    }
                });
        // 根据session创建一个邮件消息
        Message mailMessage = new MimeMessage(sendMailSession);
        // 设置邮件消息的发送者
        mailMessage.setFrom(new InternetAddress(userName));
        // 创建邮件的接收者地址,并设置到邮件消息中
        mailMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
        // 设置邮件消息的主题
        mailMessage.setSubject("html Email");
        // 设置邮件消息发送的时间
        mailMessage.setSentDate(new Date());
        //mutilPart是一个容器类,包含MimeBodyPart类型的对象
        Multipart multipart=new MimeMultipart();
        //创建包含html内容的MimeBodyPart
        BodyPart html=new MimeBodyPart();
        html.setContent(
                "<html><body><img src='http://avatar.csdn.net/A/C/A/1_jianggujin.jpg'/><div>this is a HTML email.</div></body></html>",
                "text/html; charset=utf-8");
        multipart.addBodyPart(html);

        // 设置邮件消息的主要内容
        mailMessage.setContent(multipart);
        // 发送邮件
        Transport.send(mailMessage);
    }


}

refer to:

http://blog.didispace.com/springbootmailsender/

https://blog.csdn.net/qq_32371887/article/details/72821291

https://blog.csdn.net/jianggujin/article/details/51253129

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325904435&siteId=291194637