• Spring邮箱发送


    package com.lhh.test;
    import java.util.Properties;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    public class Email {
    private String mailServer="smtp.qq.com";
    private String from="2694072078@qq.com";
    private String to="3385351592@qq.com";
    private String MailSubject="大白区块链";
    private String MailContent="你好";
    private String userName="2694072078";
    //qq邮箱key
    private String password="zojrqyuxvdpnddaexx";
    public void send(){
    try {
    Properties p=System.getProperties();
    p.put("mail.smtp.host", mailServer);
    p.put("mail.smtp.auth", "true");
    p.put("mail.smtp.ssl.enable", "true");

    EmailAuthentiactor authen=new EmailAuthentiactor(userName, password);

    Session session=Session.getInstance(p, authen);

    Message message=new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(MailSubject);
    message.setContent(MailContent,"text/html;charset=utf8");

    Transport t=session.getTransport("smtp");
    t.send(message,message.getAllRecipients());
    t.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    }

    package com.lhh.test;

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

    public class EmailAuthentiactor extends Authenticator{
    private String userName;
    private String password;
    public void setUserName(String userName) {
    this.userName = userName;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public EmailAuthentiactor(String userName, String password) {
    super();
    this.userName = userName;
    this.password = password;
    }
    public PasswordAuthentication getPasswordAuthentication(){
    return new PasswordAuthentication(userName, password);
    }
    }

    //test类

    package com.lhh.test;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Test {
    public static void main(String[] args) {
    ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
    /*EmailHtml e= (EmailHtml) app.getBean("mailHtml");
    e.send();*/
    //EmailAuthentiactor a=(EmailAuthentiactor) app.getBean("mailHtml");
    Email a=new Email();
    a.send();

    System.out.println("发送成功!");
    }
    }

  • 相关阅读:
    GitHub与Markdown(学习笔记)
    “Another git process seems to be running in this repository...”Git此问题解决
    Git学习笔记--实践(三)
    Git学习笔记--配置(二)
    Java 锁(学习笔记)
    Git学习笔记--历史与安装(一)
    Java 8中Stream API(学习笔记)
    Qt Creator的下载和安装
    获取 wx.getUserInfo 接口后续将不再出现授权弹窗,请注意升级(微信小程序开发)
    大型网站架构技术一览
  • 原文地址:https://www.cnblogs.com/nancheng/p/8820205.html
Copyright © 2020-2023  润新知