5

次のコードを見てください。

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 アクセスが拒否されました

Yahooのメールアドレスとパスワードは正しく付与されていますが、この問題は続いています。どうしてこれなの?助けてください!

4

5 に答える 5

2

ログオンするときは、「@yahoo.com」を使用せず、ユーザー名のみを使用してみてください

于 2012-09-21T15:31:29.640 に答える
0

パラメータを変更する必要があります。Yahoo コードに「Gmail」、Gmail コードに「Yahoo」を渡しました。これらのパラメーターを変更します。

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

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

また、SMTP Host も変更する必要があります。

それはうまくいくはずです。

于 2013-12-13T07:47:37.403 に答える
0

ソケットファクトリを取り除きます。 Yahoo Mail への接続の詳細については、JavaMail FAQ を参照してください

于 2012-09-21T19:25:08.977 に答える
0

コードで gmail と yahoo の使用方法に違いがあるのはなぜですか? SocketFactory の 2 行の代わりに、for yahoo も
使用してみましたか?start.tls.enable=true

于 2012-09-21T15:29:32.443 に答える