📪 可能是最小的 Java 邮件发送库了,支持抄送、附件、模板等功能。

Related tags

Spring Boot java email
Overview

oh-my-email

或许是最小的 Java 邮件发送类库了。

Build Status Codacy Badge codecov.io maven-central License Twitter URL

特性

  • 简洁的邮件发送API
  • 支持自定义发件人昵称
  • 支持扩展邮件Message
  • 支持抄送/HTML/附件
  • 支持异步发送
  • 支持邮件模板
  • 可能是代码量最小的库了,200多行 😂 非常好维护

使用

maven坐标

<dependency>
    <groupId>io.github.biezhi</groupId>
    <artifactId>oh-my-email</artifactId>
    <version>0.0.4</version>
</dependency>

举个栗子 🌰

@Before
public void before() throws GeneralSecurityException {
    // 配置,一次即可
    OhMyEmail.config(SMTP_QQ(), "[email protected]", "your@password");
}

@Test
public void testSendText() throws MessagingException {
    OhMyEmail.subject("这是一封测试TEXT邮件")
            .from("小姐姐的邮箱")
            .to("[email protected]")
            .text("信件内容")
            .send();
}

@Test
public void testSendHtml() throws MessagingException {
    OhMyEmail.subject("这是一封测试HTML邮件")
            .from("小姐姐的邮箱")
            .to("[email protected]")
            .html("<h1 font=red>信件内容</h1>")
            .send();
}

@Test
public void testSendAttach() throws MessagingException {
    OhMyEmail.subject("这是一封测试附件邮件")
            .from("小姐姐的邮箱")
            .to("[email protected]")
            .html("<h1 font=red>信件内容</h1>")
            .attach(new File("/Users/biezhi/Downloads/hello.jpeg"), "测试图片.jpeg")
            .send();
}

