导航:首页 > 编程语言 > java发送邮件模板

java发送邮件模板

发布时间:2025-01-05 17:51:55

A. java实现发送邮件功能

要实现邮件发送功能需要导入包:mail.jar

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.demo.action;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 org.demo.form.DemoForm;

public class DemoAction extends Action {

private static final String CONTENT_TYPE = "test/html;charset=GB2312";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DemoForm demoForm = (DemoForm) form;
System.out.println("标题是" + demoForm.getBiaoti());
System.out.println("内容是" + demoForm.getNeirong());
try {
response.setContentType(CONTENT_TYPE);
String smtphost = "smtp.nj.headware.cn"; // 发送邮件服务器
String user = "q0000015369"; // 邮件服务器登录用户名
String password = "Queshuwen26"; // 邮件服务器登录密码
String from = "[email protected]"; //
String to = "[email protected]"; // 收件人邮件地址
String subject = demoForm.getBiaoti(); // 邮件标题
String body = demoForm.getNeirong(); // 邮件内容
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth", "true");
Session ssn = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(ssn);

InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = ssn.getTransport("smtp");

transport.connect(smtphost, user, password);

transport.sendMessage(message, message
.getRecipients(Message.RecipientType.TO));
// transport.send(message);
transport.close();
return mapping.findForward("succ");
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("fail");
}

}
}

阅读全文

与java发送邮件模板相关的资料

热点内容
124十进制转二进制算法 浏览:417
发邮件文件夹添加不了 浏览:267
外国服务器地址和端口 浏览:385
android水球 浏览:969
魅族校准电池命令 浏览:107
gitlab需要什么配置服务器 浏览:238
怎样设置文件夹不被封 浏览:455
服务器管理口是干什么的 浏览:655
java对齐输出右对齐 浏览:912
android获取应用程序大小 浏览:11
javac通信 浏览:910
单独开启资源管理器的命令是 浏览:562
python安卓模拟器 浏览:47
pdf阅读器android 浏览:169
酒店信息管理系统源码 浏览:307
php实现blob加密视频 浏览:383
比泽尔二手压缩机 浏览:612
层级遍历算法复杂度 浏览:296
单片机课设致谢 浏览:906
农行app我的账户在哪里 浏览:47