testing mail with appengine development server (java)

后端 未结 6 612
Happy的楠姐
Happy的楠姐 2021-02-02 17:23

I\'m using javamail to send mails from my appengine application. It works perfectly in the deployment, but I can\'t figure out how to do this using the development server. Whene

6条回答
  •  旧时难觅i
    2021-02-02 17:34

    You can do the following for setting up email on the development server

    final String username = "xxxxxxxxx@gmail.com";//change accordingly
    final String password = "xxxxxxx";//change accordingly
    
    // Assuming you are sending email through gmail
    String host = "smtp.gmail.com";
    
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", "587");
    
    // Get the Session object.
    Session session = Session.getInstance(props,
    new javax.mail.Authenticator() {
     protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
     }
    });
    session.setProtocolForAddress("rfc822", "smtp");
    

    And use the session normally to send emails:

    try {
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("xxxx@gmail.com", "xxxx"));
        msg.addRecipient(Message.RecipientType.TO,
        new InternetAddress("user@testdomain.com,"Mr. User"));
        msg.setSubject("Test email from GAE/J development");
        msg.setText("This is test:);
        Transport.send(msg);
    
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    Additionally you need to add the following two libraries in build path and under war/WEB-INF/lib:

    • javax.mail.jar
    • javax.activation.jar

    You can find the links easily by googling them.

    Finally if you want to use gmail as the smtp server, you need to go to your account, and enable access for less seucre apps https://www.google.com/settings/security/lesssecureapps

提交回复
热议问题