gpt4 book ai didi

Java发送邮件,激活需要时间

转载 作者:行者123 更新时间:2023-12-02 05:26:59 27 4
gpt4 key购买 nike

我需要使用 Gmail 的 SMTP 和 javax.mail api 发送邮件。我正在使用的相同代码在 Android 中成功运行,如果我将它带到 Java 应用程序或尝试在 Java Web 应用程序中使用它,它就会开始出现麻烦。我花了很多时间试图理解有什么区别,但没办法!我的代码如下:

public class GMailSender extends Authenticator
{

private final String mailhost;
private final String password;
private final Session session;
private final String user;

public GMailSender(String username, String password)
{
this.mailhost = "smtp.gmail.com";
this.user = username;
this.password = password;
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.host", mailhost);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.debug", "true");
properties.setProperty("mail.smtp.quitwait", "false");

System.out.println("Creating session ...");
session = Session.getInstance(properties, this);
System.out.println("Session createed ...");
}

@Override
protected PasswordAuthentication getPasswordAuthentication()
{
System.out.println("Authintecation ...");
return new PasswordAuthentication(user, password);
}

public synchronized void sendMail(String subject, String dataHandler, String senderAddress, String recepeintAddress)
throws Exception
{

MimeMessage mimemessage;
mimemessage = new MimeMessage(session);
DataHandler datahandler = new DataHandler(new ByteArrayDataSource(dataHandler.getBytes(), "text/plain"));
mimemessage.setSender(new InternetAddress(senderAddress));
mimemessage.setSubject(subject);
mimemessage.setDataHandler(datahandler);
mimemessage.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recepeintAddress));
System.out.println("Sending ...");
Transport transport = session.getTransport("smtp");
transport.send(mimemessage);
System.out.println("Sent!");
}

static {

Security.addProvider(new JSSEProvider());
}


public static void main(String[] args){
System.out.println("Starting email ...");
GMailSender sender = new GMailSender("myEmail@gmail.com", "my password");
try {
sender.sendMail("Test", "alot of data", "myEmail@gmail.com", "someonesemail@gmail.com");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


public final 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>() {
@Override
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;
}
});
}
}

当我运行代码时,我得到以下结果:

> Starting email ... 
> Creating session ...
> Session createed ...
> Sending ...
> Authintecation ...

然后大约需要 10 分钟才能返回以下内容:

> javax.mail.MessagingException: Could not connect to SMTP host:
> smtp.gmail.com, port: 465, response: -1
> at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1379)
> at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
> 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 com.srycrm.mail.GMailSender.sendMail(GMailSender.java:66)
> at org.apache.jsp.send_jsp._jspService(send_jsp.java:85)
> at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
> at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
> at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
> at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
> at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
> at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
> at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

谁能帮帮我!!谢谢。

最佳答案

好的,您正在使用端口 465,因此将 ssl mail.smtp.ssl.enable 启用为 true :

properties.put("mail.smtp.ssl.enable", "true");

如果不起作用,则使用 properties.put("mail.smtp.starttls.enable", "true"); 并将端口更改为 587看看是否有帮助。

关于Java发送邮件,激活需要时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25923346/

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