htmlemail java_Java HtmlEmail.embed方法代码示例

import org.apache.commons.mail.HtmlEmail; //导入方法依赖的package包/类

public boolean sendSupportEmail() {

try {

String senderName = sender_name.getText();

String senderEmail = sender_email.getText();

String sendingTime = date_time.getText();

String systemUser = system_user.getText();

String message = messge_content.getText();

if (message.isEmpty()) {

JOptionPane.showMessageDialog(this, "You haven't entered your message. Please enter the message and try again.");

}

// Create the email message

HtmlEmail email = new HtmlEmail();

email.setHostName("host url");

email.addTo("sender email", "Sender Name");

email.setFrom("from email", "From Name");

email.setAuthentication("username", "password");

email.setSSLOnConnect(true);

email.setStartTLSEnabled(true);

email.addReplyTo("[email protected]", "Support Service - Company");

email.setSmtpPort(465);

email.setSubject("New Support Request from Application");

// embed the image and get the content id

URL url = getClass().getResource("/app/resources/shield_icon16x16.png");

String cid = email.embed(url, "Application Logo");

URL template = getClass().getResource("/app/support/email_template_20161101_isuru.emailtemplate");

byte[] encoded = Files.readAllBytes(Paths.get(template.toURI()));

String htmlMessage = new String(encoded, StandardCharsets.UTF_8);

htmlMessage = htmlMessage.replace("_EP1_", "cid:" + cid);

htmlMessage = htmlMessage.replace("_EP2_", systemUser);

htmlMessage = htmlMessage.replace("_EP3_", senderName + "(" + senderEmail + ")");

htmlMessage = htmlMessage.replace("_EP4_", sendingTime);

htmlMessage = htmlMessage.replace("_EP5_", message.replaceAll("\\r?\\n", "
"));

// set the html message

email.setHtmlMsg(htmlMessage);

String textMessage = "Application - Support Request\n"

+ "---------------------------------------------------------------------\n"

+ "New Support Request from P1\n"

+ "Sent by P2 on P3\n"

+ "---------------------------------------------------------------------\n"

+ "\n"

+ "Message: \nP4\n"

+ "\n"

+ "---------------------------------------------------------------------\n"

+ "This is an automatically generated email sent from Application.\n"

+ "\n"

+ "© All Rights Reserved - Management Development Co-operative Co. Ltd.";

textMessage = textMessage.replace("P1", systemUser);

textMessage = textMessage.replace("P2", senderName + "(" + senderEmail + ")");

textMessage = textMessage.replace("P3", sendingTime);

textMessage = textMessage.replace("P4", message);

// set the alternative message

email.setTextMsg(textMessage);

// send the email

email.send();

return true;

} catch (Exception e) {

JOptionPane.showMessageDialog(this, "Cannot send email.\nError:" + e.getLocalizedMessage(), "Sending failure", JOptionPane.ERROR_MESSAGE);

return false;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值