使用appengine开发服务器(java)测试邮件

使用appengine开发服务器(java)测试邮件,java,google-app-engine,email,Java,Google App Engine,Email,我正在使用javamail从appengine应用程序发送邮件。它在部署中工作得很好,但我不知道如何使用开发服务器来实现这一点。每当我需要测试sendmail时,我就不得不部署这个应用程序,这很烦人 背景信息(为什么日志不起作用): 我们知道电子邮件会进入appengine开发服务器上的日志。但是,希望从开发服务器发送电子邮件的主要原因是能够测试电子邮件的格式。看起来怎么样?是否需要对电子邮件模板进行更改,以使其在电子邮件客户端A、B和C中看起来良好,并且可以快速完成,而无需每次都部署到实际的默

我正在使用javamail从appengine应用程序发送邮件。它在部署中工作得很好,但我不知道如何使用开发服务器来实现这一点。每当我需要测试sendmail时,我就不得不部署这个应用程序,这很烦人

背景信息(为什么日志不起作用):

我们知道电子邮件会进入appengine开发服务器上的日志。但是,希望从开发服务器发送电子邮件的主要原因是能够测试电子邮件的格式。看起来怎么样?是否需要对电子邮件模板进行更改,以使其在电子邮件客户端A、B和C中看起来良好,并且可以快速完成,而无需每次都部署到实际的默认appengine版本

我们不是垃圾邮件发送者。我们不想规避任何形式的安全措施。简言之,我们希望能够合法地在一个或多个电子邮件客户端中看到真实的电子邮件,然后立即进行代码更改,这样我们就可以调整它们,而不必经历编辑、编译、等待5分钟以部署、测试、重复周期的艰苦过程。由于在每个电子邮件客户端如何呈现电子邮件方面没有标准,因此,通过尝试在许多客户端中实现某种功能,这一艰苦的过程被放大了

问题:

如何将Java Google App Engine开发服务器配置为从本地计算机或SMTP服务发送电子邮件,以测试发送到真实电子邮件客户端的电子邮件?

来自:

当应用程序在 开发服务器调用邮件 用于发送电子邮件的服务 消息被打印到日志中。这个 Java开发服务器不发送 电子邮件


因此,当您打算发送邮件时,只需检查日志,并确保它显示在那里。实际上不会发送真正的邮件。

从Eclipse中,选择运行菜单、调试配置…,然后选择应用程序的 配置选择Arguments选项卡,然后在“VM Arguments”部分中设置VM 类似这样的属性:

