如何配置邮件服务器以与 JavaMail 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2957299/
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
How do I configure a mail server for use with JavaMail?
提问by simplyblue
I'm trying to work with the below code:
我正在尝试使用以下代码:
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);
}
}
}
I'm using mail.jar and activation.jar. But I can't understand how I should configure it with a mail server. Which mail server should I use? Will I be able to send an email using above code? What are the requirements a mail server? How should I configure it?
我正在使用mail.jar 和activation.jar。但我不明白我应该如何使用邮件服务器配置它。我应该使用哪个邮件服务器?我可以使用上面的代码发送电子邮件吗?邮件服务器有什么要求?我应该如何配置它?
采纳答案by BalusC
To start, you need a SMTP server. It's required to be able to send emails. The same way as you need a HTTP server to be able to serve a website. You apparently already have a HTTP server (with a servletcontainer), but you don't have a SMTP server configured yet.
首先,您需要一个SMTP 服务器。它需要能够发送电子邮件。与您需要 HTTP 服务器才能为网站提供服务的方式相同。您显然已经有一个 HTTP 服务器(带有一个 servletcontainer),但是您还没有配置 SMTP 服务器。
You can make use of the SMTP server associated with your own existing email account, such as the one from your ISP or public mailboxes like Gmail, Yahoo, etc. You can find SMTP connection details in their documentation. You usually just need to know the hostnameand the port number. The username/passwordare just the same as those of your email account.
您可以使用与您自己现有的电子邮件帐户相关联的 SMTP 服务器,例如来自您的 ISP 或公共邮箱(如 Gmail、Yahoo 等)的服务器。您可以在他们的文档中找到 SMTP 连接的详细信息。您通常只需要知道主机名和端口号。用户名/密码与您的电子邮件帐户的用户名/密码相同。
The hostname and port number should then be set as SMTP properties for JavaMail:
然后应将主机名和端口号设置为 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");
The username/password should be used in a Authenticator
as follows:
用户名/密码应按Authenticator
如下方式使用:
properties.put("mail.smtp.auth", "true");
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("yourusername", "yourpassword");
}
};
Then you can get the mail session as follows:
然后您可以按如下方式获取邮件会话:
Session session = Session.getDefaultInstance(properties, authenticator);
With the account of your ISP or public mailboxes, you're however restricted to using your own address in the From
field of the email and usually also in the amount of emails you're allowed to send at certain intervals. If you'd like to get around this, then you need to install your own SMTP server, for example Apache James, which is Java based, or Microsoft Exchange and so on.
但是,使用 ISP 或公共邮箱的帐户,您只能From
在电子邮件字段中使用您自己的地址,并且通常还限制您在特定时间间隔内允许发送的电子邮件数量。如果您想解决这个问题,那么您需要安装自己的 SMTP 服务器,例如基于 Java 的Apache James或 Microsoft Exchange 等。
After all, I suggest you to get yourself through a JavaMail tutorialso that you get a better understanding.
毕竟,我建议您通过JavaMail 教程来了解自己,以便更好地理解。