How do I configure a mail server for use with JavaMail?

I'm trying to use the following 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 don't understand how I should configure my mail server Which mail server should I use? Can I use the above code to send email? What are the requirements of the mail server? How do I configure it?

Solution

To start, you need an SMTP server It needs to be able to send email As you need an HTTP server, you can provide websites in the same way You obviously already have an HTTP server (with a servletcontainer), but you don't have an SMTP server configured

You can use the SMTP server associated with your existing email account, such as your ISP, Gmail, Yahoo and other public mailboxes You can find SMTP connection details in its documentation You usually only need to know the host name and port number The user name / password is the same as your email account

Then set the host name and port number to the SMTP properties of JavaMail:

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 user name / password should be used in authenticator as follows:

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

Then you can get the email session as follows:

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

Use your ISP or public mailbox account, but you are not limited to using your own address in the "from" field of e-mail, and usually the number of e-mail messages allowed to be sent at specific intervals If you want to solve this problem, you need to install your own SMTP server, such as Java based Apache James or Microsoft Exchange

After all, I suggest you get a better understanding through a JavaMail tutorial

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>