Java邮件发送程序(可以同时发给多个地址、可以带附件)
更新时间:2008年07月30日 23:26:03 作者:
不错的功能比较齐全的邮件发送程序源码
发送邮件的主程序
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!!!!!!<br><a href=#>aaa</a>";
//所包含的附件,及附件的重新命名
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 < to.length; i++)
{
System.out.println("发送到:" + to[i]);
sendTo[i] = new InternetAddress(to[i]);
}
mimeMsg.setRecipients(javax.mail.internet.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.close();
}
}
验证类
package common.util;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticatorbean extends Authenticator
{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public Email_Autherticatorbean(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
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!!!!!!<br><a href=#>aaa</a>";
//所包含的附件,及附件的重新命名
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 < to.length; i++)
{
System.out.println("发送到:" + to[i]);
sendTo[i] = new InternetAddress(to[i]);
}
mimeMsg.setRecipients(javax.mail.internet.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.close();
}
}
验证类
package common.util;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticatorbean extends Authenticator
{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public Email_Autherticatorbean(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
相关文章
详解Spring Boot实战之Filter实现使用JWT进行接口认证
本篇文章主要介绍了详解Spring Boot实战之Filter实现使用JWT进行接口认证,具有一定的参考价值,有兴趣的可以了解一下2017-07-07Mybatis中collection和association的使用区别详解
这篇文章主要介绍了Mybatis中collection和association的使用区别详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-11-11ASM源码学习之ClassReader、ClassVisitor与ClassWriter详解
这篇文章主要给大家介绍了ASM源码之ClassReader、ClassVisitor与ClassWriter的相关资料,文中介绍的非常相信,相信对大家的学习或者工作具有一定的参考借鉴价值,有需要的朋友可以参考借鉴,下面来一起看看吧。2017-01-01mybatis-plus使用@EnumValue处理枚举类型的示例代码
这篇文章主要介绍了mybatis-plus使用@EnumValue处理枚举类型的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09多线程Thread,Runnable,Callable实现方式
这篇文章主要为大家详细介绍了Java多线程如何实现Thread,Runnable,Callable的方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-08-08
最新评论