Java Mail:无法通过 Yahoo 发送电子邮件

问题描述 投票:0回答:6

请看下面的代码。

package email;

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

public class SendEmail
{

    private String to, from, bcc, cc, account, message,  password, subject;

    public SendEmail(String to, String from,String bcc, String cc, String account, String message, String password, String subject)
    {
        setFrom(from);
        setTo(to);
        setBCC(bcc);
        setCC(cc);
        setAccount(account);
        setMessage(message);
        //setUserName(userName);
        setPassword(password);
        setSubject(subject);


    }


    //Setters
    public void setFrom(String from)
    {
        this.from = from;
    }

    public void setTo(String to)
    {
        this.to = to;
    }

    public void setBCC(String bcc)
    {
        this.bcc = bcc;
    }

    public void setCC(String cc)
    {
        this.cc = cc;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

   /* public void setUserName(String userName)
    {
        this.userName = userName;
    }*/

    public void setPassword(String password)
    {
        this.password = password;
    }

    public void setAccount(String account)
    {
        this.account = account;
    }

    public void setSubject(String subject)
    {
        this.subject = subject;
    }



    //Getters
    public String getFrom()
    {
        return from;
    }

    public String getTo()
    {
        return to;
    }

    public String getBcc()
    {
        return bcc;
    }

    public String getCC()
    {
        return cc;
    }

    public String getMessage()
    {
        return message;
    }

    /*public String getUserName()
    {
        return userName;
    }*/

    public String getPassword()
    {
        return password;
    }

    public String getAccount()
    {
        return account;
    }

    public String getSubject()
    {
        return subject;
    }


    //This method is used to send the email
    public String send()
    {
        String result = "";
        try
        {
            Session mailSession = Session.getInstance(getProperties(), new PasswordAuthenticator());

            MimeMessage msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress(from));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            msg.setSubject(subject);
            msg.setText(message);

            Transport.send(msg);

            result = "Mail Sent";
        }
        catch(Exception e)
        {
            result = "An error Occured";
            e.printStackTrace();
        }

        return result;
    }


    //This method will return properties appropreiate for the email account
    public Properties getProperties()
    {
        Properties props = new Properties();

        if(getAccount().equals("GMail"))
        {
            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");
        }
        else if(getAccount().equals("Yahoo"))
        {
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.socketFactory.port","465");
            props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.host","smtp.mail.yahoo.com");
            props.put("mail.smtp.port","465");
        }

        return props;
    }



    //This class Authnticates the password
    private class PasswordAuthenticator extends Authenticator
    {

        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(getFrom(), getPassword());
        }
    }
}

在这里,如果我选择GMail,没有问题。但如果我尝试使用 Yahoo 发送电子邮件,则会出现以下错误

javax.mail.AuthenticationFailedException:530 访问被拒绝

雅虎邮件地址和密码给出正确,但这个问题不断出现。为什么是这样?请帮忙!

java email jakarta-mail yahoo
6个回答
2
投票

登录时尝试不要使用“@yahoo.com”,仅使用您的用户名


0
投票

为什么在代码中使用 gmail 和 yahoo 的方式有区别?
您是否也尝试过对 yahoo 使用

start.tls.enable=true
,而不是 SocketFactory 的两行?


0
投票

摆脱套接字工厂。有关连接到 Yahoo Mail 的详细信息,请参阅 JavaMail 常见问题解答


0
投票

您需要更改参数。您已经为 Yahoo 代码传递了“Gmail”,为 Gmail 代码传递了“Yahoo”。更改这些参数。

if(getAccount().equals("Yahoo"));

else if(getAccount().equals("GMail"));

并且 SMTP 主机也应该更改。

应该可以。


0
投票

到目前为止,您无法使用 SMTP 从 Java 发送电子邮件,因为 Yahoo 需要应用程序密码,并且他们至少 1 年不提供设置应用程序密码。


-1
投票

不要忘记在此处禁用不太安全的登录https://login.yahoo.com/account/security?el=1&done=https%3A%2F%2Fwww.yahoo.com&crumb=Fb3iREAVZwY&.scrumb=k3h5YYmGz%2Fw&guccounter= 1

    Properties props = System.getProperties();
    // Setup mail server
    props.put("mail.smtp.host", "smtp.mail.yahoo.com");
    props.put("mail.smtp.port", 465);
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.ssl.enable", true);
    props.put("mail.debug", "true");

    // Setup authentication, get session
    return Session.getInstance(props, new javax.mail.Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication("*********@yahoo.com", "**********");
              }
           });`
© www.soinside.com 2019 - 2024. All rights reserved.