服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用smtp协议发送邮件


  使用smtp协议发送邮件,可以不通过smtp服务器,直接将邮件发送到邮件服务器。很多服务器端程序可能需要向很多用户发送邮件,直接通过smtp发送可能是最有效的。

关于smtp协议定义在rfc821,可以在此看中文版。

第一步:通过目标email查找邮件服务器。
例如:asklxf@sohu.com,其邮件服务器地址为:sohumx.sohu.com

import java.net.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;

public class smtp {

    public static void main(string[] args) throws exception {
        // dns服务器,看看本机的dns配置
        string dns = "dns://192.168.1.1";
        // 邮箱后缀:
        string domain = "sohu.com";
        hashtable env = new hashtable();
        env.put("java.naming.factory.initial", "com.sun.jndi.dns.dnscontextfactory");
        env.put("java.naming.provider.url", dns);
        dircontext ctx = new initialdircontext(env);
        attributes attr = ctx.getattributes(domain, new string[]{"mx" });
        namingenumeration servers = attr.getall();
        // 列出所有邮件服务器:
        while(servers.hasmore()) {
            system.out.println(servers.next());
        }
    }
}

第二步:直接连接邮件服务器的25端口,用smtp协议发送邮件。
这里使用sohu信箱,邮件服务器为sohumx.sohu.com,收信人必须在此服务器上:

import java.net.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;

public class smtp {

    private static string end_flag = "/r/n";

    public static void main(string[] args) throws exception {
        string mx = "sohumx.sohu.com";
        inetaddress addr = inetaddress.getbyname(mx);
        socket socket = new socket(addr, 25);

        inputstream in = socket.getinputstream();
        outputstream out = socket.getoutputstream();

        // 连接成功后服务器会响应:
        response(in);

        // 首先发送helo命令:
        send("helo
www.javasprite.com" + end_flag, out);
        response(in);

        // 然后发送发件人地址:
        send("mail from: someone@somewhere.com" + end_flag, out);
        response(in);

        // 设置收件人地址:
        send("rcpt to: asklxf@sohu.com" + end_flag, out);
        response(in);

        // 开始发送邮件正文:
        send("data" + end_flag, out);
        response(in);

        send("from: someone@somewhere.com" + end_flag, out);
        send("to: asklxf@sohu.com" + end_flag, out);
        send("subject: test without smtp server" + end_flag, out);
        send("content-type: text/plain;" + end_flag, out);
        send(end_flag + end_flag, out);

        // 发送邮件正文,如果用中文,需要base64编码:
        send("text message body!" + end_flag, out);
        // 每行以/r/n结束,不可过长,可拆成多行。

        // 以"/r/n./r/n"作为结束标志:
        send(end_flag + "." + end_flag, out);
        response(in);

        // 结束并确认发送:
        send("quit" + end_flag, out);
        response(in);
        in.close();
        out.close();
        socket.close();
    }

    public static void response(inputstream in) throws exception {
        byte[] buffer = new byte[1024];
        int n = in.read(buffer);
        string s = new string(buffer, 0, n);
        // 服务器会返回:### text
        // 具体含义见rfc821
        system.out.println(s);
    }

    public static void send(string s, outputstream out) throws exception {
        byte[] buffer = s.getbytes();
        out.write(buffer);
        // 不要忘了flush(),否则可能在缓冲区:
        out.flush();
    }
}

ok,打开outlook收信,会发现有一封来自someone@somewhere.com的信件。

第三步:处理服务器返回码,各种异常,包装成java组件以便重用:

public interface sendmail {
    void send(string from, string to, string subject, string text)
}

public class sendmailimpl extends thread implements sendmail {
    // todo: 自己写......
}

扫描关注微信公众号