从 Javalin 后端发送电子邮件

创建发送电子邮件的“联系我们”表单(通过 Gmail)
2017年8月6日 • 作者:David Åse阅读时间:~5 分钟
本教程的源代码可以在 GitHub上找到。请 fork/clone 并在阅读时查看。

依赖项

首先,我们需要创建一个带有一些依赖项的 Maven 项目:(→ 教程)

<dependencies>
    <dependency>
        <groupId>io.javalin</groupId>
        <artifactId>javalin-bundle</artifactId> <!-- For handling http-requests -->
        <version>6.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-email</artifactId> <!-- For sending emails -->
        <version>1.5</version>
    </dependency>
    <dependency>
        <groupId>com.j2html</groupId>
        <artifactId>j2html</artifactId> <!-- For creating HTML form -->
        <version>1.6.0</version>
    </dependency>
</dependencies>

设置后端

我们需要三个端点:GET ‘/‘和:POST ‘/contact-us’GET ‘/contact-us/success’

import io.javalin.Javalin;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;

import static io.javalin.apibuilder.ApiBuilder.get;
import static io.javalin.apibuilder.ApiBuilder.post;
import static j2html.TagCreator.br;
import static j2html.TagCreator.button;
import static j2html.TagCreator.form;
import static j2html.TagCreator.input;
import static j2html.TagCreator.textarea;

public class JavalinEmailExampleApp {

    public static void main(String[] args) {

        Javalin.create(config -> {
            config.router.apiBuilder(() -> {
                get("/", ctx -> ctx.html(
                    form().withAction("/contact-us").withMethod("post").with(
                        input().withName("subject").withPlaceholder("Subject"),
                        br(),
                        textarea().withName("message").withPlaceholder("Your message ..."),
                        br(),
                        button("Submit")
                    ).render()
                ));
                post("/contact-us", ctx -> {
                    Email email = new SimpleEmail();
                    email.setHostName("smtp.googlemail.com");
                    email.setSmtpPort(465);
                    email.setAuthenticator(new DefaultAuthenticator("YOUR_EMAIL", "YOUR_PASSWORD"));
                    email.setSSLOnConnect(true);
                    email.setFrom("YOUR_EMAIL");
                    email.setSubject(ctx.formParam("subject"));
                    email.setMsg(ctx.formParam("message"));
                    email.addTo("RECEIVING_EMAIL");
                    email.send(); // will throw email-exception if something is wrong
                    ctx.redirect("/contact-us/success");
                });
                get("/contact-us/success", ctx -> ctx.html("Your message was sent"));
            });
        }).start(7070);
    }

}

为了使上述代码能够工作,您需要做一些更改:

  • 更改YOUR_EMAIL为您的 Gmail 帐户(youremail@gmail.com
  • 更改YOUR_PASSWORD您的 Gmail 密码*
  • 更改RECEIVING_ADDRESS为您希望发送电子邮件的位置
  • *创建一个测试帐户而不是使用真实的 gmail 凭据可能是个好主意。

修改代码后,运行程序并转到 http://localhost:7000 。您将看到一个简单的无样式表单,其中包含一个输入字段、一个文本区域和一个按钮。填写表单并点击按钮即可测试您的电子邮件服务器。点击按钮后,您的浏览器将被重定向到/contact-us/success(如果电子邮件已发送)。

您发送的任何电子邮件都将显示在SentGmail 网络界面的文件夹中。

作者:Jeebiz  创建时间:2025-05-04 00:15
最后编辑:Jeebiz  更新时间:2025-05-04 00:55