使用smtp发送邮件时出错

时间:2013-05-03 08:55:19

标签: java email gmail javamail

我需要从我的Gmail帐户发送邮件到另一个帐户。我使用了以下代码。

               String fromaddress = "xxx@gmail.com"; 
    String password = "yyy";
    String hostname = "smtp.gmail.com";
    String hoststring = "mail.smtp.host";
    String toaddress = "yyy@gmail.com";
    String emailcontent;

    Properties properties = System.getProperties();
    properties.setProperty(hoststring, hostname);
    Session session = Session.getDefaultInstance(properties);

    try{
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromaddress));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(toaddress));
        message.setSubject("hi");
        emailcontent = "hi...";
        message.setText(emailcontent);
        System.out.println(emailcontent);
        Transport.send(message);
        System.out.println("Sent....");
    }catch (MessagingException mex) 
    {
        mex.printStackTrace();
    }

但我得到的错误如下...... javax.mail.MessagingException:无法连接到SMTP主机:smtp.gmail.com,port:25

我如何解决这个问题。你能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

我认为您需要更改端口号。 25至587

你可以从

获得帮助

http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

gmail设置帮助链接:

http://gmailsmtpsettings.com/

答案 1 :(得分:1)

只需对上述问题进行更多调整:

将端口更改为465以启用ssl发送。

我不认为上面的代码会起作用,因为你需要有一个authenticator对象。因为smtp还需要在gmail的情况下进行身份验证。

您可以执行以下操作:

有一个布尔标志,

boolean authEnable = true;  //True for gmail
boolean useSSL = true; //For gmail
//Getters and setters for the same

  if (isUseSSL()) {
         properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
         properties.put("mail.smtp.socketFactory.port", "465");
  }

Authenticator authenticator = new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("abc@gmail.com", "xyz"));
        }
    };
    if (isAuthEnable()) {
        properties.put("mail.smtp.auth", "true");
        session = Session.getDefaultInstance(properties, authenticator);
    } else {
        session = Session.getDefaultInstance(properties);
    }

答案 2 :(得分:1)

public static void sendEmail(Email mail) {

    String host = "smtp.gmail.com";
    String from = "YOUR_GMAIL_ID";
    String pass = "YOUR_GMAIL_PASSWORD";
    Properties 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", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    // Get the default Session object.
    Session session = Session.getDefaultInstance(props, null);

    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        // Set sender
        message.setFrom(new InternetAddress("Senders_EMail_Id"));

        // Set recipient
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("RECIPIENT_EMAIL_ID"));

        // Set Subject: header field
        message.setSubject("SUBJECT");

        // set content and define type
        message.setContent("CONTENT", "text/html; charset=utf-8");

        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
      } catch (MessagingException mex) {
        System.out.println(mex.getLocalizedMessage());
    }

}

}`

我认为这应该可以解决问题。