Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行远程服务器时无法使用Java代码发送电子邮件_Java_Email_Exception_Networking_Smtp - Fatal编程技术网

运行远程服务器时无法使用Java代码发送电子邮件

运行远程服务器时无法使用Java代码发送电子邮件,java,email,exception,networking,smtp,Java,Email,Exception,Networking,Smtp,我试图在运行远程服务器时使用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

我试图在运行远程服务器时使用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网络新手,我不知道怎么回事。请帮助。

看看这个例子:

导入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);
        }
    }
}
有关更多详细信息,请访问。这里的代码是可用的

其他详细描述


仍然有问题,请发邮件给我。

您添加了smtp端口吗???@user3145373ツ 我该怎么做?谢谢,它工作得很好!你能告诉我我做错了什么吗?!我们正在运行本地smtp服务器吗?在您的代码中,您正在这样做,这就是问题发生的原因,作为回答,它使用gmail smtp服务器发送邮件,我们必须使用它,否则我们无法发送邮件。我们也可以使用其他smtp服务器,如yahoo、live等,但gmail是常用的。很高兴你解决了你的问题。:)