如何从 jsp/servlet 发送电子邮件? [英] How to send an email from jsp/servlet?

查看:41
本文介绍了如何从 jsp/servlet 发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 JSP/servlet 发送电子邮件?是否需要下载一些jar包,或者您可以从没有任何jar包的JSP/servlets发送电子邮件吗?

How to send an email from JSP/servlet? Is it necessary to download some jars or can you send an email from JSP/servlets without any jars?

  • 我的 Java 代码会是什么样子?

  • What would my Java code look like?

我的 HTML 代码会是什么样子(如果有)?

What would my HTML code look like (if any)?

是否需要多个类,还是可以只使用一个类?

Are multiple classes necessary, or can you use just one class?

推荐答案

邮件程序逻辑应该放在它自己的独立类中,您可以在任何地方重复使用.JSP 文件应仅包含表示逻辑和标记.Servlet 类应该只以适当的方式处理请求并调用邮件程序类.以下是您需要采取的步骤:

The mailer logic should go in its own standalone class which you can reuse everywhere. The JSP file should contain presentation logic and markup only. The Servlet class should just process the request the appropriate way and call the mailer class. Here are the steps which you need to take:

  1. 首先决定您要使用哪个 SMTP 服务器,以便您将能够发送电子邮件.您的 ISP 之一?Gmail 之一?雅虎?网站托管服务提供商?一个自己维护的?无论如何,计算这个 SMTP 服务器的主机名、端口、用户名和密码.您将需要这些信息.

  1. First decide which SMTP server you'd like to use so that you would be able to send emails. The one of your ISP? The one of Gmail? Yahoo? Website hosting provider? A self-maintained one? Regardless, figure the hostname, port, username and password of this SMTP server. You're going to need this information.

创建一个使用 JavaMail API 的普通 Java 类 发送邮件消息.JavaMail API 附带了一个优秀的教程常见问题解答.将类命名为 Mailer 并为其指定一个 send() 方法(或任何您想要的方法).使用一些带有 main() 方法的测试器类来测试它,如下所示:

Create a plain vanilla Java class which uses JavaMail API to send a mail message. The JavaMail API comes with an excellent tutorial and FAQ. Name the class Mailer and give it a send() method (or whatever you want). Test it using some tester class with a main() method like this:

public class TestMail {
    public static void main(String... args) throws Exception {
        // Create mailer.
        String hostname = "smtp.example.com";
        int port = 2525;
        String username = "nobody";
        String password = "idonttellyou";
        Mailer mailer = new Mailer(hostname, port, username, password);

        // Send mail.
        String from = "john.doe@example.com";
        String to = "jane.doe@example.com";
        String subject = "Interesting news";
        String message = "I've got JavaMail to work!";
        mailer.send(from, to, subject, message);
    }
}

您可以根据需要使其变得简单或高级.没关系,只要你有一个可以发送这样的邮件的类.

You can make it as simple or advanced as you want. It doesn't matter, as long as you have a class with which you can send a mail like that.

现在是 JSP 部分,你为什么提到 JSP 并不完全清楚,但是因为 JSP 是 应该 只表示 HTML,我敢打赌您希望在 JSP 中拥有类似联系表单的东西.这是一个启动示例:

Now the JSP part, it's not entirely clear why you mentioned JSP, but since a JSP is supposed to represent only HTML, I bet that you'd like to have something like a contact form in a JSP. Here's a kickoff example:

<form action="contact" method="post">
    <p>Your email address: <input name="email"></p>
    <p>Mail subject: <input name="subject"></p>
    <p>Mail message: <textarea name="message"></textarea></p>
    <p><input type="submit"><span class="message">${message}</span></p>
</form>

是的,简单明了,只需按照您想要的方式标记/设置样式即可.

Yes, plain simple, just markup/style it whatever way you want.

现在,创建一个 Servlet 类,它侦听 /contacturl-pattern(与提交的表单相同)并实现 doPost() 方法(与表单使用的方法相同)如下:

Now, create a Servlet class which listens on an url-pattern of /contact (the same as the form is submitting to) and implement the doPost() method (the same method as the form is using) as follows:

public class ContactServlet extends HttpServlet {
    private Mailer mailer;
    private String to;

    public void init() {
        // Create mailer. You could eventually obtain the settings as
        // web.xml init parameters or from some properties file.
        String hostname = "smtp.example.com";
        int port = 2525;
        String username = "nobody";
        String password = "forgetit";
        this.mailer = new Mailer(hostname, port, username, password);
        this.to = "you@example.com";
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email = request.getParameter("email");
        String subject = request.getParameter("subject");
        String message = request.getParameter("message");
        // Do some validations and then send mail:

        try {
            mailer.send(email, to, subject, message);
            request.setAttribute("message", "Mail succesfully sent!");
            request.getRequestDispatcher("/WEB-INF/contact.jsp").forward(request, response);
        } catch (MailException e) {
            throw new ServletException("Mailer failed", e);
        }
    }
}

就是这样.保持简单和干净.每件事都有自己明确的职责.

That's it. Keep it simple and clean. Each thing has its own clear responsibilities.

这篇关于如何从 jsp/servlet 发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