gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:09:13 24 4
gpt4 key购买 nike

无法连接到 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;

}

});

}

}

最佳答案

你要告诉它使用 ssl 吗?

添加此行:

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

在 GmailSender 构造函数中。

关于java - 无法连接到 smtp 主机 :smtp. gmail.com,端口:465,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27471454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com