gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:51:44 27 4
gpt4 key购买 nike

我正在尝试在运行远程服务器时使用 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);
}
}
}

欲了解更多详情,您可以visit 。这里提供了代码。

其他tutorial有完整的描述。

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

关于java - 运行远程服务器时无法使用 Java 代码发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180618/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com