java - 如何配置邮件服务器以与 JavaMail 一起使用?

标签 java email servlets jakarta-mail

我正在尝试使用以下代码:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;   // important
import javax.mail.event.*;      // important
import java.net.*;
import java.util.*;

public class servletmail extends HttpServlet {
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        response.setContentType("text/html");
        try {
            Properties props=new Properties();
            props.put("mail.smtp.host","localhost");   //  'localhost' for testing
            Session   session1  =  Session.getDefaultInstance(props,null);
            String s1 = request.getParameter("text1"); //sender (from)
            String s2 = request.getParameter("text2");
            String s3 = request.getParameter("text3");
            String s4 = request.getParameter("area1");
            Message message =new MimeMessage(session1);
            message.setFrom(new InternetAddress(s1));
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(s2,false));
            message.setSubject(s3);
            message.setText(s4);        
            Transport.send(message);
            out.println("mail has been sent");
        } catch(Exception ex) {
            System.out.println("ERROR....."+ex);
        }
    }
}

我正在使用 mail.jar 和 activation.jar。但我不明白我应该如何用邮件服务器配置它。我应该使用哪个邮件服务器?我可以使用上面的代码发送电子邮件吗?邮件服务器有什么要求?我该如何配置?

最佳答案

首先,您需要一个 SMTP server .它需要能够发送电子邮件。就像您需要 HTTP 服务器才能为网站提供服务一样。您显然已经有一个 HTTP 服务器(带有 servletcontainer),但您还没有配置 SMTP 服务器。

您可以使用与您现有的电子邮件帐户相关联的 SMTP 服务器,例如来自您的 ISP 或公共(public)邮箱(如 Gmail、Yahoo 等)的服务器。您可以在其文档中找到 SMTP 连接的详细信息。您通常只需要知道主机名端口号用户名/密码与您的电子邮件帐户相同。

然后应将主机名和端口号设置为 JavaMail 的 SMTP 属性:

Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", "smtp.example.com"); // smtp.gmail.com?
properties.put("mail.smtp.port", "25");

用户名/密码应该在 Authenticator 中使用,如下所示:

properties.put("mail.smtp.auth", "true");
Authenticator authenticator = new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("yourusername", "yourpassword");
    }
};

然后就可以得到邮件 session 如下:

Session session = Session.getDefaultInstance(properties, authenticator);

但是,使用您的 ISP 或公共(public)邮箱的帐户,您只能在电子邮件的 From 字段中使用您自己的地址,并且通常也会限制您允许的电子邮件数量以一定的间隔发送。如果您想解决这个问题,那么您需要安装自己的 SMTP 服务器,例如 Apache James ,基于 Java,或 Microsoft Exchange 等。

毕竟,我建议你自己通过 JavaMail tutorial以便您更好地理解。

关于java - 如何配置邮件服务器以与 JavaMail 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957299/

相关文章:

Java Remote 终止用户 session

java - 从 Servlet 访问 SSL 私钥

java - 如何为 Java Web 应用程序配置路由

Java 对象.等于

java - number 不能包裹在 flyingsaucer 和 itext 中

java - Spring MVC - 将空日期作为参数传递

java - HashMap put() 和 get() 方法工作原理的内部结构(仅基本逻辑)

python - 如何使用 exchangelib 发送带有“请勿转发”标志的电子邮件

php - 我应该使用 Queue 通过 3rd 方服务发送电子邮件吗

android - <div> 在 android 中发送电子邮件时不创建列