1

私はアンドロイドが初めてです。メール送信オプションを備えた新しいアプリを開発しています。メールを送信するために、gmail構成ホスト「smtp.gmail.com」、ポート465、SSL trueを使用しました。メールを送信するには、apache commons API があります。OnTouchイベントメール送信メソッドが呼び出します。ボタンをタッチするたびに、次のエラーが表示されます。

エラー : メソッド org.apache.commons.mail.Email.setMailSessionFromJNDI から参照されているクラス 'javax.naming.InitialContext' が見つかりませんでした
    警告: VFY: Lorg/apache/commons/mail/Email で新しいインスタンス 955 (Ljavax/naming/InitialContext;) を解決できません。
    警告: org.apache.commons.mail.EmailException: 次のサーバーへの電子メールの送信に失敗しました: smtp.gmail.com:465

マニフェスト ファイルに uses-permission android:name="android.permission.INTERNET" を追加しました。

Android ですべての Java ファイルを使用できますか?

電子メール コードは、スタンドアロンの Java プログラムとして正しく実行されました。

4

2 に答える 2

0

これは、私がアプリで行っていることの例です。ユーザーがフォームに入力して送信ボタンを押すと、ユーザーにメールを送信する独自​​のメール アカウントを持つアプリがあります。

重要: アプリでlibSMTP.jarファイルが参照されていることを確認してください。このライブラリを次のコードに使用しています。使用されている次のコードは次のとおりです。必要なものを取得してください。これが役立つことを願っています。

必要なインポート:

import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.commons.net.smtp.SimpleSMTPHeader;

電子メールの送信をリクエストする送信ボタン

 submit.setOnClickListener(new OnClickListener()
    {       
        public void onClick(View v)
        {
            //-- Submit saves data to sqlite db, but removed that portion for this demo...


            //-- Executes an new task to send an automated email to user when they fill out a form...
            new sendEmailTask().execute();
        }
        }
    });

別のスレッドで実行される電子メール タスク:

private class sendEmailTask extends AsyncTask<Void, Void, Void>
{

    @Override
    protected void onPostExecute(Void result) 
    {

    }

    @Override
    protected void onPreExecute() 
    {

    }

    @SuppressLint("ParserError")
    @Override
    protected Void doInBackground(Void... params) 
    {

        try {

            //--Note the send format is as follows: send(from, to, subject line, body message)
            send("myAppName@gmail.com",  "emailToSendTo@gmail.com", "Form Submitted",  "You submitted the form.");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

使用中の送信機能:

public void send(String from, String to, String subject, String text) throws IOException 
{
    SMTPClient client = new SMTPClient("UTF-8");
    client.setDefaultTimeout(60 * 1000);

    client.setRequireStartTLS(true); // requires STARTTLS
    //client.setUseStartTLS(true); // tries STARTTLS, but falls back if not supported
    client.setUseAuth(true); // use SMTP AUTH
    //client.setAuthMechanisms(authMechanisms); // sets AUTH mechanisms e.g. LOGIN

    client.connect("smtp.gmail.com", 587);
    checkReply(client);

    //--Note the following format is as follows: client.login("localhost", (...your email account being used to send email from...), (...your email accounts password ...));
    client.login("localhost", "myAppName@gmail.com", "...myAppName email account password...");
    checkReply(client);

    client.setSender(from);
    checkReply(client);
    client.addRecipient(to);
    checkReply(client);

    Writer writer = client.sendMessageData();

    if (writer != null) 
    {
        SimpleSMTPHeader header = new SimpleSMTPHeader(from, to, subject);
        writer.write(header.toString());
        writer.write(text);
        writer.close();
        client.completePendingCommand();
            checkReply(client);
    }

    client.logout();
    client.disconnect();
}

返信機能が使用されていることを確認します。

private void checkReply(SMTPClient sc) throws IOException 
{
    if (SMTPReply.isNegativeTransient(sc.getReplyCode())) 
    {
        sc.disconnect();
        throw new IOException("Transient SMTP error " + sc.getReplyCode());
    } 
    else if (SMTPReply.isNegativePermanent(sc.getReplyCode())) 
    {
        sc.disconnect();
        throw new IOException("Permanent SMTP error " + sc.getReplyCode());
    }
}
于 2013-03-12T13:22:43.083 に答える