如何配置与JavaMail一起使用的邮件服务器? [英] How do I configure a mail server for use with JavaMail?

查看:180
本文介绍了如何配置与JavaMail一起使用的邮件服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 {
属性props = new Properties();
props.put(mail.smtp.host,localhost); //'localhost'用于测试
会话session1 = Session.getDefaultInstance(道具,null);
String s1 = request.getParameter(text1); // sender(from)
String s2 = request.getParameter(text2);
String s3 = request.getParameter(text3);
String s4 = request.getParameter(area1);
消息消息= 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和activation.jar。但我不明白我应该如何配置邮件服务器。我应该使用哪个邮件服务器?我可以使用以上代码发送电子邮件吗?邮件服务器的要求是什么?如何配置?

解决方案

要开始,您需要一个 SMTP服务器。它需要能够发送电子邮件。与您需要HTTP服务器一样能够提供网站的方式相同。您显然已经有一个HTTP服务器(带有servletcontainer),但是您尚未配置SMTP服务器。



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



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

 属性属性= 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);

用户名/密码应用于验证器如下:

  properties.put(mail.smtp.auth,true); 
Authenticator authentator = new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
返回新的PasswordAuthentication(yourusername,yourpassword);
}
};

然后,您可以按以下方式获取邮件会话:

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

使用您的ISP或公共邮箱的帐户,您只能使用自己的地址电子邮件的 From 字段,通常也是您允许以特定间隔发送的电子邮件数量。如果您想要解决此问题,那么您需要安装自己的SMTP服务器,例如 Apache James ,它是基于Java的,或Microsoft Exchange等。



毕竟,我建议你通过一个 JavaMail教程,以便您更好地了解。


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?

解决方案

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.

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 hostname and the port number. The username/password are just the same as those of your email account.

The hostname and port number should then be set as SMTP properties for 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 username/password should be used in a 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 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.

After all, I suggest you to get yourself through a JavaMail tutorial so that you get a better understanding.

这篇关于如何配置与JavaMail一起使用的邮件服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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