2011-03-03 63 views
0

两个异常来到我的程序发送邮件.......问题在Java中

  1. 无法连接到本地主机,端口25

  2. 连接被拒绝

mail.java的代码是---

package jMail; 

import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 

public class Mail { 

    private String to; 
    private String from; 
    private String message; 
    private String subject; 
    private String smtpServ; 

    public String getTo() { 
     return to; 
    } 

    public void setTo(String to) { 
     this.to = to; 
    } 

    public String getFrom() { 
     return from; 
    } 

    public void setFrom(String from) { 
     this.from = from; 
    } 

    public String getMessage() { 
     return message; 
    } 

    public void setMessage(String message) { 
     this.message = message; 
    } 

    public String getSubject() { 
     return subject; 
    } 

    public void setSubject(String subject) { 
     this.subject = subject; 
    } 

    public String getSmtpServ() { 
     return smtpServ; 
    } 

    public void setSmtpServ(String smtpServ) { 
     this.smtpServ = smtpServ; 
    } 

    public Exception sendMail(){ 
     try 
     { 
      Properties props = System.getProperties(); 
       // -- Attaching to default Session, or we could start a new one -- 
       props.put("mail.transport.protocol", "smtp"); 
       props.put("mail.smtp.starttls.enable","true"); 
       props.put("mail.smtp.host","localhost"); 
       props.put("mail.smtp.auth", "true"); 
       Authenticator auth = new SMTPAuthenticator(); 
       Session session = Session.getInstance(props, auth); 
       // -- Create a new message -- 
       Message msg = new MimeMessage(session); 
       // -- Set the FROM and TO fields -- 
       msg.setFrom(new InternetAddress(from)); 
       msg.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(to, false)); 
       // -- We could include CC recipients too -- 
       // if (cc != null) 
       // msg.setRecipients(Message.RecipientType.CC 
       // ,InternetAddress.parse(cc, false)); 
       // -- Set the subject and body text -- 
       msg.setSubject(subject); 
       msg.setText(message); 
       // -- Set some other header information -- 
       msg.setHeader("MyMail", "Java Mail Test"); 
       msg.setSentDate(new Date()); 
       // -- Send the message -- 
       Transport.send(msg); 
       System.out.println("Message sent to"+to+" OK."); 
       return null; 
     } 
     catch (Exception ex) 
     { 
      ex.printStackTrace(); 
      System.out.println("Exception "+ex); 
      return ex; 
     } 
    } 

    private class SMTPAuthenticator extends javax.mail.Authenticator { 
     @Override 
     public PasswordAuthentication getPasswordAuthentication() { 
      String username = "[email protected]"; 
      String password = "javamail"; 
      return new PasswordAuthentication(username, password); 
     } 
    } 
} 

所以请告诉我该怎么办&为什么会出现这些异常&我该如何才能使用java localhost作为主机发送邮件&。 .......................提前致谢。

+1

您的计算机上是否运行SMTP服务器? – 2011-03-03 19:50:15

+0

此外,您尝试连接到本地主机,但使用Gmail凭据进行身份验证。你的意思是通过Gmail的SMTP服务器发送邮件吗? – dkarp 2011-03-04 04:09:32

+0

我是新手,但我认为smtp服务器正在我的m/c上运行,因为当我使用smtp.gmail.com作为主机时,异常Aries -javax.mail.AuthenticationFailedException。 – user643529 2011-03-04 09:58:55

回答

0

以下是发送电子邮件的代码片段。

try { 
     String host = "yourHostName"; 
     String from = "[email protected]"; 
     String to[] = {"[email protected]"}; 
     String subject = "Test"; 

     String message = "Test"; 

     Properties prop = System.getProperties(); 
     prop.put("mail.smtp.host", host); 
     Session sess1 = Session.getInstance(prop, null); 
     MimeMessage msg = new MimeMessage(sess1); 
     msg.setFrom(new InternetAddress(from)); 
     InternetAddress[] toAddress = new InternetAddress[to.length]; 
     for (int i = 0; i < to.length; i++) { 
      toAddress[i] = new InternetAddress(to[i]); 
     } 
     msg.setRecipients(Message.RecipientType.TO, toAddress); 
     msg.setSubject(subject); 

     //Fill the message 
     msg.setText(message); 
     Transport.send(msg); 
    } catch (MessagingException me) { 
     me.printStackTrace(); 
    } 

发送电子邮件时,您会遇到什么异常?你确定你的SMTP服务器正在监听默认端口25吗?你能够通过Telnet手动发送电子邮件吗?另外,关闭任何防火墙,同时测试这个只是为了确保。

+0

1. javax.mail.MessagingException:无法连接到SMTP主机:localhost,port:25; 2.嵌套异常是:java.net.ConnectException:连接被拒绝:当我使用username =“[email protected]”时连接。 ,我怎么能知道我的SMTP服务器正在默认端口监听,我是新手,所以我不知道要通过telnet手动发送电子邮件 – user643529 2011-03-04 10:14:16

+0

请点击此链接http://www.yuki-onna.co.uk/email/smtp .html通过Telnet发送电子邮件。 – 2011-03-04 17:01:32

+0

同时检查本地防火墙。 – 2011-03-09 22:38:21

2

只需按照此代码;将电子邮件发送到Java桌面并且它工作起来真的很有用。

import java.util.*; 
import javax.activation.CommandMap; 
import javax.activation.MailcapCommandMap; 
import javax.mail.*; 
import javax.mail.Provider; 


import javax.mail.internet.*; 
public class Main 
{ 
    public static void main(String[] args) 
    { 
     final String username = "[email protected]"; 
     final String password = "password"; 
     Properties prop = new Properties(); 
     prop.put("mail.smtp.auth", "true"); 
     prop.put("mail.smtp.host", "smtp.gmail.com"); 
     prop.put("mail.smtp.port", "587"); 
     prop.put("mail.smtp.starttls.enable", "true"); 
     Session session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() 
     { 
      protected PasswordAuthentication getPasswordAuthentication() 
      { 
       return new PasswordAuthentication(username, password); 
      } 
     }); 
     try 
     { 
      String body = "Dear Renish Khunt Welcome"; 
      String htmlBody = "<strong>This is an HTML Message</strong>"; 
      String textBody = "This is a Text Message."; 
      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]")); 
      message.setSubject("Testing Subject"); 
      MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
      mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
      mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
      mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
      mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
      mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
      CommandMap.setDefaultCommandMap(mc); 
      message.setText(htmlBody); 
      message.setContent(textBody, "text/html"); 
      Transport.send(message); 
      System.out.println("Done"); 
     } 
     catch (MessagingException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

很好的例子对我来说工作得很好! – lucifer 2014-04-14 14:24:59