·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » 修改后可以发送附件、抄送、密送的JAVABEAN,吐血推荐~(JAVAMAIL范例)

修改后可以发送附件、抄送、密送的JAVABEAN,吐血推荐~(JAVAMAIL范例)

类别: JSP教程  评论数:0 总得分:0
package com.westarsoft.function;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMail {
private String SMTPServer = new String();
private String from = new String();
private String subject = new String();
private String content = new String();
private String user = new String();
private String password = new String();
private String fileAttachment = new String();
public String getSMTPServer() {
return SMTPServer;
}
public void setSMTPServer(String SMTPServer) {
this.SMTPServer = SMTPServer;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFileAttachment() {
return fileAttachment;
}
public void setFileAttachment(String fileAttachment) {
this.fileAttachment = fileAttachment;
}

public void sendMailTo(String to,String cc,String bcc) {
try {
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;

sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host", SMTPServer);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
/*
StringTokenizer tokenTO = new StringTokenizer(to, ",");
InternetAddress[] addrArrTO = new InternetAddress[tokenTO.countTokens()];
int i = 0;
while(tokenTO.hasMoreTokens()) {
addrArrTO[i] = new InternetAddress(tokenTO.nextToken().toString());
i++;
}
*/
transport = sendMailSession.getTransport("smtp");
transport.connect(SMTPServer, user, password);
//newMessage.setRecipients(Message.RecipientType.TO, addrArrTO);
newMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
newMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
newMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(content);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
newMessage.setContent(multipart);

transport.send(newMessage);
}
catch(Exception e) {
System.out.println(e);
}
}
}





<%
if(request.getMethod().equals("POST")) {
SendMail mail = new SendMail();
mail.setSMTPServer("200.1.1.157");
mail.setUser("lint");
mail.setPassword("30320");
mail.setFrom(request.getParameter("from"));
mail.setSubject(request.getParameter("subject"));
mail.setContent(request.getParameter("content"));
mail.setFileAttachment(request.getParameter("filename"));
mail.sendMailTo(request.getParameter("to"),request.getParameter("cc"),request.getParameter("bcc"));
}
%>
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1