当前位置: 编程技术>java/j2ee
Java邮件发送程序(可以同时发给多个地址、可以带附件)
来源: 互联网 发布时间:2014-10-21
本文导语: 发送邮件的主程序 import java.util.Properties; import common.util.Email_Autherticatorbean; import javax.mail.Authenticator; import javax.mail.internet.InternetAddress; import org.apache.commons.lang.StringUtils; import javax.mail.internet.MimeBodyPart; import javax.mail.Multipart; i...
发送邮件的主程序
import java.util.Properties;
import common.util.Email_Autherticatorbean;
import javax.mail.Authenticator;
import javax.mail.internet.InternetAddress;
import org.apache.commons.lang.StringUtils;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.activation.FileDataSource;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataHandler;
import javax.mail.internet.MimeUtility;
import java.util.Date;
/**
* 利用java.mail的邮件发送程序
*/
public class SendMailTest
{
public static void main(String[] args)
{
String title = "titleTest";//所发送邮件的标题
String from ="sir_znp@163.com";//从那里发送
String sendTo[] = {"znp@163.net","sir_znp@163.com"};//发送到那里
//邮件的文本内容,可以包含html标记则显示为html页面
String content = "mail test!!!!!!
aaa";
//所包含的附件,及附件的重新命名
String fileNames[] = {"F:\music\text1.txt,text1.txt","F:\music\text2.txt,text2.txt"};
try {
// MailSender mailsender = new MailSender();
sendmail(title, from, sendTo, content, fileNames,"text/html;charset=gb2312");
} catch (Exception ex) { ex.printStackTrace(); }
}
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 = "sir_znp";
String serverpaswd = "123";
javax.mail.Session mailSession; //邮件会话对象
javax.mail.internet.MimeMessage mimeMsg; //MIME邮件对象
props = java.lang.System.getProperties(); //获得系统属性对象
props.put("mail.smtp.host", smtp); //设置SMTP主机
props.put("mail.smtp.auth", "true"); //是否到服务器用户名和密码验证
//到服务器验证发送的用户名和密码是否正确
Email_Autherticatorbean myEmailAuther = new Email_Autherticatorbean(servername, serverpaswd);
//设置邮件会话
mailSession = javax.mail.Session.getInstance(props, (Authenticator) myEmailAuther);
//设置传输协议
javax.mail.Transport transport = mailSession.getTransport("smtp");
//设置from、to等信息
mimeMsg = new javax.mail.internet.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
import java.util.Properties;
import common.util.Email_Autherticatorbean;
import javax.mail.Authenticator;
import javax.mail.internet.InternetAddress;
import org.apache.commons.lang.StringUtils;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.activation.FileDataSource;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataHandler;
import javax.mail.internet.MimeUtility;
import java.util.Date;
/**
* 利用java.mail的邮件发送程序
*/
public class SendMailTest
{
public static void main(String[] args)
{
String title = "titleTest";//所发送邮件的标题
String from ="sir_znp@163.com";//从那里发送
String sendTo[] = {"znp@163.net","sir_znp@163.com"};//发送到那里
//邮件的文本内容,可以包含html标记则显示为html页面
String content = "mail test!!!!!!
aaa";
//所包含的附件,及附件的重新命名
String fileNames[] = {"F:\music\text1.txt,text1.txt","F:\music\text2.txt,text2.txt"};
try {
// MailSender mailsender = new MailSender();
sendmail(title, from, sendTo, content, fileNames,"text/html;charset=gb2312");
} catch (Exception ex) { ex.printStackTrace(); }
}
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 = "sir_znp";
String serverpaswd = "123";
javax.mail.Session mailSession; //邮件会话对象
javax.mail.internet.MimeMessage mimeMsg; //MIME邮件对象
props = java.lang.System.getProperties(); //获得系统属性对象
props.put("mail.smtp.host", smtp); //设置SMTP主机
props.put("mail.smtp.auth", "true"); //是否到服务器用户名和密码验证
//到服务器验证发送的用户名和密码是否正确
Email_Autherticatorbean myEmailAuther = new Email_Autherticatorbean(servername, serverpaswd);
//设置邮件会话
mailSession = javax.mail.Session.getInstance(props, (Authenticator) myEmailAuther);
//设置传输协议
javax.mail.Transport transport = mailSession.getTransport("smtp");
//设置from、to等信息
mimeMsg = new javax.mail.internet.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