MimeMessage msg = new MimeMessage(session);
...
if ("1".equals(System.getProperty("mail.debug"))) {
    msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml")));
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Test;

public class MimeMessageTest {

    @Test
    public void tesstMimeMessage() throws MessagingException, FileNotFoundException, IOException {
        Session session = Session.getDefaultInstance(new Properties(), null);
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("admin@foo.bar", "Foo Admin"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress("baz@foo.bar", "Baz User"));
        msg.setSubject("Subject from admin e-mail to baz user");

        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText("test message and so on");
        mbp1.setContent("<h1>test message and so on in HTML</h1>", "text/html");

        // create the second message part
        MimeBodyPart mbp2 = new MimeBodyPart();

        // attach the file to the message
        FileDataSource fds = new FileDataSource("/tmp/fileToBeAttached");
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(fds.getName());

        // create the Multipart and add its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);

        // add the Multipart to the message
        msg.setContent(mp);

        if ("1".equals(System.getProperty("debug"))) {
            msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml")));
        }
    }
}

-Dmail.log\u mail\u level=WARNING-Dmail.log\u mail\u body=true

当我使用电子邮件服务实现时,我使用了一个很酷的提示。因此,如果您也使用
MimeMessage
,只需检查消息的格式是否符合预期,检查附件是否存在,HTML格式是否正确,图像是否正确引用等等,您就可以构建整个消息,在调试过程中,您可能会有如下代码:

MimeMessage msg = new MimeMessage(session);
...
if ("1".equals(System.getProperty("mail.debug"))) {
    msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml")));
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Test;

public class MimeMessageTest {

    @Test
    public void tesstMimeMessage() throws MessagingException, FileNotFoundException, IOException {
        Session session = Session.getDefaultInstance(new Properties(), null);
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("admin@foo.bar", "Foo Admin"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress("baz@foo.bar", "Baz User"));
        msg.setSubject("Subject from admin e-mail to baz user");

        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText("test message and so on");
        mbp1.setContent("<h1>test message and so on in HTML</h1>", "text/html");

        // create the second message part
        MimeBodyPart mbp2 = new MimeBodyPart();

        // attach the file to the message
        FileDataSource fds = new FileDataSource("/tmp/fileToBeAttached");
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(fds.getName());

        // create the Multipart and add its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);

        // add the Multipart to the message
        msg.setContent(mp);

        if ("1".equals(System.getProperty("debug"))) {
            msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml")));
        }
    }
}
每次执行此操作时,
MimeMessage
实例将保存到
emailSent.eml
。您可以使用电子邮件阅读器打开此文件并检查是否一切正常

当然,您需要使用-Dmail.debug=1参数执行应用程序

采用这种方法的附加文件、文本消息和html消息示例如下:

MimeMessage msg = new MimeMessage(session);
...
if ("1".equals(System.getProperty("mail.debug"))) {
    msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml")));
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Test;

public class MimeMessageTest {

    @Test
    public void tesstMimeMessage() throws MessagingException, FileNotFoundException, IOException {
        Session session = Session.getDefaultInstance(new Properties(), null);
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("admin@foo.bar", "Foo Admin"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress("baz@foo.bar", "Baz User"));
        msg.setSubject("Subject from admin e-mail to baz user");

        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText("test message and so on");
        mbp1.setContent("<h1>test message and so on in HTML</h1>", "text/html");

        // create the second message part
        MimeBodyPart mbp2 = new MimeBodyPart();

        // attach the file to the message
        FileDataSource fds = new FileDataSource("/tmp/fileToBeAttached");
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(fds.getName());

        // create the Multipart and add its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);

        // add the Multipart to the message
        msg.setContent(mp);

        if ("1".equals(System.getProperty("debug"))) {
            msg.writeTo(new FileOutputStream(new File("/tmp/sentEmail.eml")));
        }
    }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.Properties;
导入javax.activation.DataHandler;
导入javax.activation.FileDataSource;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Multipart;
导入javax.mail.Session;
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.MimeBodyPart;
导入javax.mail.internet.mimessage;
导入javax.mail.internet.MimeMultipart;
导入org.junit.Test;
公共类mimessagetest{
@试验
public void tesstMimeMessage()引发MessaginException、FileNotFoundException、IOException{
Session Session=Session.getDefaultInstance(新属性(),null);
MimeMessage msg=新MimeMessage(会话);
msg.setFrom(新的InternetAddress(“admin@foo.bar","Foo Admin"),;
msg.addRecipient(Message.RecipientType.TO,新的InternetAddress(“baz@foo.bar“,”Baz用户“);
msg.setSubject(“从管理员电子邮件发送给baz用户的主题”);
//创建并填充第一个消息部分
MimeBodyPart mbp1=新的MimeBodyPart();
mbp1.setText(“测试消息等”);
mbp1.setContent(“HTML中的测试消息等”、“text/HTML”);
//创建第二个消息部分
MimeBodyPart mbp2=新的MimeBodyPart();
//将文件附加到邮件
FileDataSource fds=新的FileDataSource(“/tmp/fileToBeAttached”);
mbp2.setDataHandler(新DataHandler(fds));
mbp2.setFileName(fds.getName());
//创建多部分并将其部分添加到其中
Multipart mp=新的MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
//将多部分添加到消息中
msg.setContent(mp);
if(“1”.equals(System.getProperty(“debug”)){
msg.writeTo(新文件outputstream(新文件(“/tmp/sentmail.eml”));
}
}
}

GAE使用JavaMail,因此让它工作起来并不太困难。有两件事你需要改变

首先是为STMP服务器正确设置JavaMail会话。为此,请使用
Session.getDefaultInstance
,而不是使用
Session.getInstance
,至少提供
mail.smtp.host
属性。请参阅,或者只是查找一个通用的JavaMail SMTP教程

第二个变化是,你需要停止GAE处理你的电子邮件。它之所以这样做是因为线路

rfc822=gm
在SDK jar中的META-INF/javamail.address.map中。您可以包含自己的地址映射(但这很烦人,因为我假设您只希望它用于调试),也可以从代码中修改地址映射。这很简单

session.setProtocolForAddress("rfc822", "smtp");

在第一步中创建的会话上。这会将您的所有电子邮件路由到标准SMTP处理程序。

您可以执行以下操作在开发服务器上设置电子邮件

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");
并正常使用会话发送ema