我想发邮件。但它显示以下错误

时间:2015-03-30 09:47:43

标签: android

03-30 14:38:17.117: W/System.err(827): javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
03-30 14:38:17.117: W/System.err(827):   nested exception is:
03-30 14:38:17.117: W/System.err(827):  javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
03-30 14:38:17.136: W/System.err(827):  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
03-30 14:38:17.136: W/System.err(827):  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
03-30 14:38:17.136: W/System.err(827):  at javax.mail.Service.connect(Service.java:310)
03-30 14:38:17.136: W/System.err(827):  at javax.mail.Service.connect(Service.java:169)
03-30 14:38:17.146: W/System.err(827):  at javax.mail.Service.connect(Service.java:118)
03-30 14:38:17.146: W/System.err(827):  at javax.mail.Transport.send0(Transport.java:188)
03-30 14:38:17.146: W/System.err(827):  at javax.mail.Transport.send(Transport.java:118)
03-30 14:38:17.156: W/System.err(827):  at com.example.mailsenderactivity.GMailSender.sendMail(GMailSender.java:61)
03-30 14:38:17.156: W/System.err(827):  at com.example.mailsenderactivity.MainActivity$1.onClick(MainActivity.java:38)
03-30 14:38:17.166: W/System.err(827):  at android.view.View.performClick(View.java:3480)
03-30 14:38:17.166: W/System.err(827):  at android.view.View$PerformClick.run(View.java:13983)
03-30 14:38:17.176: W/System.err(827):  at android.os.Handler.handleCallback(Handler.java:605)
03-30 14:38:17.186: W/System.err(827):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 14:38:17.186: W/System.err(827):  at android.os.Looper.loop(Looper.java:137)
03-30 14:38:17.186: W/System.err(827):  at android.app.ActivityThread.main(ActivityThread.java:4340)
03-30 14:38:17.197: W/System.err(827):  at java.lang.reflect.Method.invokeNative(Native Method)
03-30 14:38:17.205: W/System.err(827):  at java.lang.reflect.Method.invoke(Method.java:511)
03-30 14:38:17.205: W/System.err(827):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-30 14:38:17.216: W/System.err(827):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-30 14:38:17.216: W/System.err(827):  at dalvik.system.NativeStart.main(Native Method)
03-30 14:38:17.226: W/System.err(827): Caused by: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

1 个答案:

答案 0 :(得分:0)

在项目中复制此代码并进行检查!

import java.util.Properties;

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

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
       import android.widget.Toast;

   public class MainActivity extends Activity implements OnClickListener {

Button button;
Session session;
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    SendMailBySite bySite=new SendMailBySite();
    bySite.execute();

}


class SendMailBySite extends AsyncTask<Void, Void, Void>{  



    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog=new ProgressDialog(MainActivity.this);
        dialog.setMessage("Sending mail.....");
        dialog.show();

    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

          final String user="";//change accordingly  
          final String password="";//change accordingly  

          String to="";//change accordingly  

           //Get the session object  
          Properties props = new Properties();
          props.put("mail.smtp.host", "smtp.gmail.com");
             props.put("mail.smtp.socketFactory.port", "465");
             props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
             props.put("mail.smtp.auth", "true");
             props.put("mail.smtp.port", "465");

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

           //Compose the message  
            try {  
             MimeMessage message = new MimeMessage(session);  
             message.setFrom(new InternetAddress(user));  
             message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
             message.setSubject("javatpoint");  
             message.setText("This is simple program of sending email using JavaMail API");  

            //send the message  
             Transport.send(message);  



             } catch (MessagingException e) {e.printStackTrace();} 


        return null;


    }  
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        dialog.dismiss();
         Toast.makeText(getApplicationContext(), "message sent successfully...", 3000).show();
    }
    }  
}