Java使用gmail发送邮件

配置

在使用gmail邮箱发送邮件之前需要先开启相关的配置项

依赖

这里使用jodd工具类库来实现邮件的发送,jodd是一个轻量级的java常用操作类库,包括字符串处理、http请求、邮件发送、html解析等功能,有java工具的瑞士军刀美称,详情可以访问官网了解,https://jodd.org/

  <dependency> 
    <groupId>org.jodd</groupId>  
    <artifactId>jodd-all</artifactId>  
    <version>5.0.0</version> 
  </dependency>  
  <dependency> 
    <groupId>javax.mail</groupId>  
    <artifactId>mail</artifactId>  
    <version>1.4.5</version> 
  </dependency>  
  <dependency> 
    <groupId>com.sun.mail</groupId>  
    <artifactId>javax.mail</artifactId>  
    <version>1.5.4</version> 
  </dependency> 

demo

SmtpServer smtpServer = MailServer.create()
            .ssl(true)
            .host("smtp.gmail.com")
            .auth("my@gmail.com", "password")
            .buildSmtpMailServer();

Email email = Email.create()
        .from("my@gmail.com")
        .to("my@qq.com")
        .subject("hello")
        .textMessage("Hello world!");

SendMailSession session = smtpServer.createSession();
session.open();     
session.sendMail(email);
session.close();
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容