Java邮件发送程序(可以同时发给多个地址、可以带附件)
程序员文章站
2023-11-21 23:46:22
发送邮件的主程序 import java.util.properties; import common.util.email_autherticator...
发送邮件的主程序
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);
}
}
上一篇: java字符串的合并