Transport.send(msg)无法正常工作 [英] Transport.send(msg) not working

查看:728
本文介绍了Transport.send(msg)无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javamail从一个帐户向另一个帐户发送电子邮件.但是代码无法执行Transport.send(msg);线.可能是什么原因? 下面是下面的jsp代码.

I am trying to send an email from one account to another using javamail. But the code is unable to execute Transport.send(msg); line. What could be the possible reason? Below is the following jsp code.

<%

        String host = "localhost";
        String to = request.getParameter("to");
        String from = request.getParameter("from");
        String subject = request.getParameter("subject");
        String messageText = request.getParameter("body");
        boolean sessionDebug = false;

        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtp");

        Session mailSession = Session.getDefaultInstance(props, null);

        mailSession.setDebug(sessionDebug);

        try { 
        Message msg = new MimeMessage(mailSession);

        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setText(messageText);

        Transport.send(msg);

        out.println("Mail was sent to " + to);
        out.println(" from " + from);
        out.println(" using host " + host + ".");
        } catch (MessagingException mex) {mex.printStackTrace();}
    %>

推荐答案

这些是我通常使用的属性

These are the properties that I usually use

properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.host", host);
properties.put("mail.user", from);
properties.put("mail.smtp.port", smtpPort);
properties.put("mail.smtp.localhost", "myHost");

Session session = Session.getInstance(properties, null);

您会注意到我设置了mail.smtp.localhost,如果机器的主机名设置不正确,则需要设置mail.smtp.localhost.用于VM等

You will notice that I set mail.smtp.localhost which needs to be set if the machines hostname is not set up properly e.g. for a VM etc

这篇关于Transport.send(msg)无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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