使用 Javamail API 从 yahoo id 向其他电子邮件 id 发送邮件

Posted

技术标签:

【中文标题】使用 Javamail API 从 yahoo id 向其他电子邮件 id 发送邮件【英文标题】:Sending mail from yahoo id to other email ids using Javamail API 【发布时间】:2012-07-06 13:26:57 【问题描述】:

我无法使用 Java 邮件 API 从我的 yahoo id 发送电子邮件。我尝试了谷歌的不同选项,但失败了。请看看我下面的代码,如果我遗漏了什么,请告诉我。在我看来 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

我该如何解决这个问题?

【问题讨论】:

然后对于 nauta,因为它将是外发邮件 (SMTP) 服务器服务器 SMTP:smtp.nauta.cu,端口:25,身份验证 (SI) In email app 【参考方案1】:

使用 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();
   


【讨论】:

这对我有用,谢谢!顺便说一句,可以删除 props.put("mail.smtp.host", host); props.put("mail.smtp.port", "587"); props.put("mail.smtp.user", user); props.put("mail.smtp.password", pass) 并简单地在 transport.connect(host, port, user, pass); 中提及它,它应该可以正常工作。 失败并出现错误:com.sun.mail.smtp.SMTPSendFailedException: 554 Transaction failed : 由于可能滥用而无法发送消息;请访问postmaster.yahoo.com/abuse_smtp.html 了解更多信息,请访问 com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108) com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1889) com.sun .mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120) 在 SendFromYahoo.main(SendFromYahoo.java:46)【参考方案2】:

试试这个代码

public class SendMail 

    String host, port, emailid,username, password;
    Properties props = System.getProperties();
    Session l_session = null;

    public BSendMail() 
        host = "smtp.mail.yahoo.com";
        port = "587";
        emailid = "a@yahoo.com";
        username = "a";
        password = "pwd";

        emailSettings();
        createSession();
        sendMessage("a@yahoo.com", "rahul@gmail.com","Test","test Mail");
    

    public void emailSettings() 
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "false");
        props.put("mail.smtp.port", port);
//        props.put("mail.smtp.socketFactory.port", port);
//        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        props.put("mail.smtp.socketFactory.fallback", "false");

    

    public void createSession() 

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

        l_session.setDebug(true); // Enable the debug mode

    

    public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) 
        //System.out.println("Inside sendMessage 2 :: >> ");
        try 
            //System.out.println("Sending Message *********************************** ");
            MimeMessage message = new MimeMessage(l_session);
            emailid = emailFromUser;
            //System.out.println("mail id in property ============= >>>>>>>>>>>>>> " + emailid);
            //message.setFrom(new InternetAddress(emailid));
            message.setFrom(new InternetAddress(this.emailid));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail));
            message.setSubject(subject);
            message.setContent(msg, "text/html");

            //message.setText(msg);
            Transport.send(message);
            System.out.println("Message Sent");
         catch (MessagingException mex) 
            mex.printStackTrace();
         catch (Exception e) 
            e.printStackTrace();
        //end catch block
        return true;
    


【讨论】:

你好 Rahul,早上工作正常,但现在它给了我以下错误:知道为什么会这样,请告诉我。 DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com", port 587, isSSL false javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 587; 除了端口号(不是 587)而是 465 之外,以上所有内容都是正确的。希望这会有所帮助。

以上是关于使用 Javamail API 从 yahoo id 向其他电子邮件 id 发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

使用 IMAP(javamail API)从 gmail 访问电子邮件

HTML 使用JSON HTML从Yahoo API中提取新闻

使用JSON HTML从Yahoo API中获取新闻

从 Gmail 中检索未读电子邮件 - JavaMail API + IMAP

我无法从 Java Mail Api 发送 Yahoo Mail

如何使用 Yahoo Weather API 从 IP 地址获取客户端天气