@Test
public void testSendAttachURL() throws MessagingException {
    try {
        OhMyEmail.subject("这是一封测试网络资源作为附件的邮件")
                .from("小姐姐的邮箱")
                .to("[email protected]")
                .html("<h1 font=red>信件内容</h1>")
                .attachURL(new URL("https://avatars1.githubusercontent.com/u/2784452?s=40&v=4"), "测试图片.jpeg")
                .send();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}


@Test
public void testPebble() throws IOException, PebbleException, MessagingException {
    PebbleEngine engine = new PebbleEngine.Builder().build();
    PebbleTemplate compiledTemplate = engine.getTemplate("register.html");

    Map<String, Object> context = new HashMap<String, Object>();
    context.put("username", "biezhi");
    context.put("email", "[email protected]");

    Writer writer = new StringWriter();
    compiledTemplate.evaluate(writer, context);

    String output = writer.toString();
    System.out.println(output);

    OhMyEmail.subject("这是一封测试Pebble模板邮件")
            .from("小姐姐的邮箱")
            .to("[email protected]")
            .html(output)
            .send();
}

@Test
public void testJetx() throws IOException, PebbleException, MessagingException {
    JetEngine engine = JetEngine.create();
    JetTemplate template = engine.getTemplate("/register.jetx");

    Map<String, Object> context = new HashMap<String, Object>();
    context.put("username", "biezhi");
    context.put("email", "[email protected]");
    context.put("url", "<a href='http://biezhi.me'>https://biezhi.me/active/asdkjajdasjdkaweoi</a>");

    StringWriter writer = new StringWriter();
    template.render(context, writer);
    String output = writer.toString();
    System.out.println(output);

    OhMyEmail.subject("这是一封测试Jetx模板邮件")
            .from("小姐姐的邮箱")
            .to("[email protected]")
            .html(output)
            .send();
}

邮件模版

<div>
    <p>亲爱的<b>{{ username }}</b>, 欢迎加入 biezhi !</p>
    <p>当您收到这封信的时候,您已经可以正常登录了。</p>
    <p>请点击链接登录首页: <a href='http://www.baidu.com'>http://biezhi.me/xxxxx</a></p>
    <p>如果您的 email 程序不支持链接点击,请将上面的地址拷贝至您的浏览器(如IE)的地址栏进入。</p>
    <p>如果您还想申请管理员权限,可以联系管理员 {{ email }}</p>
    <p>我们对您产生的不便,深表歉意。</p>
    <p>希望您在 biezhi 系统度过快乐的时光!</p>
    <p></p>
    <p>-----------------------</p>
    <p></p>
    <p>(这是一封自动产生的email,请勿回复。)</p>
</div>

问题建议

  • 我的邮箱:biezhi.me#gmail.com
You might also like...

Professional Java Developer Career Starter: Java Foundations Course Exercise Solutions

java-foundations-solutions Professional Java Developer Career Starter: Java Foundations Course Exercise Solutions The solutions are generally to be fo

Dec 28, 2022

Team 5468's 2022 FRC robot code. This code is written in Java and is based off of WPILib's Java control system and utilizes a command based system

FRC 2022 Team 5468's 2022 FRC robot code. This code is written in Java and is based off of WPILib's Java control system and utilizes a command based s

Oct 4, 2022

ijrd - internal java runtime debugger (loads through java agents LOL)

ijrd ijrd - internal java runtime debugger (loads through java agents LOL) this actually requires brain to build and then setup little guide to setup

Jan 28, 2022

Java - Packet Analyzer Application based on Java, Networking and Swing UI

Network-Packet-Tracer-using-Java Java - Packet Analyzer / Sniffing System Application based on Java, Networking and Swing UI Java - Packet Analyzer Ap

Feb 3, 2022

Java virtual machine made in Java by HoverCatz#0060

Java virtual machine made in Java by HoverCatz#0060

Obzcure Virtual Machine Java virtual machine made in Java THIS IS NOT PRODUCTION SAFE - WORK IN PROGRESS! Use at your own risk. Requires Java 17 (with

Oct 18, 2022

Create a Music Playlist Library -Core JAVA, JAVA Swing, AWT

Project Specifications Manage Everything about a basic Music Playlist Application in Java A Music Library for Listing, Replaying, Navigating between c

Nov 8, 2022

This repository contains Java programs to become zero to hero in Java. Programs related to each and every concep are present from easy to intermidiate level.

Learn Java Programming In this repository you will find topic wise programs of java from basics to intermediate. This follows topic wise approach that

Oct 9, 2022

Alibaba Cloud Dedicated KMS Transfer SDK for Java can help Java developers to migrate from the KMS keys to the Dedicated KMS keys.

Alibaba Cloud Dedicated KMS Transfer SDK for Java Alibaba Cloud Dedicated KMS Transfer SDK for Java can help Java developers to migrate from the KMS k

May 12, 2022

Resconstruct is a java library to infer missing information vectors of java classes.

Reconstruct Resconstruct is a java library to infer missing information vectors of java classes. Features Phantom classes Inheritance solving Dummy fi

Nov 17, 2022
Comments
  • send 方法报Read timed out

    send 方法报Read timed out

    异常背景,在定时钟中按照文档创建了发送方法,并在静态方法中设置了OhMyEmail.config,定时钟一天执行一次,第一次调用很正常,第二天就会报该异常,但是网络肯定是没有问题的,因为我手动重启后发送又恢复了,但是第二天还会报该异常。

    o.github.biezhi.ome.SendMailException: javax.mail.MessagingException: Exception reading response; 104002 nested exception is: 104003 java.net.SocketTimeoutException: Read timed out 104004 at io.github.biezhi.ome.OhMyEmail.send(OhMyEmail.java:309) ~[oh-my-email-0.0.4.jar!/:na] 104005 at com.example.philosophersstone.schedule.TradeDataSchedule.exportAndSendAiStockData(TradeDataSchedule.java:337) ~[classes!/:0.0.1-SNAPSHOT] 104006 at com.example.philosophersstone.schedule.TradeDataSchedule.stockAiTask(TradeDataSchedule.java:223) ~[classes!/:0.0.1-SNAPSHOT] 104007 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_302] 104008 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_302] 104009 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_302] 104010 at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_302] 104011 at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) [spring-context-5.3.9.jar!/:5.3.9] 104012 at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-5.3.9.jar!/:5.3.9] 104013 at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:95) [spring-context-5.3.9.jar!/:5.3.9] 104014 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_302] 104015 at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_302] 104016 at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_302] 104017 at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_302] 104018 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_302] 104019 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_302] 104020 at java.lang.Thread.run(Thread.java:748) [na:1.8.0_302] 104021 Caused by: javax.mail.MessagingException: Exception reading response 104022 at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2460) ~[javax.mail-1.6.2.jar!/:1.6.2] 104023 at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2187) ~[javax.mail-1.6.2.jar!/:1.6.2] 104024 at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740) ~[javax.mail-1.6.2.jar!/:1.6.2] 104025 at javax.mail.Service.connect(Service.java:388) ~[javax.mail-1.6.2.jar!/:1.6.2] 104026 at javax.mail.Service.connect(Service.java:246) ~[javax.mail-1.6.2.jar!/:1.6.2] 104027 at javax.mail.Service.connect(Service.java:195) ~[javax.mail-1.6.2.jar!/:1.6.2] 104028 at javax.mail.Transport.send0(Transport.java:254) ~[javax.mail-1.6.2.jar!/:1.6.2] 104029 at javax.mail.Transport.send(Transport.java:124) ~[javax.mail-1.6.2.jar!/:1.6.2] 104030 at io.github.biezhi.ome.OhMyEmail.send(OhMyEmail.java:307) ~[oh-my-email-0.0.4.jar!/:na] 104031 ... 16 common frames omitted 104032 Caused by: java.net.SocketTimeoutException: Read timed out 104033 at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.8.0_302] 104034 at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[na:1.8.0_302] 104035 at java.net.SocketInputStream.read(SocketInputStream.java:171) ~[na:1.8.0_302] 104036 at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_302] 104037 at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:464) ~[na:1.8.0_302] 104038 at sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68) ~[na:1.8.0_302] 104039 at sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1341) ~[na:1.8.0_302] 104040 at sun.security.ssl.SSLSocketImpl.access$300(SSLSocketImpl.java:73) ~[na:1.8.0_302] 104041 at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:957) ~[na:1.8.0_302] 104042 at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:126) ~[javax.mail-1.6.2.jar!/:1.6.2] 104043 at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) ~[na:1.8.0_302] 104044 at java.io.BufferedInputStream.read(BufferedInputStream.java:265) ~[na:1.8.0_302] 104045 at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:106) ~[javax.mail-1.6.2.jar!/:1.6.2] 104046 at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2440) ~[javax.mail-1.6.2.jar!/:1.6.2] 104047 ... 24 common frames omitted

    opened by fangxueshun 1
  • 升级 javax.mail:mail 依赖

    升级 javax.mail:mail 依赖

    项目 javax.mail:mail 已被弃用,并被移动到了 javax.mail:javax.mail-api,原先的项目仅因兼容性原因而保留。

    详见:

    • https://mvnrepository.com/artifact/javax.mail/mail
    • https://mvnrepository.com/artifact/com.sun.mail/javax.mail
    opened by andylizi 0
  • 关于邮件无法发送的问题

    关于邮件无法发送的问题

    我在测试的时候是可以发出去邮件的,可是在实际调用中却出现了拒绝链接 的bug

    2020-02-19 20:28:53.132 ERROR 1832 --- [nio-8990-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is io.github.biezhi.ome.SendMailException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
      nested exception is:
    	java.net.ConnectException: Connection refused (Connection refused)] with root cause
    
    java.net.ConnectException: Connection refused (Connection refused)
    

    是不是因为配置读不到而不能把send方法写成工具类去别的地方调用?

    opened by Allwayz 0
  • 批量发送报错

    批量发送报错

    io.github.biezhi.ome.SendMailException: javax.mail.internet.AddressException: Local address contains illegal character in string ``[xxx,xxx]''

    字符串替换的不对

    opened by hehan-wang 1
