//设置主收件人
msg.setRecipients(Message.RecipientType.TO,Address toaddress);
//设置抄送收件人
msg.addRecipients(Message.RecipientType.CC,Address coaddress);
//设置暗抄送人
msg.addRecipients(Message.RecipientType.BCC,Address bcoaddress);
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
/**
* @author Bromon
*/
public class Msgmultisend {
private String host = "";
private String user = "";
private String password = "";
public void setHost(String host) {
this.host = host;
}
public void setAccount(String user, String password) {
this.user = user;
this.password = password;
}
public void send(String from, String to, String subject, String content) {
Properties props = new Properties();
props.put("mail.smtp.host", host); //指定SMTP服务器
props.put("mail.smtp.auth", "true"); //指定是否需要SMTP验证
try {
Session mailSession = Session.getInstance(props, null);
mailSession.setDebug(true); //是否在控制台显示debug信息
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from)); //发件人
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); //这个里面的和单独的发送给某人是不同的
//message.setRecipient(Message.RecipientType.TO, new InternetAddress(address));
message.setSubject(subject); //邮件主题
message.setText(content); //邮件内容
message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect(host, user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
Msgmultisend sm = new Msgmultisend();
sm.setHost("smtp.163.com"); //指定要使用的邮件服务器
sm.setAccount("txy_821", "**********"); //指定帐号和密码
/*
* @param String 发件人的地址
* @param String 收件人地址
* @param String 邮件标题
* @param String 邮件正文
*/
sm.send("txy_821@163.com", "396668361@qq.com,txy_821@163.com,34493296@qq.com",
"mose测试ing", "无语!");
}
}