关闭

Java邮件发送程序(可以同时发给多个地址、可以带附件)

标签: java邮件
164人阅读 评论(0) 收藏 举报
分类:
package day2;


import java.util.Date;
import java.util.Properties;


import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
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.apache.commons.lang3.StringUtils;


public class SendMailTest {
public static void main(String[] args) {
String title = "titleTest";// 所发送邮件的标题
String from = "servername@163.com";// 从那里发送
String sendTo[] = { "test@qq.com", "test@sina.com" };// 发送到那里
// 邮件的文本内容,可以包含html标记则显示为html页面
String content = "mail test!!!!!!<br><a href=#>aaa</a>";
// 所包含的附件,及附件的重新命名
String fileNames[] = { "E:\\1.txt,1.txt", 
"E:\\2.txt,2.txt" };
try {
// MailSender mailsender = new MailSender();
sendmail(title, 
from, 
sendTo, 
content, 
fileNames, 
"text/html;charset=gb2312"
);
} catch (Exception ex) {
ex.printStackTrace();
}
}


/**

* @param subject
* @param from 发件人的地址
* @param to 收件人的地址
* @param text 
* @param filenames  
* @param mimeType 
*  
* */
public static void sendmail(String subject, String from, 
String[] to, String text, String[] filenames,
String mimeType) throws Exception {
// ResourceBundle mailProps = ResourceBundle.getBundle("mail");
// 可以从配置文件读取相应的参数
Properties props = new Properties();


String smtp = "smtp.163.com"; // 设置发送邮件所用到的smtp
String serverName = "serverName";
String serverPassword = "serverPassword";


Session mailSession; // 邮件会话对象
MimeMessage mimeMsg; // MIME邮件对象


props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", smtp); // 设置SMTP主机
props.put("mail.smtp.auth", "true"); // 是否到服务器用户名和密码验证
// 到服务器验证发送的用户名和密码是否正确
EmailAutherticator myEmailAuther = new 
EmailAutherticator(serverName, serverPassword);
// 根据属性新建一个邮件会话
mailSession = Session.getInstance(props, 
(Authenticator) myEmailAuther
);
// 设置传输协议
Transport transport = mailSession.getTransport("smtp");
// 设置from、to等信息
mimeMsg = new MimeMessage(mailSession);
if (!StringUtils.isEmpty(from)) {
InternetAddress sentFrom = new InternetAddress(from);
mimeMsg.setFrom(sentFrom); // 设置发送人地址
}


InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
System.out.println("发送到:" + to[i]);
sendTo[i] = new InternetAddress(to[i]);
}


// 设置收件人,并设置其接收类型为TO
mimeMsg.setRecipients(MimeMessage.RecipientType.TO, sendTo);
// 设置标题
mimeMsg.setSubject(subject, "gb2312");


MimeBodyPart messageBodyPart1 = new MimeBodyPart();
// messageBodyPart.setText(UnicodeToChinese(text));
// 设置信件内容
messageBodyPart1.setContent(text, mimeType);


Multipart multipart = new MimeMultipart();// 附件传输格式
multipart.addBodyPart(messageBodyPart1);


for (int i = 0; i < filenames.length; i++) {
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
// 选择出每一个附件名
String filename = filenames[i].split(",")[0];
System.out.println("附件名:" + filename);
String displayname = filenames[i].split(",")[1];
// 得到数据源
FileDataSource fds = new FileDataSource(filename);
// 得到附件本身并至入BodyPart
messageBodyPart2.setDataHandler(new DataHandler(fds));
// 得到文件名同样至入BodyPart
// messageBodyPart2.setFileName(displayname);
// messageBodyPart2.setFileName(fds.getName());
messageBodyPart2.setFileName(MimeUtility.encodeText(displayname));
multipart.addBodyPart(messageBodyPart2);
}
mimeMsg.setContent(multipart);
// 设置信件头的发送日期
mimeMsg.setSentDate(new Date());
// 存储邮件信息
mimeMsg.saveChanges();
// 发送邮件
//transport.send(mimeMsg);
transport.connect(serverName, serverPassword);
transport.sendMessage(mimeMsg, 
mimeMsg.getAllRecipients()
);// 发送邮件,其中第二个参数是所有已设好的收件人地址
transport.close();
}

}




package day2;


import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


public class EmailAutherticator extends Authenticator {

private String userName = null;
private String password = null;


public void setUserName(String userName) {
this.userName = userName;
}


public void setPassword(String password) {
this.password = password;
}


public EmailAutherticator(String userName, String password) {
super();
setUserName(userName);
setPassword(password);
}


public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5336次
    • 积分:214
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条