使用GAE寄信

方法一

寄送純文字信件

try {
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress("admin@taicomjp.info", "admin"));
    msg.addRecipient(Message.RecipientType.TO,
                      new InternetAddress("admin@taicomjp.info", "Mr. Admin"));
    msg.setSubject("GAE Text Mail Example");
    msg.setText(本文內容\n換行);
    Transport.send(msg);
catch (AddressException e) {
    // ...
} catch (MessagingException e) {
    // ...
}
寄送HTML格式信件
try { 

    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress("admin@taicomjp.info", "admin"));
    msg.addRecipient(Message.RecipientType.TO,
                     new InternetAddress("admin@taicomjp.info", "Mr. Admin"));
    msg.setSubject("GAE HTML Mail Example");
    msg.setContent(本文內容<br>換行,"text/html; charset=UTF-8");
    Transport.send(msg);
} catch (AddressException e) {
    // ...
} catch (MessagingException e) {
    // ...
}

方法二-使用低階郵件API

MailService ms = MailServiceFactory.getMailService();
MailService.Message message = new MailService.Message();
try {
    message.setSender("admin@taicomjp.info");
    message.setTo("admin@taicomjp.info");
    message.setSubject("GAE Low Level Mail Example");
    message.setHtmlBody("本文內容<br>換行");
    message.setTextBody("本文內容\n換行");
    ms.send(message); } catch (Exception e) {
     e.printStackTrace(); }

0 意見:

張貼留言