java 发送邮件多个人,java中使用自己的邮箱进行多人邮件发送

host=smtp.qq.com //这是qq邮箱,如果是其他邮箱 服务要配置成相应的host

user=自己邮箱名

pwd=邮箱密码

subject=主题

1:[email protected] //发送人

2:[email protected]

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.util.Date;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

import java.util.Vector;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.Authenticator;

import javax.mail.Message;

import javax.mail.MessagingException;

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.mail.internet.MimeUtility;

import org.apache.commons.lang.StringUtils;

import com.pzoom.dsa.common.util.Log;

import com.pzoom.dsa.nerd.model.Area;

public class SendMail {

private static Log log = Log.getLogger(SendMail.class);

public static String toChinese(String text) {

try {

text = MimeUtility.encodeText(new String(text.getBytes(), "GB2312"), "GB2312", "B");

} catch (Exception e) {

e.printStackTrace();

}

return text;

}

static StringBuffer buffer = new StringBuffer();

static Map map = new HashMap();

static{

try {

InputStream input=Area.class.getClassLoader().getResourceAsStream("email.properties");

BufferedReader br=new BufferedReader(new InputStreamReader(input));

String line=br.readLine();

while(line!=null){

if(line.indexOf(":")>=0){

String[] val = line.split(":");

buffer.append(val[1]+",");

}else{

String[] val=line.split("=");

map.put(val[0], val[1]);

}

line=br.readLine();

}

} catch (IOException e) {

log.error(e);

}

}

/**

* 邮件发送

* @param mb

* @return

*/

public static boolean sendMail(MailBean mb) {

String from = map.get("user");//邮件发送人

String subject = map.get("subject");//邮件主题

String content = mb.getContent();

String fileName = mb.getFilename();

Vector file = mb.getFile();

String type = mb.getType();

String nick = mb.getNick();

Properties props = System.getProperties();

props.put("mail.smtp.host", map.get("host")); // 设置SMTP的主机

props.put("mail.smtp.auth", "true"); // 需要经过验证

if(map.get("host").contains("gmail")){

props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.setProperty("mail.smtp.socketFactory.fallback", "false");

props.setProperty("mail.smtp.port", "465");

props.setProperty("mail.smtp.socketFactory.port", "465");

}

Session session = Session.getInstance(props, new Authenticator() {

public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(map.get("user"), map.get("pwd"));

}

});

try {

MimeMessage msg = new MimeMessage(session);

if(StringUtils.isNotBlank(nick)){

nick = MimeUtility.encodeText(nick);

from = nick+"";

}

msg.setFrom(new InternetAddress(from));

new InternetAddress();

InternetAddress[] address = InternetAddress.parse(buffer.toString());

msg.setRecipients(Message.RecipientType.TO, address);

// msg.setSubject(toChinese(subject));

msg.setSubject(subject);

Multipart mp = new MimeMultipart();

MimeBodyPart mbpContent = new MimeBodyPart();

if("html".equals(type)){

mbpContent.setContent(content, "text/html;charset=gb2312");

}else {

mbpContent.setText(content);

}

mp.addBodyPart(mbpContent);

/* 往邮件中添加附件 */

if(null!=file){

Enumeration efile = file.elements();

while (efile.hasMoreElements()) {

MimeBodyPart mbpFile = new MimeBodyPart();

fileName = efile.nextElement().toString();

FileDataSource fds = new FileDataSource(fileName);

mbpFile.setDataHandler(new DataHandler(fds));

mbpFile.setFileName(toChinese(fds.getName()));

mp.addBodyPart(mbpFile);

}

}

msg.setContent(mp);

msg.setSentDate(new Date());

Transport.send(msg);

} catch (MessagingException me) {

me.printStackTrace();

return false;

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

return false;

}

return true;

}

public static class MailBean{

private String to; // 收件人

private String from; // 发件人

private String host; // SMTP主机

private String username; // 发件人的用户名

private String password; // 发件人的密码

private String subject; // 邮件主题

private String content; // 邮件正文

Vector file; // 多个附件

private String filename; // 附件的文件名

private String type;//type text html类别

private String nick;//昵称

public String getTo() {

return to;

}

public void setTo(String to) {

this.to = to;

}

public String getFrom() {

return from;

}

public void setFrom(String from) {

this.from = from;

}

public String getHost() {

return host;

}

public void setHost(String host) {

this.host = host;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

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 getFilename() {

return filename;

}

public void setFilename(String filename) {

this.filename = filename;

}

public Vector getFile(){

return file;

}

public void attachFile(String fileName) {

if(file == null)

file = new Vector();

file.addElement(fileName);

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getNick() {

return nick;

}

public void setNick(String nick) {

this.nick = nick;

}

}

}

原文:http://blog.csdn.net/u012516914/article/details/40303277

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值