gpt4 book ai didi

java - 使用 Javamail API 将邮件从雅虎 ID 发送到其他电子邮件 ID

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:14:07 24 4
gpt4 key购买 nike

我无法使用 Java 邮件 API 从我的雅虎 ID 发送电子邮件。我尝试了 Google 的不同选项,但失败了。请看看我下面的代码,如果我遗漏了什么,请告诉我。在我看来,Yahoo 不提供免费发送邮件的服务,但我不确定。请提供您对此的看法。

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

public class MailExample {
private static final String SMTP_HOST_NAME = "smtp.mail.yahoo.com";
private static final int SMTP_HOST_PORT = 587;//465,587,25
private static final String SMTP_AUTH_USER = "dummyrls@yahoo.com";
private static final String SMTP_AUTH_PWD = "my password";

public static void main(String[] args) throws Exception{
new MailExample().test();
}

public void test() throws Exception{
Properties props = new Properties();

props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
// props.put("mail.smtps.quitwait", "false");

Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("This is a test", "text/plain");

message.addRecipient(Message.RecipientType.TO,
new InternetAddress("rlss@abc.com"));

transport.connect
(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);

transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}

上面的代码对于 Gmail 工作正常,但是对于 Yahoo 它会给出如下错误:

DEBUG: setDebug: JavaMail version 1.4.1 DEBUG: getProvider() 
returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,
Sun Microsystems, Inc.,1.4.1] DEBUG SMTP: useEhlo true,
useAuth true
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com", port 587,
isSSL false Exception in thread "main"
javax.mail.MessagingException: Could not connect to SMTP
host: smtp.mail.yahoo.com, port: 587; nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:288)
at com.sample.mailexample.MailExample.test(MailExample.java:313)
at com.sample.mailexample.MailExample.main(MailExample.java:291) Caused by:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
... 4 more

我该如何解决这个问题?

最佳答案

使用 JavaMail API 从 Yahoo 发送电子邮件的完整代码如下:

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

public class SendFromYahoo
{
public static void main(String [] args)
{
// Sender's email ID needs to be mentioned
String from = "test123@yahoo.com";
String pass ="test123";
// Recipient's email ID needs to be mentioned.
String to = "riponalwasim@yahoo.com";
String host = "smtp.mail.yahoo.com";

// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");

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

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");

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

关于java - 使用 Javamail API 将邮件从雅虎 ID 发送到其他电子邮件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11356237/

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