邮件提醒

MimeMessage,它可用于电子邮件和Usenet新闻消息。除此之外,其他厂商可以自由扩展Message来满足自身需求。
from字段   --用于指明发件人
to字段    --用于指明收件人
subject字段 --用于说明邮件主题
cc字段    -- 抄送,将邮件发送给收件人的同时抄送给另一个收件人,收件人可以看到邮件抄送给了谁
bcc字段    -- 密送,将邮件发送给收件人的同时将邮件秘密发送给另一个收件人,收件人无法看到邮件密送给了谁
4.2 JavaMail中主要类
java mail中主要类:javax.mail.Session、javax.mail.internet.MimeMessage、javax.mail.Transport。

Session:表示会话,即客户端与邮件服务器之间的会话!想获得会话需要给出账户和密码,当然还要给出服务器名称。在邮件服务中的Session对象,就相当于连接数据库时的Connection对象。

MimeMessage:表示邮件类,它是Message的子类。它包含邮件的主题(标题)、内容,收件人地址、发件人地址,还可以设置抄送和暗送,甚至还可以设置附件。

Transport:用来发送邮件。它是发送器!

/**
* 邮箱注册
* @throws Exception
*/
@RequestMapping(value = "/emailRegister", method = RequestMethod.POST)
@ResponseBody
public String emailRegister(HttpServletRequest request, String userEmail,
String userPassword) throws Exception {
int i = 0;
System.out.println(userEmail + "------------" + userPassword);
BookUser bookUser = new BookUser();
bookUser.setUserEmail(userEmail);
bookUser.setUserPassword(userPassword);
/*
*
* 注册并写入数据库部分
*
*/
i = bookUserService.register(bookUser);
/*
*
* 注册成功,发送欢迎邮件
*/

if (i != 0) {
if (CheckoutSendEmailUtil.sendmail(userEmail) == true) {
// msg="邮件发送成功";
System.out.println("发送成功");
return "success";
}
// msg="邮件发送失败";
// response.setContentType("text/html;charset=utf-8");
// response.getWriter().write("<script>alert('邮件发送失败');</script>");
return "success";
} else {
System.out.println("该用户已被注册");
return "error";
}

//throw new GlobalException("你的内容已丢失");

}

--需要导入两个邮箱有关的Java-jar包:初始化类与方法

import javax.mail.SendFailedException;

private static String SMTPHost;
private static String Port;
private static String MailUsername;
private static String MailPassword;
private static String MailFrom;

static{
SMTPHost="smtp.163.com";
Port="25";
MailUsername="15074803249@163.com";
MailPassword="123456yhm";
MailFrom="15074803249@163.com";
}
public CheckoutSendEmailUtil(){}
public static boolean sendmail(String mailto) throws Exception {// 账户跟密码
String MailTo = mailto;// 收件人
String MailSubject = "Email registration completed";// 邮件主题
String MailBCopyTo = "";
String MailCopyTo = "";
String MailBody = "<p><img alt=\"westlakelogo\" src=\"http://www.expertsonchina.com/images/top.gif\" border=\"0\" /></p>"
+ "<p> 亲爱的------ "
+ ",<br />"
+ "<br />"
+ " 欢迎您,您已经通过该邮箱注册成功。</p>"
+ " <p>We'd like to thank you for your interest in our expert network business. We appreciate you taking time to apply for membership in our expert community.</p>"
+ " <p>To ensure the integrity and quality of our network, we seek to verify the credentials of our experts. We hope that you understand it. We will send you a confirmation email shortly.</p>"
+ " <p>Best regards,<br />"
+ " <br /> chauvinist </p> <p>&nbsp;</p>";
System.out.println("---------------"+SMTPHost);
if (SMTPHost == null || SMTPHost == "" || MailUsername == null
|| MailUsername == "" || MailPassword == null
|| MailPassword == "" || MailFrom == null || MailFrom == "") {
System.out.println("Servlet parameter Wrongs");
}
SendEmailUtil send = new SendEmailUtil(SMTPHost, Port, MailUsername,
MailPassword);
if (send.sendingMimeMail(MailFrom, MailTo, MailCopyTo, MailBCopyTo,
MailSubject, MailBody)) {
return true;
} else {
return false;
}
}

 

--邮箱工具类

private Properties props;// 系统属性
private Session mailSession;// 邮件会话对象
private MimeMessage mimeMsg; // MIME邮件对象
private String username = "";//账户
private String password = "";//密码
public SendEmailUtil(){
}

public SendEmailUtil(String username, String password) {
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}

public SendEmailUtil(String SMTPHost, String Port, String MailUsername, String MailPassword) throws SendFailedException {
SendEmailUtil au = new SendEmailUtil(MailUsername, MailPassword);
// 设置系统属性
props = java.lang.System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", SMTPHost); // 设置SMTP主机
props.put("mail.smtp.port", Port); // 设置服务端口号
props.put("mail.smtp.auth", "true");// 同时通过验证
// 获得邮件会话对象
mailSession = Session.getInstance(props, au);
}

public boolean sendingMimeMail(String MailFrom, String MailTo,
String MailCopyTo, String MailBCopyTo, String MailSubject,
String MailBody) throws Exception {

// 创建MIME邮件对象
mimeMsg = new MimeMessage(mailSession);
// 设置发信人
mimeMsg.setFrom(new InternetAddress(MailFrom));
// 设置收信人
if (MailTo != null) {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(MailTo));
}
// 设置抄送人
if (MailCopyTo != null) {
mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC,
InternetAddress.parse(MailCopyTo));
}
// 设置暗送人
if (MailBCopyTo != null) {
mimeMsg.setRecipients(javax.mail.Message.RecipientType.BCC,
InternetAddress.parse(MailBCopyTo));
}
// 设置邮件主题
mimeMsg.setSubject(MailSubject, "utf-8");
// 设置邮件内容,将邮件body部分转化为HTML格式
mimeMsg.setContent(MailBody, "text/html;charset=utf-8");
//mimeMsg.setDataHandler(new javax.activation.DataHandler(
// new StringDataSource(MailBody, "text/html")));
// 发送邮件Transport:用来发送邮件。它是发送器!
Transport.send(mimeMsg);
return true;
}

 

学生作品记录:有不足之处,请多指教

 

转载于:https://www.cnblogs.com/yhm9/articles/11093247.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值