導航:首頁 > 編程語言 > 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發送郵件模板相關的資料

熱點內容
php按鈕點擊事件 瀏覽:934
河南伺服器機房售後服務雲主機 瀏覽:184
android仿聯系人 瀏覽:368
什麼app軟體可以學音標 瀏覽:669
郭天祥十天學會單片機優酷 瀏覽:330
什麼app兒童免費 瀏覽:582
遺傳演算法的理解 瀏覽:800
php刪除sql 瀏覽:841
紅進藍出指標源碼 瀏覽:700
python數據轉換列表類型 瀏覽:717
解壓後的文件怎麼解開 瀏覽:175
四川補貼認證下載什麼app 瀏覽:858
android設計風格 瀏覽:426
視頻不支持我的加密 瀏覽:342
布包pdf 瀏覽:267
程序員錄制課程表 瀏覽:626
eclipsephp斷點調試 瀏覽:895
虛擬成交量指標源碼 瀏覽:838
什麼APP有背單詞小組 瀏覽:43
蘋果2g視頻怎麼加密 瀏覽:204