国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

在jsp中發送email

2020-06-01 23:26:59
字體:
來源:轉載
供稿:網友
在jsp中發送email

一、我們可以通過任何支持sun規范中的sun.net.smtp包的JSP引擎(如JSWDK)發送mail。
(警告:使用內置的internal Sun規范包,這將影響到你的jsp程序的可移植性。)

以下scriptlet利用SmtpClient類在jsp文件中發送email。





二、 JavaMail是官方的 Java mail API,可參考 http://java.sun.com/products/javamail/。雖然該API比 sun.net.smtp.SmtpClient更豐富或者說更復雜,但它是可移植的。這里重新創建了一個 MailSender類,它包含了 JavaMail API。如下所示:


// ms_ prefix is for MailSender class variables
// str prefix is for String
// astr prefix is for array of Strings
// strbuf prefix is for StringBuffers, etc.
public MailSender(
String strFrom, // sender
String[] astrTo, // recipient(s)
String[] astrBCC, // bcc recipient(s), optional
String strSubject, // subject
boolean debugging)
{
ms_strFrom = strFrom; // who the message is from
ms_astrTo = astrTo; // who (plural) the message is to
ms_debugging = debugging; // who (plural) the message is to

// set the host
Properties props = new Properties();
props.put(/"mail.smtp.host/", ms_strSMTPHost);

// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(ms_debugging);

try {
// create a message
ms_msg = new MimeMessage(session);

// set the from
InternetAddress from = new InternetAddress(strFrom);
ms_msg.setFrom(from);

// set the to
InternetAddress[] address = new InternetAddress[astrTo.length];
for (int i = 0; i astrTo.length; ++i)
{
address[i] = new InternetAddress(astrTo[i]);
}
ms_msg.setRecipients(Message.RecipientType.TO, address);

// set the bcc recipients
if (astrBCC != null)
{
address = new InternetAddress[astrBCC.length];
for (int i = 0; i astrBCC.length; ++i)
{
eh.dbg(/"astrBCC[/" + i + /"] is: /'/" + astrBCC[i] + /"/'/");
address[i] = new InternetAddress(astrBCC[i]);
}
ms_msg.setRecipients(Message.RecipientType.BCC, address);
}

// set the subject
ms_msg.setSubject(strSubject);

// set up the string buffer which will hold the message
ms_strbufMsg = new StringBuffer();

} catch (MessagingException mex) {
mex.printStackTrace(System.err);
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}

public void ms_add(String strText)
{
ms_strbufMsg.append(strText);
}

public void ms_send()
{
try {
// set the content as plain text
ms_msg.setContent(new String(ms_strbufMsg), /"text/plain/");

// and away
Transport.send(ms_msg);
} catch (Exception ex) {
System.out.println(/"Caught exception in MailSender.ms_send: /" + ex);
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大冶市| 奎屯市| 郎溪县| 瑞昌市| 崇明县| 江山市| 兴海县| 淮北市| 宜宾县| 定边县| 嘉峪关市| 重庆市| 温泉县| 巴中市| 康马县| 景德镇市| 鹤岗市| 九龙县| 高密市| 乐清市| 衡山县| 吴堡县| 紫阳县| 青龙| 灵宝市| 盐亭县| 普安县| 封开县| 堆龙德庆县| 柳州市| 普洱| 大悟县| 鄂尔多斯市| 高密市| 澳门| 洪雅县| 道孚县| 东乡族自治县| 高州市| 民勤县| 肥乡县|