如何从 Java Web 应用程序发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1920265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:31:35  来源:igfitidea点击:

How to send email from a java web app

javaemail

提问by Ankur

I have a java web application sitting on Tomcat/Apache.

我有一个基于 Tomcat/Apache 的 Java Web 应用程序。

I have a form which has to send email. What is the best way to get this working.

我有一个必须发送电子邮件的表格。让这个工作的最好方法是什么。

回答by sfussenegger

You should look at JavaMail API

你应该看看JavaMail API

Additionally, you may want to look at Fancymail, a small library to simplify usage of JavaMail API.

此外,您可能需要查看Fancymail,这是一个用于简化 JavaMail API 使用的小型库。

回答by Joel

Short and dirty copy-and-paste for sending a simple plain text mail message using javamail here

用于在此处使用 javamail 发送简单纯文本邮件消息的简短而肮脏的复制和粘贴

Tiny example of sending a plain text msg, using custom smtp host:

使用自定义 smtp 主机发送纯文本消息的小示例:

        Properties props = new Properties();
    props.put("mail.smtp.host", "your.mailhost.com");
    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(true);
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress("[email protected]"));
    msg.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress("[email protected]")});
    msg.setSubject("Subject Line");
    msg.setText("Text Body");
    Transport.send(msg);