gpt4 book ai didi

java - 无法通过 Java 发送电子邮件

转载 作者:行者123 更新时间:2023-11-30 09:42:33 27 4
gpt4 key购买 nike

在浏览了针对相同问题提供的帖子后,我编写了以下代码。但我收到以下异常:

javax.mail.MessagingException: 无法连接到 SMTP 主机:smtp.gmail.com,端口:587; 嵌套异常是:java.net.ConnectException:连接超时:连接

public static void main(String[] args) {

String to = "xxx@gmail.com" // valid gmail address.
String from = "yyy@gmail.com"; // valid gmail address

String host = "smtp.gmail.com";
String password = "****"; // password of the gmaill acc used in from

int port = 587;


Properties properties = System.getProperties();
properties.put("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.host",host );
properties.setProperty("mail.smtp.user", from);
properties.setProperty("mail.smtp.password", password);
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties,null);

try {

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

message.setSubject("Test Mail");

message.setText("This is just a test mail generated");

Transport transport = session.getTransport("smtp");
transport.connect(host,from,password);
InternetAddress[] addresses = new InternetAddress[1];
addresses[0] = new InternetAddress(to);
transport.sendMessage(message,addresses);


System.out.println("Message Sent Successfully");
}catch(MessagingException excp){
System.out.println(excp);
}

}

有人能说出我在做的错误吗?我的 gmail 帐户中是否有任何设置需要设置为使用 gmail smtp 服务器?

最佳答案

试试下面的代码。您将需要下载 javax.mail 包(一个 jar 文件),我假设您已经拥有该 jar 文件,因为您已经尝试过此代码,因此,我没有提供下载链接那个jar文件。正确设置类路径并导入必要的包。请注意您选择的防火墙和主机端口。

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

final class MailClient
{
private class SMTPAuthenticator extends Authenticator
{
private PasswordAuthentication authentication;

public SMTPAuthenticator(String login, String password)
{
authentication = new PasswordAuthentication(login, password);
}

@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return authentication;
}
}

public void mail()
{
try
{
String from = "xyz.com";
String to = "abc.com";
String subject = "Your Subject.";
String message = "Message Text.";
String login = "xyz.com";
String password = "password";

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

Authenticator auth = new SMTPAuthenticator(login, password);

Session session = Session.getInstance(props, auth);

MimeMessage msg = new MimeMessage(session);

try
{
msg.setText(message);
msg.setSubject(subject);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
Transport.send(msg);
}
catch (MessagingException ex)
{
Logger.getLogger(MailClient.class.getName()).
log(Level.SEVERE, null, ex);
}
}
}
}

final public class Main
{
public static void main(String...args)
{
new MailClient().mail();
}
}

关于java - 无法通过 Java 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8612437/

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