簡體   English   中英

使用javax.mail發送消息

[英]using javax.mail to send message

我想使用javax.mail從example@uni.ac.uk發送電子郵件到receive@uni.ac.uk。 我試過下面的代碼,但它給出了錯誤,我也放在下面。 歡迎任何幫助。

    Properties prop = new Properties();
    Session sess = Session.getDefaultInstance(prop,null);

    Message msg = new MimeMessage(sess);
    msg.setFrom(new InternetAddress("example@uni.ac.uk", "Sender"));
             msg.addRecipient(Message.RecipientType.TO,
                      new InternetAddress("receiver@uni.ac.uk", "Receiver"));
            msg.setSubject("Your Example.com account has been activated");
            msg.setText("Testing messgage");
           Transport.send(msg);


}

產生的錯誤是

Exception in thread "main" javax.mail.MessagingException: Could not connect to
SMTP host: localhost, port: 25; nested exception is: java.net.SocketException: Permission denied: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1962) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124) at MAIL.sendmail.main(sendmail.java:41) Caused by: java.net.SocketException: Permission denied: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1928) ... 7 more

這是使用gmail發送電子郵件的示例屬性:

private Properties createConfiguration() {
    return new Properties() {{
        put("mail.smtp.auth", "true");
        put("mail.smtp.host", "smtp.gmail.com");
        put("mail.smtp.port", "587");
        put("mail.smtp.starttls.enable", "true");
    }};
}

您將“屬性”對象中的SMTP服務器設置為您自己的計算機(本地主機),這顯然未在端口25上運行SMTP服務器。我認為您想使用現有的SMTP服務器(例如從互聯網提供商處獲得的SMTP服務器,或GMail為您提供的郵件等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM