2011-11-03 57 views
0

我正在尝试使用java代码向我的gmail发送邮件。从我的电脑发送邮件到我的gmail帐户在java中

这是我的代码:

MailSender.java

package MailSenderInfo; 

    import javax.mail.MessagingException; 
    import javax.mail.internet.AddressException; 

    public class MailSender { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    String from = "[email protected]"; 
    String to = "[email protected]"; 
    String subject = "Test"; 
    String message = "A test message"; 

    SendMailBody sendMail = new SendMailBody(from, to, subject, message); 

     sendMail.send(); 

} 

}

SenderMailBody.java:

package MailSenderInfo; 

import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMessage.RecipientType; 

public class SendMailBody { 

private String from; 
private String to; 
private String subject; 
private String text; 

public SendMailBody(String from, String to, String subject, String text){ 
    this.from = from; 
    this.to = to; 
    this.subject = subject; 
    this.text = text; 
} 

public void send(){ 

    Properties props = new Properties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 

     props.put("mail.smtp.password", "pass"); 
    props.put("mail.smtp.port", "587"); 
    props.put("mail.smtp.auth", "true"); 
    Session mailSession = Session.getDefaultInstance(props); 
    Message simpleMessage = new MimeMessage(mailSession); 

    InternetAddress fromAddress = null; 
    InternetAddress toAddress = null; 
    try { 
     fromAddress = new InternetAddress(from); 
     toAddress = new InternetAddress(to); 
    } catch (AddressException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     simpleMessage.setFrom(fromAddress); 
     simpleMessage.setRecipient(RecipientType.TO, toAddress); 
     simpleMessage.setSubject(subject); 
     simpleMessage.setText(text); 

     Transport.send(simpleMessage);   
    } catch (MessagingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }  
} 

}

这是ER ROR这我得到:

javax.mail.SendFailedException: Sending failed; 
nested exception is: 
class javax.mail.MessagingException: Could not connect to SMTP host:  smtp.gmail.com, port: 25; 
     nested exception is: 
    java.net.ConnectException: Connection refused: connect 
    at javax.mail.Transport.send0(Transport.java:218) 
    at javax.mail.Transport.send(Transport.java:80) 
    at MailSenderInfo.SendMailBody.send(SendMailBody.java:53) 
    at MailSenderInfo.MailSender.main(MailSender.java:20) 

这在我的系统我已经配置Outlook和我的系统也有杀毒software.Whether因为这两个以上的错误可以occur.Can任何人指导我。

回答

0

变更Gmail SMTP端口465个或587,并添加STARTTLS选项

host = "smtp.gmail.com";  
props = System.getProperties(); 
props.put("mail.smtp.starttls.enable", "true"); // added this line 
props.put("mail.smtp.host", host); 
props.put("mail.smtp.user", from); 
props.put("mail.smtp.password", password); 
props.put("mail.smtp.port", "587"); 
props.put("mail.smtp.auth", "true"); 
+0

我收到以下错误。 javax.mail.SendFailedException:发送失败; 嵌套异常是: \t类javax.mail.AuthenticationFailedException \t在javax.mail.Transport.send0(Transport.java:218) \t在javax.mail.Transport.send(Transport.java:80) \t在MailSenderInfo.SendMailBody.send(SendMailBody.java:61) \t at MailSenderInfo.MailSender.main(MailSender.java:20) – bharathi

+0

from without gmail.com – SjB

+0

它不工作,是否因为oulook配置。 – bharathi

相关问题