如何配置邮件服务器以用于 JavaMail?
2022-09-02 20:40:38
我正在尝试使用以下代码:
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);
}
}
}
我正在使用邮件.jar和激活.jar。但是我不明白我应该如何用邮件服务器配置它。我应该使用哪个邮件服务器?我可以使用上述代码发送电子邮件吗?邮件服务器有什么要求?我应该如何配置它?