有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用google应用程序引擎发送电子邮件

我尝试使用谷歌应用程序引擎发送一封简单的电子邮件,其中包含此代码。 但是什么都没有发生,为了使用邮件api,我需要配置什么吗? 这在本地主机上运行。 我使用gmail作为邮件主机

   String host = "smtp.google.com";
String to = "example@yahoo.fr";
String from = "example@gmail.com";
String subject = "this is a test";
String messageText = "test";
boolean sessionDebug = false;
// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");
Session mailSession = Session.getDefaultInstance(props, null);

// Set debug on the Session
// Passing false will not echo debug info, and passing True will.

mailSession.setDebug(sessionDebug);

// Instantiate a new MimeMessage and fill it with the 
// required information.

Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

// Hand the message to the default transport service
// for delivery.

Transport.send(msg);

共 (4) 个答案

  1. # 1 楼答案

    除了电子邮件不能在本地主机上运行,或者由于发件人的电子邮件不是经过身份验证的电子邮件,我已经体验到,即使版本不是默认版本,电子邮件也不能运行。我在任何地方都找不到这个文件

    例如: nondefaultversion-dot-myapp.appspot.com(电子邮件不起作用,没有错误日志) myapp.appspot.com(电子邮件有效)

    请确认其他人是否也面临过这个问题

  2. # 2 楼答案

    显然,GAE不再允许使用管理员帐户。您需要使用服务帐户:project-id@appspot.gserviceaccount.com

    我以前的项目仍然使用管理员帐户,但最近创建的项目不允许我使用任何管理员帐户

  3. # 3 楼答案

    在本地运行AppEngine development server时,通过邮件服务发送的任何内容实际上都不会被发送,只会被记录到控制台

    here

    When an application running in the development server calls the Mail service to send an email message, the message is printed to the log. The Java development server does not send the email message.

    此外,from地址必须是(从here

    • 应用程序管理员的电子邮件
    • 当前登录用户使用谷歌帐户登录的电子邮件
    • 来自应用的有效电子邮件接收地址
  4. # 4 楼答案

    发件人应该是你自己的Gmail电子邮件地址,而不是example@gmail.com

    原因是SMTP服务器需要对您进行身份验证