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

我在清单文件中添加了使用权限 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 回答
0

从 Apache Commons Net 3.3,您可以将 jar 放到您的类路径中并开始使用 AuthenticationSMTPClient:http ://blog.dahanne.net/2013/06/17/sending-a-mail-in-java-and-android -with-apache-commons-net/

于 2013-06-18T03:26:28.670 回答