我想要做一个邮件群发系统,请问该怎么去实现。呵呵这是我面试的时候别人问我的,我不知道怎么回答,还问我用到哪些类。问了我好几次这个问题了,有高手帮我解决下,谢谢。我简历用的是别人的,里面有这个项目 呵呵。邮件推广系统:
定时向注册用户发送邮件的群发系统
责任描述:
开发了新、高效率邮件推广系统,能够顺利到达多数的邮箱系统、如Hotmail、Yahoo,等,解决了旧系统的多种问题。
定时向注册用户发送邮件的群发系统
责任描述:
开发了新、高效率邮件推广系统,能够顺利到达多数的邮箱系统、如Hotmail、Yahoo,等,解决了旧系统的多种问题。
try {
JavaMailSenderImpl mail = new JavaMailSenderImpl();
String smtpHostName = commonVO.getHostName();//IP地址,你自己改
int port = commonVO.getPort();//端口,你自己改
mail.setHost(smtpHostName);
mail.setPort(port);
MimeMessage mailMessage = mail.createMimeMessage();
MimeMessageHelper messageHelper = null;
String sendFrom = "[email protected]";//发送人的邮箱地址
String title = commonVO.getTitle();//标题
String content = commonVO.getContent();//内容
//下面就是群发的部分,mailTOList,CcToList 其实就是保存地址字符串数组,你换成自己的地址数组
if(commonVO.getMailToList() != null && commonVO.getMailToList().length > 0){
String[] toList = commonVO.getMailToList();
messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");
messageHelper.setFrom(sendFrom);
messageHelper.setTo(toList);
if(commonVO.getCcToList() != null) {
String[] ccToList = commonVO.getCcToList();
messageHelper.setCc(ccToList);
}
messageHelper.setSubject(title);
messageHelper.setText(content, false);
File[] files = commonVO.getAttachments();
if(files != null){
for(int i=0;i<files.length;i++){
messageHelper.addAttachment(files[i].getName(), files[i]);
}
}
mail.send(mailMessage);
}
} catch (MessagingException e) {
log.error(e.getMessage());
} catch (Exception e) {
log.error(e.getMessage());
}需要 spring.jar,mail.jar支持
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.text.html.MinimalHTMLWriter;
/**
* <P>
* Title:用java发送邮件的例子
* </P>
*
* <P>
* Description:发送图片附件并在html中使用该图片
* </P>
*
* <P>
* Copyright: Copyright (c) 2007
* </P>
*
* @author
* @main
* @date
*/
public class SendMail {
private static String username = "*************";
private static String password = "**********";
private static String smtpServer = "smtp.qq.com";
private static String fromMailAddress = "********@qq.com";
private static String toMailAddress = "********@hotmail.com";
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpServer);
// 获得邮件会话对象
Session session = Session.getDefaultInstance(props,
new SmtpAuthenticator(username, password));
/** *************************************************** */
// 创建MIME邮件对象
MimeMessage mimeMessage = new MimeMessage(session); System.out.println("发件人:"+new InternetAddress(fromMailAddress));
mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toMailAddress));// 收件人
mimeMessage.setSubject("主题");
mimeMessage.setSentDate(new Date());// 发送日期
Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
/** *************************************************** */
BodyPart bodyPart = new MimeBodyPart();// 正文
String str = "<table width=40% border='1'><tr><td>我的邮箱</td><td><input type='text' id='myEmail' name='myEmail'></input></td></tr> <tr><td>好友的邮箱</td><td><input type='text' id='freEmail' name='freEmail'></td></tr><tr><td colspan=2 align=center><input type='button' value='推荐给好友'></td></tr></table>";
//String str = ReadCode.getWebCon("http://192.168.1.101:8080/sendEmail/web/index.htm");
bodyPart.setDataHandler(new DataHandler(str,
"text/html;charset=utf-8"));// 网页格式
/** *************************************************** */
/*BodyPart attachBodyPart = new MimeBodyPart();// 普通附件
FileDataSource fds = new FileDataSource("c:/boot.ini");
attachBodyPart.setDataHandler(new DataHandler(fds));
attachBodyPart.setFileName("=?GBK?B?"+ new sun.misc.BASE64Encoder().encode(fds.getName().getBytes())+ "?=");// 解决附件名中文乱码
mp.addBodyPart(attachBodyPart); */
/** *************************************************** */
/* MimeBodyPart imgBodyPart = new MimeBodyPart(); // 附件图标
byte[] bytes = readFile("C:/button.gif");
ByteArrayDataSource fileds = new ByteArrayDataSource(bytes,
"application/octet-stream");
imgBodyPart.setDataHandler(new DataHandler(fileds));
imgBodyPart.setFileName("button.gif");
imgBodyPart.setHeader("Content-ID", "<IMG1></IMG1>");// 在html中使用该图片方法src="cid:IMG1"
mp.addBodyPart(imgBodyPart); */
/** *************************************************** */
mp.addBodyPart(bodyPart);
mimeMessage.setContent(mp);// 设置邮件内容对象
Transport.send(mimeMessage);// 发送邮件
}
/**
* 读取文件
*
* @param file
* 文件路径
* @return 返回二进制数组
*/
public static byte[] readFile(String file) {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
int bytesRead;
byte buffer[] = new byte[1024 * 1024];
while ((bytesRead = fis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
Arrays.fill(buffer, (byte) 0);
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bos.toByteArray();
}
}
/**
* Smtp认证
*/
class SmtpAuthenticator extends Authenticator {
String username = null;
String password = null;
// SMTP身份验证
public SmtpAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}
}
class ByteArrayDataSource implements DataSource {
private final String contentType;
private final byte[] buf;
private final int len;
public ByteArrayDataSource(byte[] buf, String contentType) {
this(buf, buf.length, contentType);
}
public ByteArrayDataSource(byte[] buf, int length, String contentType) {
this.buf = buf;
this.len = length;
this.contentType = contentType;
}
public String getContentType() {
if (contentType == null)
return "application/octet-stream";
return contentType;
}
public InputStream getInputStream() {
return new ByteArrayInputStream(buf, 0, len);
}
public String getName() {
return null;
}
public OutputStream getOutputStream() {
throw new UnsupportedOperationException();
}
}
smtp身份认证
getMail()
sendMail()
可添加附件的邮件群发系统,没有定时功能
下面是邮件发送相关的类文件,需要的话,再贴一些代码上来,4楼的代码已经很清晰了
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.springframework.web.multipart.MultipartFile;
《利用Spring框架封装的JavaMail实现同步或异步邮件发送》