无法连接到smtp主机:smtp.gmail.com,端口:465

时间:2014-12-14 16:38:09

标签: java android email

无法连接到smtp主机:smtp.gmail.com,端口:465 我已经搜索了很多次并且多次更改了端口但是每次都出现相同的错误,如端口567,25等等但没有用... 提前谢谢你 UserPage.java是

   package com.example.androidphp;

   import android.support.v7.app.ActionBarActivity;

   import android.annotation.SuppressLint;

   import android.annotation.TargetApi;

   import android.os.Build;

   import android.os.Bundle;

   import android.os.StrictMode;

   import android.util.Log;

   import android.view.MenuItem;

   import android.view.View;

   import android.widget.Button;

   import android.widget.TextView;


  public class UserPage extends ActionBarActivity {

  Button button;

  TextView txtvw;

  String text;

 @TargetApi(Build.VERSION_CODES.GINGERBREAD)

 @SuppressLint("NewApi")

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.userpage);

    txtvw = (TextView)findViewById(R.id.TextView01);

    button =(Button) findViewById(R.id.save);

    text = "";

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy);

    button.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

             try {   

                 System.out.println("in try");

                   GmailSender sender = new GmailSender("sidzyum@gmail.com", "yum12345");

                     sender.sendMail("This is Subject",   

                         "This is Body",   

                            "sidzyum@gmail.com",   

                            "sadiaasgharnov@gmail.com");  

                         System.out.println("after mail send ");

                    } catch (Exception e) {   

               Log.e("SendMail", e.getMessage(), e); 

                   e.printStackTrace();

                }

        }

     });

   }


@Override

public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar item clicks here. The action bar will

    // automatically handle clicks on the Home/Up button, so long

    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    if (id == R.id.action_settings) {

        return true;

    }

    return super.onOptionsItemSelected(item);

}

 }

和GmailSender.java是

   package com.example.androidphp;

   import javax.activation.DataHandler;   

   import javax.activation.DataSource;   

   import javax.mail.Message;   

   import javax.mail.PasswordAuthentication;   

   import javax.mail.Session;   

   import javax.mail.Transport;   

   import javax.mail.internet.InternetAddress;   

   import javax.mail.internet.MimeMessage;   

   import java.io.ByteArrayInputStream;   

   import java.io.IOException;   

   import java.io.InputStream;   

   import java.io.OutputStream;   

   import java.security.Security;   

   import java.util.Properties;   

  public class GmailSender extends javax.mail.Authenticator {   

   private String mailhost = "smtp.gmail.com";   

   private String user;   

   private String password;   

   private Session session;   

   static {   

    Security.addProvider(new com.example.androidphp.JSSEProvider());   

 }  

public GmailSender(String user, String password) {   

    this.user = user;   

    this.password = password;   

    Properties props = new Properties();   

    props.setProperty("mail.transport.protocol", "smtp");   

    props.setProperty("mail.host", mailhost);   

    props.put("mail.smtp.auth", "true");   

    props.put("mail.smtp.port", "25");   

    props.put("mail.smtp.socketFactory.port", "25");   

    props.put("mail.smtp.socketFactory.class",   

            "javax.net.ssl.SSLSocketFactory");   

    props.put("mail.smtp.socketFactory.fallback", "false");   

    props.setProperty("mail.smtp.quitwait", "false");   

    session = Session.getDefaultInstance(props, this);   

}   

protected PasswordAuthentication getPasswordAuthentication() {   

    return new PasswordAuthentication(user, password);   

}   

public synchronized void sendMail(String subject, String body, String sender, String recipients) 

throws Exception {   

    MimeMessage message = new MimeMessage(session);   

    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), 

    "text/plain"));   

    message.setSender(new InternetAddress(sender));   

    message.setSubject(subject);   

    message.setDataHandler(handler);   

    if (recipients.indexOf(',') > 0)   

        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   

    else  

        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   

    Transport.send(message);   

}   

public class ByteArrayDataSource implements DataSource {   

    private byte[] data;   

    private String type;   

    public ByteArrayDataSource(byte[] data, String type) {   

        super();   

        this.data = data;   

        this.type = type;   

    }   

    public ByteArrayDataSource(byte[] data) {   

        super();   

        this.data = data;   

    }   

    public void setType(String type) {   

        this.type = type;   

    }   

    public String getContentType() {   

        if (type == null)   

            return "application/octet-stream";   

        else  

            return type;   

    }   


    public InputStream getInputStream() throws IOException {   

        return new ByteArrayInputStream(data);   

    }   

    public String getName() {   

        return "ByteArrayDataSource";   

    }   

    public OutputStream getOutputStream() throws IOException {   

        throw new IOException("Not Supported");   

    }   

  }   

 }  

和JSSEProvider是

  package com.example.androidphp;

 import java.security.Provider;


 import java.security.AccessController;

 public class JSSEProvider extends Provider {

     /**

     * 

     */

  private static final long serialVersionUID = 1L;

   public JSSEProvider() {

        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");

        AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {

            public Void run() {

                put("SSLContext.TLS",

                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");

                put("Alg.Alias.SSLContext.TLSv1", "TLS");

                put("KeyManagerFactory.X509",

                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");

                put("TrustManagerFactory.X509",

                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");

                return null;

            }

        });

     }

  }

3 个答案:

答案 0 :(得分:2)

你是告诉它使用ssl吗?

添加以下行:

props.put("mail.smtp.starttls.enable","true");

在您的GmailSender构造函数中。

答案 1 :(得分:0)

保持端口号:为465 .... 尝试将会话值更改为

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

还可以通过点击以下链接更改帐户中安全性较低的应用的安全设置: https://www.google.com/settings/security/lesssecureapps

答案 2 :(得分:0)

关闭防病毒软件或添加例外。为我工作。并使用应用程序专用密码(如果它的Gmail)