Java 邮件 : Unable to send email via Yahoo

标签 java email jakarta-mail yahoo

请看下面的代码。

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 Access denied

Yahoo 邮箱地址和密码输入正确,但问题不断。为什么是这样?请帮忙!

最佳答案

登录时尽量不要使用“@yahoo.com”,只使用您的用户名

关于Java 邮件 : Unable to send email via Yahoo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12533394/

相关文章:

java mail - 处理csv附件

JavaMail POP3 邮件客户端错误

java - 使用java从HTML中提取数据

java - JSF 登录过滤器

java - Android ScheduledThreadPoolExecutor 立即执行延迟任务

Python的 `email.message.as_string`将某些部分编码为base64;不清楚为什么

java - 从 CSV 文件读取速度慢

java - 为什么 getPersonal() 在下面的情况下返回 null?

php - 在 PHP 中读取电子邮件并将数据保存到数据库

java - JavaMail 线程安全中的 IMAPStore 和 IMAPFolder 吗?