简体   繁体   English

在GAE上使用Servlet发送电子邮件

[英]Sending email with Servlet on GAE

Am writing the code as follow in my application 在我的应用程序中编写代码如下

 public void send_email(String email)
 {
     Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.sendgrid.net");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("my_username","my_password");
                    }
                });

        Message message = new MimeMessage(session);
        try {

            message.setFrom(new InternetAddress("from@no-spam.com"));

            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("ramesh@abc.com"));

            message.setSubject("Testing Subject");

            message.setText("Dear Mail Crawler," +
                    "\n\n No spam to my email, please!");

            Transport.send(message);                


            System.out.println("Done");
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


 }

This is method is called in my servlet class. 这个方法在我的servlet类中被调用。 After that it executed well and give a message as " Done " on console , but i didnt receive any email in Email-inbox. 之后,它执行得很好,并在控制台上显示为“ Done ”的消息,但我没有在“电子邮件收件箱”中收到任何电子邮件。

If i run this same code as java application it works fine and received an email. 如果我运行与Java应用程序相同的代码,则可以正常工作并收到电子邮件。

But when i run it on Google web server its not working.. And one thing, here i removed both javaee.jar file and mail.jar files from lib, but still it didn't give any error.. 但是,当我在Google Web服务器上运行它时,它无法正常工作。还有一件事,这里我从lib中删除了javaee.jar文件和mail.jar文件,但仍然没有出现任何错误。

Give me any suggestions guys.... 给我任何建议的家伙。

But when i run it on Google web server its not working.. 但是,当我在Google Web服务器上运行它时,它无法正常工作。

"Google web server" in your case means Google AppEngine? 在您的情况下,“ Google Web服务器”是指Google AppEngine? If so then you cannot use the full JavaMail API but must use Google's infrastructure . 如果是这样,则您不能使用完整的JavaMail API,而必须使用Google的基础架构

An app cannot use the JavaMail interface to connect to other mail services for sending or receiving email messages. 应用程序无法使用JavaMail界面连接到其他邮件服务以发送或接收电子邮件。 SMTP configuration added to the Transport or Session is ignored. 添加到传输或会话的SMTP配置将被忽略。

Be aware you cannot send an email from just any email address. 请注意,您不能仅从任何电子邮件地址发送电子邮件。 You need to use one that is authorized in your app's domain. 您需要使用应用程序域中授权的一种。

The email address of the sender, the From address. 发件人的电子邮件地址,发件人地址。 The sender address must be one of the following types: 发件人地址必须是以下类型之一:

  • The address of a registered administrator for the application. 该应用程序的注册管理员的地址。 You can add administrators to an application using the Administration Console. 您可以使用管理控制台将管理员添加到应用程序中。

  • The address of the user for the current request signed in with a Google Account. 使用Google帐户登录的当前请求的用户地址。 You can determine the current user's email address with the Users API. 您可以使用Users API确定当前用户的电子邮件地址。 The user's account must be a Gmail account, or be on a domain managed by Google Apps. 该用户的帐户必须是Gmail帐户,或者位于由Google Apps管理的域上。

  • Any valid email receiving address for the app (such as xxx@APP-ID.appspotmail.com). 该应用程序的任何有效电子邮件接收地址(例如xxx@APP-ID.appspotmail.com)。

  • Any valid email receiving address of a domain account, such as support@example.com. 域帐户的任何有效电子邮件接收地址,例如support@example.com。 Domain accounts are accounts outside of the Google domain with email addresses that do not end in @gmail.com or @APP-ID.appspotmail.com. 域帐户是Google域之外的帐户,其电子邮件地址不以@ gmail.com或@ APP-ID.appspotmail.com结尾。

https://developers.google.com/appengine/docs/python/mail/sendingmail https://developers.google.com/appengine/docs/python/mail/sendingmail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM