我已经弄清楚如何通过 Google 引擎接收电子邮件。现在我正在尝试发送确认电子邮件已收到,但我不断收到此错误:
com.jkimgroup.emailtospreadsheet.MailHandlerServlet doPost:
MessagingException: javax.mail.SendFailedException:
Send failure (javax.mail.MessagingException:
Could not connect to SMTP host: localhost, port: 25 (java.net.SocketException: Permission denied:
Attempt to access a blocked recipient without permission.))
我将“发件人”地址设置为我的管理员电子邮件。这是一条错误消息,可能我没有在 /WEB-INF/lib 文件夹中包含正确的 jar 文件?
我使用了谷歌的标准示例:https ://developers.google.com/appengine/docs/java/mail/usingjavamail
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
// ...
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
String msgBody = "...";
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("admin@example.com", "Example.com Admin"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("user@example.com", "Mr. User"));
msg.setSubject("Your Example.com account has been activated");
msg.setText(msgBody);
Transport.send(msg);
} catch (AddressException e) {
// ...
} catch (MessagingException e) {
// ...
}
更新 我开始想也许我应该使用 appengine 库而不是邮件处理,即使官方文档指向使用 JavaMail 1.4.7?
如果是这种情况,这里是我在代码中导入的库。
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.TimeZone;
import java.util.logging.Logger;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
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;
//Am I supossed to import this one instead of the javax.mail.* jars?
//import com.google.appengine.api.mail.MailService.Message;
import com.google.appengine.api.users.User;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
UPDATE 2 FIXED NOW 我将 Oracle 的 JavaMail JAR 添加到我的应用程序中,这确实导致了以下问题:
您需要的所有 JavaMail 类都包含在 App Engine SDK 中。不要将 Oracle® 的 JavaMail JAR 添加到您的应用程序中;如果你这样做,应用程序将抛出异常。
https://developers.google.com/appengine/docs/java/mail/?csw=1