在Android中发送电子邮件时出错

时间:2012-10-10 14:25:39

标签: android

BackupContactsActivity.java

package com.testing.backup;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BackupContactsActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    TextView result;
    String msg = "**********Backup Phone Contacts**********\n\n";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      

        Button backup = (Button)findViewById(R.id.btn);
        backup.setOnClickListener(this);
    }

    public void onClick(View view){

        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        while(cursor.moveToNext()){
        String name = cursor.getString(cursor.getColumnIndexOrThrow(Phone.DISPLAY_NAME));
        String number = cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER));

        msg += name + "\t\t\t" + number + "\n";         
        }
        result = (TextView)findViewById(R.id.txt);
        result.setText(msg.toString());

        // Recipient's email ID needs to be mentioned.
        String to = "testing@hotmail.com";
        // Sender's email ID needs to be mentioned
        String from = "testing@gmail.com";
        // Email Password
        String password = "testing";
        // Assuming you are sending email from localhost
        String host = "smtp.gmail.com";

        // Get system properties
        Properties properties = System.getProperties();
        // Setup mail server
        //properties.setProperty("mail.smtp.host", host);
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtps.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties, null);

        try{
        // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);
            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));
            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            // Set Subject: header field
            message.setSubject("This is the Backup Contacts Email!");
            // Now set the actual message
            message.setText(msg.toString());

            // Send message
            //Transport.send(message);
            Transport transport = session.getTransport("smtps");
    transport.connect(host, from, password);
    transport.sendMessage(message, message.getAllRecipients());
            System.out.println("Sent message successfully....");
            transport.close();
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

我的Logcat

10-10 12:42:33.773: E/AndroidRuntime(692): FATAL EXCEPTION: main
10-10 12:42:33.773: E/AndroidRuntime(692): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.backup/com.testing.backup.BackupContactsActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=692, uid=10057 requires android.permission.READ_CONTACTS
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.os.Looper.loop(Looper.java:137)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.main(ActivityThread.java:4424)
10-10 12:42:33.773: E/AndroidRuntime(692):  at java.lang.reflect.Method.invokeNative(Native Method)
10-10 12:42:33.773: E/AndroidRuntime(692):  at java.lang.reflect.Method.invoke(Method.java:511)
10-10 12:42:33.773: E/AndroidRuntime(692):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-10 12:42:33.773: E/AndroidRuntime(692):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-10 12:42:33.773: E/AndroidRuntime(692):  at dalvik.system.NativeStart.main(Native Method)
10-10 12:42:33.773: E/AndroidRuntime(692): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=692, uid=10057 requires android.permission.READ_CONTACTS
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.os.Parcel.readException(Parcel.java:1327)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.content.ContentResolver.query(ContentResolver.java:311)
10-10 12:42:33.773: E/AndroidRuntime(692):  at com.testing.backup.BackupContactsActivity.onCreate(BackupContactsActivity.java:32)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.Activity.performCreate(Activity.java:4465)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-10 12:42:33.773: E/AndroidRuntime(692):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-10 12:42:33.773: E/AndroidRuntime(692):  ... 11 more

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.testing.backup"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".BackupContactsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我想提取手机中的所有电话联系人(姓名+号码)并将它们放入字符串消息中,因此当我点击应用程序中的“备份”按钮时,这些联系人信息将直接发送到我的电子邮箱。我刚刚使用Eclipse模拟器尝试了这个,我已经在我的Manifest中添加了 READ_CONTACTS INTERNET 权限。但它出现了我的logcat中陈述的问题。有人可以帮我解决问题吗?谢谢... (我确实使用了发件人和收件人的真实电子邮件地址)

1 个答案:

答案 0 :(得分:2)

我认为这是由于清单文件中的uses-permission。检查manifest-permission的清单文件。