0

次のコードを使用してメールを送信しています

public class Mail
{
    public static void main(String[] args)
    {
                String[] to={"abcd@xyz.co"};
                String[] cc={"pqrs@xyz.com"};
                String subject = "Subject";
                String body = "This is Body....!!";

        Mail.sendMail("From@gmail.com","Password","smtp.gmail.com","465","true",
       "true",true,"javax.net.ssl.SSLSocketFactory","false",to,cc,
       subject,body);             
    }



  public synchronized static boolean sendMail(String userName,String passWord,String host,String port,String starttls,String auth,boolean debug,String socketFactoryClass,String fallback,String[] to,String[] cc,String subject,String text)
{
            Properties props = new Properties();
            //Properties props=System.getProperties();
    props.put("mail.smtp.user", userName);
    props.put("mail.smtp.host", host);
            if(!"".equals(port))
    props.put("mail.smtp.port", port);
            if(!"".equals(starttls))
    props.put("mail.smtp.starttls.enable",starttls);
    props.put("mail.smtp.auth", auth);
            if(debug){
            props.put("mail.smtp.debug", "true");
            }else{
            props.put("mail.smtp.debug", "false");         
            }
            if(!"".equals(port))
    props.put("mail.smtp.socketFactory.port", port);
            if(!"".equals(socketFactoryClass))
    props.put("mail.smtp.socketFactory.class",socketFactoryClass);
            if(!"".equals(fallback))
    props.put("mail.smtp.socketFactory.fallback", fallback);

    try
    {
                    Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);
        MimeMessage msg = new MimeMessage(session);
        msg.setText(text);
        msg.setSubject(subject);
        msg.setFrom(new InternetAddress("From@gmail.com"));
                    for(int i=0;i<to.length;i++){
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
                    }
                    for(int i=0;i<cc.length;i++){
        msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
                    }
        msg.saveChanges();
                    Transport transport = session.getTransport("smtp");
                    transport.connect(host, userName, passWord);
                    transport.sendMessage(msg, msg.getAllRecipients());
                    transport.close();
                    return true;
    }
    catch (Exception mex)
    {
        mex.printStackTrace();
                    return false;
    }
    }

コードは正常に機能しており、メッセージは送信されています。しかし、メッセージが送信されていない場合 (有効なメール ID ではないため abcd@xyz.com に送信しようとした場合など) にフラグ (flag=false など) を設定したいと考えています。誰かがこれについて私を助けることができれば本当に感謝しています.

前もって感謝します

4

2 に答える 2

2

おそらく、実際の電子メールを入れて、その受信ボックスで配信不能レポートmsg.setFrom()を確認する必要があります。

ほとんどのサーバーは、あきらめる前に数回メールを送信しようとするため、メールが有効かどうかをすぐに判断することはできません.

于 2013-01-04T05:29:59.070 に答える
-1

Authenticatorの作成中にオブジェクトを渡す必要がありますSession

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

Authenticator - ユーザー名とパスワードが必要な場合に、アプリケーションにコールバックするために使用される Authenticator オブジェクト。

説明付きの実例を見つけてください -リンク

于 2013-01-04T05:21:00.993 に答える