【问题标题】:Not able to send email with Java code while running remote server运行远程服务器时无法使用 Java 代码发送电子邮件
【发布时间】:2014-08-02 13:19:20
【问题描述】:

我正在尝试在运行远程服务器时使用 Java 发送电子邮件。这是我的代码:

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

    //import javax.activation.*;

    public class Email {

    private static String SMPT_HOSTNAME = "smtp.mtnl.net.in";
    private static String USERNAME = "20870134";
    private static String PASSWORD = "2070252342";

    public static void main(String[] args) {

    // Recipient's email ID needs to be mentioned.
    String to = "katha4494@gmail.com";

    // Sender's email ID needs to be mentioned
    String from = "safepassw0rd@gmail.com";

    // Assuming you are sending email from localhost
    // String host = "localhost";

    // Get system properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.setProperty("mail.smtp.host", SMPT_HOSTNAME);

    // Get the default Session object.
    // Session session = Session.getDefaultInstance(properties);

    // create a session with an Authenticator
    Session session = Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(USERNAME, PASSWORD);
        }
    });

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

        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));

        // Set To: header field of the header.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                to));

        // Set Subject: header field
        message.setSubject("This is the Subject Line!");

        // Now set the actual message
        message.setText("This is actual message");
        System.out.println("Sending message ....");
        // Send message
        Transport.send(message);
        System.out.println("Sent message successfully....");
    } catch (MessagingException mex) {
        mex.printStackTrace();
    }
    }

我收到以下错误:

    Sending message ....
    javax.mail.MessagingException: [EOF]
at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481)
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1512)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1054)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:634)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:118)
at SendEmail.Email.main(Email.java:61)

我从我的 ISP 获得了服务器主机名、用户名和密码。我是 Java 网络的新手,我不明白哪里出了问题。请帮忙。

【问题讨论】:

  • 你添加了smtp端口吗??
  • @user3145373ツ 我该怎么做?

标签: java email exception networking smtp


【解决方案1】:

看看这个例子:

导入 java.util.Properties;

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

public class SendMailTLS {

    public static void main(String[] args) {

        final String username = "username@gmail.com";
        final String password = "password";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from-email@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to-email@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

更多详情您可以visit。这里有代码。

其他tutorial 有完整描述。

还是有问题,给我发帖。

【讨论】:

  • 谢谢你,它工作得很好!你能告诉我我做错了什么吗?!我们在这里运行本地 smtp 服务器吗?
  • 在您的代码中,您正在这样做,这就是问题发生的原因,作为答案,它使用 gmail smtp 服务器来发送邮件,我们必须使用它,否则我们无法发送邮件。我们也可以使用其他 smtp 服务器,如 yahoo、live 等,但通常使用 gmail。太好了,你解决了你的问题。 :)
猜你喜欢
  • 2013-09-29
  • 2014-06-15
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
相关资源
最近更新 更多