在java中从我的计算机向我的Gmail帐户发送邮件

时间:2011-11-03 09:48:15

标签: java

我正在尝试使用java代码向我的Gmail发送邮件。

这是我的代码:

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 = "mymailId@enmediatech.com";
    String to = "mymailIdto_send@gmail.com";
    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();
    }       
}

}

这是我得到的错误:

 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,我的系统也有防病毒软件。是否因为这两个,可能会出现上述错误。任何人都可以指导我。

1 个答案:

答案 0 :(得分: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");