有时电子邮件不会在谷歌应用引擎上发送

时间:2017-02-09 07:20:02

标签: java email google-app-engine gwt

我正在与GWT合作,使用Google App Engine电子邮件代码发送电子邮件。以下是我用来发送电子邮件的代码:

try {
    System.out.println("Inside Try Block");
    Properties props=new Properties();
    Session session=Session.getDefaultInstance(props,null);
    Message message = new MimeMessage(session);
    Multipart multiPart=new MimeMultipart();

    // To Mail  Address
    message.setFrom(new InternetAddress(myEmail));
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(myEmail));

    // Email To to Multiple Recipients 
    InternetAddress[] mailToMultiple = new InternetAddress[toEmailIdArray.length];
    for( int i=0; i<toEmailIdArray.length; i++ ){
        mailToMultiple[i] = new InternetAddress(toEmailIdArray[i]);
    }
    message.setRecipients(Message.RecipientType.TO,mailToMultiple );

    // Email CC to Multiple Recipients 
    InternetAddress[] ccmailToMultiple = new InternetAddress[ccEmailIdArray.length];
    for( int i=0; i<ccEmailIdArray.length; i++ ){
        ccmailToMultiple[i] = new InternetAddress(ccEmailIdArray[i]);
    }
    message.setRecipients(Message.RecipientType.CC, ccmailToMultiple);
    //below line for add BCC if needed then uncomment 
    //message.setRecipient(Message.RecipientType.BCC, new InternetAddress(approverEmail));

    // Mail Subject
    message.setSubject(mailSub);
    message.setText(message1);

    logger.log(Level.SEVERE, "Ready To send Email");
    Transport.send(message);
    logger.log(Level.SEVERE,"Email sent Successfully !!!!")

} catch (Exception e) {
   e.printStackTrace();
}

使用此代码,有时不会发送电子邮件。每当收到电子邮件时,我都会看到日志消息Ready To send Email,但不会看到日志消息Email sent Successfully !!!!

发送电子邮件时,我会收到一条日志消息; Email sent Successfully !!!!
通常我会收到电子邮件,但有时候不会发送电子邮件。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您的props props.load(.properties FileInputStream);props.put(key, value);

像这样

    Properties prop = new Properties();
    prop.load(new FileInputStream(SendTextMail.class.getResource("/mail.properties").getPath()));
    Session session = Session.getDefaultInstance(prop);

    Properties prop = new Properties();
    prop.put("mail.transport.protocol", "smtp");
    prop.put("mail.smtp.host", "localhost");
    prop.put("mail.smtp.port", 25);
    prop.put("mail.smtp.auth", "true");
    prop.put("mail.debug", "true"); 
    Session session = Session.getDefaultInstance(prop);