2015-09-21 7 views
0
Session session = null; 
ProgressDialog pdialog = null; 
Context context = null; 
EditText reciep, sub, msg; 
String rec, subject, textMessage; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.b9); 

    context = this; 

    Button login = (Button) findViewById(R.id.btn_submit); 
    reciep = (EditText) findViewById(R.id.et_to); 
    sub = (EditText) findViewById(R.id.et_sub); 
    msg = (EditText) findViewById(R.id.et_text); 

    login.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    rec = reciep.getText().toString(); 
    subject = sub.getText().toString(); 
    textMessage = msg.getText().toString(); 
    Properties props = new Properties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.socketFactory.port", "587"); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "587"); 

    session = Session.getDefaultInstance(props, new Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("[email protected]gmail.com", "xyz123"); 
     } 
    }); 

    pdialog = ProgressDialog.show(context, "", "Sending Mail...", true); 

    RetrieveFeedTask task = new RetrieveFeedTask(); 
    task.execute(); 
    } 

RetrieveFeedTaskAndroid Отправка почты GMAIL с использованием SMTP-

class RetrieveFeedTask extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 

     try{ 
      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO, InternetAddress. 
        parse(rec)); 
      message.setSubject(subject); 
      message.setContent(textMessage, "text/html; charset=utf-8"); 
      Transport.send(message); 
     } catch(MessagingException e) { 
      e.printStackTrace(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     pdialog.dismiss(); 
     reciep.setText(""); 
     msg.setText(""); 
     sub.setText(""); 
     Toast.makeText(getApplicationContext(), "Message sent",  Toast.LENGTH_LONG).show(); 
    } 
} 
} 

и я получаю следующее сообщение об ошибке, я не мог в состоянии получать почту в своем почтовом ящике

ОШИБКА:

  javax.mail.MessagingException: Could not connect to SMTP host:   smtp.gmail.com, port: 587; 
      nested exception is: 
      avax.net.ssl.SSLHandshakeException: j  avax.net.ssl.SSLProtocolException: SSL  handshake aborted: ssl=0x755e6040: Failure in SSL library, usually a protocol error 
                            at javax.mail.Service.connect(Service.java:310) 
       at javax.mail.Service.connect(Service.java:169) 
       at javax.mail.Service.connect(Service.java:118) 
       at javax.mail.Transport.send0(Transport.java:188) 
       at javax.mail.Transport.send(Transport.java:118) 


       at android.os.AsyncTask$2.call(AsyncTask.java:288) 
       at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
         java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
       at  java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
       at java.lang.Thread.run(Thread.java:841) 
       Caused by: javax.net.ssl.SSLHandshakeException:  javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x755e6040:  Failure in SSL library, usually a protocol error 
       error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown  protocol (external/openssl/ssl/s23_clnt.c:769 0x72fb0d74:0x00000000) 
      at mpl.getInputStream(OpenSSLSocketImpl.java:633) 
﹕  
       getSelectedText on inactive InputConnection 
        getSelectedText on inactive InputConnection 
        getSelectedText on inactive InputConnection 
        getSelectedText on inactive InputConnection 
        getSelectedText on inactive InputConnection 
        getSelectedText on inactive InputConnection 
        getSelectedText on inactive InputConnection 
        getSelectedText on inactive InputConnection 

getSelectedText on inactive InputConnection 
getTextBeforeCursor on inactive InputConnection 
+0

Когда я нажимаю кнопку он отображает сообщения, отправленные, но я не получал никаких писем в моем GMAIL ид представить. dDoes кто-нибудь мне помогает. Я так сильно поражен этим – user000

ответ

0

Если вы используете SSL, правильный порт составляет 465. 587 для TLS

https://support.google.com/mail/answer/13287?hl=en

Если это не проблема, попробуйте после этого урока

http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

+0

Мне нужно подключение, связанное с android, которое я пробовал с некоторым кодом, но я не получаю – user000

+0

Любое новое сообщение об ошибке или оно то же самое? Кроме того, вы установили учетную запись gmail для приема попыток входа в менее безопасные приложения? http://stackoverflow.com/questions/21937586/phpmailer-smtp-error-password-command-failed-when-send-mail-from-my-server – Lukehey

Смежные вопросы