📄 sendmailaction.java
字号:
package tarena.action;import java.util.Date;import java.util.Properties;import javax.mail.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.mail.internet.*;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import tarena.util.EmailAutherticator;public class SendMailAction extends Action { public String host = "mail.enterinfo.net"; public String username = "TonyDeng"; public String password = "xiaopei1"; public String mail_head_name = "this is head of this mail"; public String mail_head_value = "this is head of this mail"; public String mail_to = "chineseoa@gmail.com"; public String mail_from = "TonyDeng@enterinfo.net"; public String mail_subject = "this is the subject of this test mail"; public String mail_body = "this is the mail_body of this test mail"; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { try { Properties props = new Properties();// 获取系统环境 Authenticator auth = new EmailAutherticator();// 进行邮件服务器用户认证 props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, auth); // 设置session,和邮件服务器进行通讯。 MimeMessage message = new MimeMessage(session); message.setContent("Hello", "text/plain");// 设置邮件格式 message.setSubject(mail_subject);// 设置邮件主题 message.setText(mail_body);// 设置邮件正文 message.setHeader(mail_head_name, mail_head_value);// 设置邮件标题 message.setSentDate(new Date());// 设置邮件发送日期 Address address = new InternetAddress(mail_from, "sunxiaoming"); message.setFrom(address); // 设置邮件发送者的地址 // 如果要对邮件发送者进行多个参数的设置,可以用以下语句 // Address address[] = {new // InternetAddress("sunxm@oaklet.co.jp","sunxmatoaklet"),new // InternetAddress("firstsxm@hotmail.com","sunxmathotmail")}; // message.addFrom(address); Address toAddress = new InternetAddress(mail_to);// 设置邮件接收方的地址 message.addRecipient(Message.RecipientType.TO, toAddress); // Address ccAddress = new // InternetAddress("firstsxm@hotmail.com");//设置邮件抄送者的地址 // message.addRecipient(Message.RecipientType.CC,ccAddress); Transport.send(message);// 发送邮件 System.out.println("send ok!"); } catch (Exception ex) { System.out.println("faild" + ex); } return mapping.findForward("login"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -