Java Mail: Unable to send email via Yahoo

别等时光非礼了梦想. 提交于 2019-12-23 12:27:39

问题


Please have a look at the following code.

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());
        }
    }
}

In here, if I select GMail, no issue. But if I try to send email using Yahoo, the following error occurs

javax.mail.AuthenticationFailedException: 530 Access denied

Yahoo mail address and password is given correctly, but this issue is keep on coming. Why is this? Please help!


回答1:


When logging on try not using the "@yahoo.com", just your user name




回答2:


Why do you have differentiation between the way you're using gmail and yahoo at your code?
Have you tried using start.tls.enable=true for yahoo as well, instead of the two lines with the SocketFactory?




回答3:


Get rid of the socket factory. See the JavaMail FAQ for details on connecting to Yahoo Mail.




回答4:


You need change the parameters. You have passed "Gmail" for Yahoo Code and "Yahoo" for the Gmail Code. Change those parameters.

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

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

And SMTP Host also Should be change.

It should work.




回答5:


Don't forget to disable less secure signin here 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", "**********");
              }
           });`


来源:https://stackoverflow.com/questions/12533394/java-mail-unable-to-send-email-via-yahoo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!