java如何不连smtp,直接发送邮件

看了一些发送邮件的源代码,是通过smtp方式发送邮件到smtp服务器,smtp服务器发送至目标邮箱。
smtp服务器是通过邮件发送协议进行发送,因此,我们自己可以实现一个程序想smtp服务器那样发送邮件到目标邮箱。

不知道java有没有现成的实现?有什么lib可以实现。Spring里面貌似有个simpleMessage实现,但是不能添加附件。我需要可以添加附件的直接发送邮件的代码

apache james 是个纯 java 实现的 mail server,如果有必要可以集成进你的系统。
不过这样做远不如直接用 smtp 简单。

兄弟你弄错了吧,spring也不能不通过mail服务器直接发mail的。spring的MimeMessagePreparator可以直接发带附件的mail。但是也要配置smtp服务器。

private void sendMimeMessage(final JavaMailSender sender) throws Exception {
//附件文件集合
final List files = new ArrayList();
MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
mimeMessage.setRecipient(Message.RecipientType.TO,
new InternetAddress("superman_wshm@126.com"));
mimeMessage.setFrom(new InternetAddress("superman_wshm@126.com"));
mimeMessage.setSubject("Spring发送带附件的邮件", "gb2312");

            Multipart mp = new MimeMultipart();

            //向Multipart添加正文

            MimeBodyPart content = new MimeBodyPart();
            content.setText("内含spring邮件发送的例子,请查收!");

            //向MimeMessage添加(Multipart代表正文)
            mp.addBodyPart(content);
            files.add("com/action/SpringMail.java");
            files.add("applicationContext.xml");

            //向Multipart添加附件
            Iterator it = files.iterator();
            while(it.hasNext()) {
                MimeBodyPart attachFile = new MimeBodyPart();
                String filename = it.next().toString();
                FileDataSource fds = new FileDataSource(filename);
                attachFile.setDataHandler(new DataHandler(fds));
                attachFile.setFileName(fds.getName());
                mp.addBodyPart(attachFile);
            }

            files.clear();

            //向Multipart添加MimeMessage
            mimeMessage.setContent(mp);
            mimeMessage.setSentDate(new Date());
        }
    };

    //发送带附件的邮件
    sender.send(mimeMail);

不连接smtp,直接发送邮件,一般的邮件服务器都会拒收的。没有意义。