Releases(v0.0.4)
Owner
見える
見える
Java-Programs---For-Practice is one of the Java Programming Practice Series By Shaikh Minhaj ( minhaj-313 ). This Series will help you to level up your Programming Skills. This Java Programs are very much helpful for Beginners.

Java-Programs---For-Practice is one of the Java Programming Practice Series By Shaikh Minhaj ( minhaj-313 ). This Series will help you to level up your Programming Skills. This Java Programs are very much helpful for Beginners. If You Have any doubt or query you can ask me here or you can also ask me on My LinkedIn Profile

Shaikh Minhaj 3 Nov 8, 2022
(Java & React) Yazılım Geliştirici Yetiştirme Kampı Java kısmına ait yazılan kaynak kodlar ve ödev çalışmalarım.

JavaCamp Kamp sürecinde yazılan kaynak kodlar ve ödev çalışmalarım. Day 1 1)Intro Day 2 2)oopIntro 2.1)oopIntro ~ Homework Day 3 3)oopIntro2 3.1)inher

Melih Çelik 17 Jun 26, 2022
☁ Tencent Cloud IM Server SDK in Java | 腾讯云 IM 服务端 SDK Java 版

Tencent Cloud IM Server SDK in Java The Tencent Cloud IM Server SDK for Java enables Java developers to easily work with Tencent Cloud IM. Requirement

Doocs 64 Dec 23, 2022
Trust-java - Test Results Verification library for Java

TRUST - Test Results Verification library for Java The TRUST's primary goal is to provide the simple way of different test results verification. Gener

Serhii Shymkiv 2 Nov 19, 2017
Changelog-java-version - Eine Auflistung der Änderungen in Java aus Sicht eines Entwicklers.

Changelog Java Versions Eine Auflistung der Änderungen in Java aus Sicht eines Entwicklers. Inhaltsverzeichnis Zugehörige Präsentation Homepage des Au

Frank W. Rahn 1 Jan 4, 2022
Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

Bank Statement Analyzer Application that currently runs in terminal with the commands: javac Application.java java Application [file-name].csv GUI coming soon...

Hayden Hanson 0 May 21, 2022
Library for converting from one Java class to a dissimilar Java class with similar names based on the Bean convention

Beanmapper Beanmapper is a Java library for mapping dissimilar Java classes with similar names. The use cases for Beanmapper are the following: mappin

null 26 Nov 15, 2022
Free and 100% open source Progressive Java Runtime for modern Java™ deployments supported by a leading OpenJDK contributor

BellSoft Liberica JDK is a build of OpenJDK that is tested and verified to be compliant with the Java SE specification using OpenJDK Technology Compat

null 195 Dec 22, 2022
ActiveJ is an alternative Java platform built from the ground up. ActiveJ redefines web, high load, and cloud programming in Java, featuring ultimate performance and scalability!

Introduction ActiveJ is a full-featured modern Java platform, created from the ground up as an alternative to Spring/Micronauts/Netty/Jetty. It is des

ActiveJ LLC 579 Jan 7, 2023
Short Java programs for practice (OCP) Oracle Certified Professional Java SE 11

OCP-study Short Java programs to practice for (OCP) Oracle Certified Professional Java SE 11 Exam Google document with notes: https://docs.google.com/

Sabina Matjašič 1 May 24, 2022