使用MS Exchange Server将邮件发送到Microsoft Outlook中的帐户有哪些连接属性?

时间:2014-10-15 19:27:02

标签: java email outlook javamail exchange-server

我正在尝试使用以下类从MS Outlook帐户发送电子邮件:

public class MailSender {

public MailSender() {}

public static boolean send(String hostSmtp, String senderAddress,
        String toAddress, String subject, boolean isHTMLFormat, StringBuffer body,
        boolean debug) {

    MimeMultipart multipart = new MimeMultipart();

    Properties properties = new Properties();
    properties.put("mail.smtp.host", hostSmtp);
    properties.put("mail.smtp.starttls.enable", "true");
    //properties.put("mail.smtp.port", port);

    Session session = Session.getDefaultInstance(properties, null);
    session.setDebug(debug);
    try {
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(senderAddress));
        msg.setRecipients(Message.RecipientType.TO, toAddress);
        msg.setSubject(subject);
        msg.setSentDate(new Date());

        // BODY
        MimeBodyPart mbp = new MimeBodyPart();
        if (isHTMLFormat) {
            mbp.setContent(body.toString(), "text/html");
        } else {
            mbp.setText(body.toString());
        }

        multipart.addBodyPart(mbp);
        msg.setContent(multipart);

        Transport.send(msg);            
    } catch (Exception mex) {
        System.out.println(">> MailSender.send() error = " + mex);
        return false;
    }
    return true;
}
}

但是当我在jsp中使用该类时:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import = "pe.com.bn.PRFI.general.MailSender"%> 
<html>
<head>
<title>EnviarCorreo</title>
</head>
<body>
<%  
boolean result = MailSender.send("smtp.bn.com.pe", "email1@bn.com.pe",
"email2@bn.com.pe", "Test Mail", false, new StringBuffer("Hello"),true);

out.print("Result of sending the message : " + result);
%> 
</body>
</html>

向我展示了以下例外:

  

[15/10/14 19:30:58:126 GMT] 00000026 SystemOut O DEBUG:setDebug:JavaMail 1.3.1版   [15/10/14 19:30:58:293 GMT] 00000026 SystemOut O DEBUG:getProvider()返回javax.mail.Provider [TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]   [15/10/14 19:30:58:302 GMT] 00000026 SystemOut O DEBUG SMTP:useEhlo true,useAuth false   [15/10/14 19:30:58:302 GMT] 00000026 SystemOut O DEBUG SMTP:尝试连接主机“smtp.bn.com.pe”,端口25   [15/10/14 19:30:58:305 GMT] 00000026 SystemOut O&gt;&gt; MailSender.send()error = javax.mail.SendFailedException:发送失败;     嵌套异常是:       class javax.mail.MessagingException:无法连接到SMTP主机:smtp.bn.com.pe,port:25;     嵌套异常是:       java.net.SocketException:Permission denied:connect

是否已明确指定连接属性?

1 个答案:

答案 0 :(得分:0)

首先,更正此common mistake

如果这没有用,并且您在安全管理器的服务器上运行,则可能需要grant your application the security permissions to connect to the mail server