JAVA实现发送邮件功能

package util;
import java.io.InputStream;
//抽象类表示字节输入流的所有类的超类。需要定义 InputStream 的子类的应用程序必须始终提供返回下一个输入字节的方法。
import java.util.Properties;
//持久属性集。可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
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;

public class EdmMail {
private static String MAIL_TITLE="测试……";
private String mailServerHost="smtp.qq.com";
private String fromAddress1="309952828@qq.com";//发送人
private String toAddress="313959688@qq.com";//收件人
private MimeMessage mimemsg;//邮件对象
private Session session;//会话对象
private Properties properties;//系统属性
private Multipart part;//Multipart对象:邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
private String username="XXXXXXXXXXXX@qq.com";//smtp用户名
private String password="XXXXXXXXXXXX";//smtp密码
private String profile="serach.properties";
private String fromAddress="309952828@qq.com";


private EdmMail(){
//getValues();
setSmtpHost(mailServerHost);
getMailSession();


}
public EdmMail(String sub){
this();
MAIL_TITLE=sub;

}
/*private boolean getValues(){
Properties proties=null;
InputStream input=null;
try{
input=EdmMail.class.getClassLoader().getResourceAsStream(profile);
proties=new Properties();
proties.load(input);
mailServerHost=proties.getProperty(mailServerHost);
fromAddress=proties.getProperty(fromAddress1);
username=proties.getProperty(username);
password=proties.getProperty(password);
toAddress=proties.getProperty(toAddress);
return true;

}catch(Exception e){
System.out.println("配置文件出错!"+e);
return false;
}

}*/
//设置SMTP主机
private void setSmtpHost(String mailhost){
if(properties==null){
properties=System.getProperties();//获取系统属性
}
properties.setProperty("mail.smtp.host",mailhost);

}
//获取邮件会话,创建MimeMessage对象
private boolean getMailSession(){
try{
session=session.getDefaultInstance(properties,null);

}catch(Exception e){
System.out.println("获取邮件会话对象出错,原因:"+e);
return false;
}try{
mimemsg=new MimeMessage(session);
part=new MimeMultipart();
return false;
}catch(Exception e){
System.out.println("创建对象出错,原因:"+e);
return false;
}


}
//smtp身份验证
private void setSmtpAuth(boolean bool)
{
if(properties==null){
properties=System.getProperties();

}
if(bool)
{
properties.put("mail.smtp.auth","true");

}else{
properties.put("mail.smtp.auth","false");
}

}
//邮件主题
private boolean setMailSub(String mailsubject){
try{
mimemsg.setSubject(mailsubject,"GBK");
return true;
}catch(Exception e){
System.out.println("标题出错,原因"+e);
return false;
}


}
//邮件格式
private boolean setMailBody(String mainBody)
{
BodyPart bdyPart=new MimeBodyPart();
try{
bdyPart.setContent(mainBody,"text/html;charset=GBK");
part.addBodyPart(bdyPart);
return true;

}catch(Exception e){
System.out.println("设置邮件格式出错,原因"+e);
return false;
}
}
//邮件附件
private boolean addAttach(String filePath)
{
BodyPart bdy=new MimeBodyPart();
try{
FileDataSource dataSource=new FileDataSource(filePath);
bdy.setDataHandler(new DataHandler(dataSource));
bdy.setFileName(dataSource.getName());
part.addBodyPart(bdy);
return true;
}catch(Exception e){
System.out.println("添加附件:"+filePath+"出错,原因:"+e);
return false;
}
}
//发送人
private boolean setMailFrom(String from)
{
try{
mimemsg.setFrom(new InternetAddress(from));
return true;
}catch(Exception e){
System.out.println("设置发送人:"+from+"出错原因:"+e);
return false;
}

}
//收件人
private boolean setMailTo(String mailto){
if(mailto==null){
return false;
}
try{
mimemsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(mailto));
return true;
}catch(Exception e){
System.out.println("设置收件人:"+mailto+"出错原因"+e);
return false;
}
}
//发送邮件
private boolean sendout(){
try{
mimemsg.setContent(part);
mimemsg.saveChanges();
System.out.println("开始发送邮件……");
Session mailSession=Session.getInstance(properties,null);
Transport tsport=mailSession.getTransport("smtp");
tsport.connect((String)properties.getProperty("mail.smtp.host"),username,password);
tsport.sendMessage(mimemsg, mimemsg.getRecipients(Message.RecipientType.TO));
tsport.close();
System.out.println("发送成功");
return true;

}catch(Exception e){
System.out.println("发送邮件出错原因:"+e);
return false;
}
}
//拼接邮件内容
public void sendMail(String fileAttach){
StringBuffer content=new StringBuffer();
//头部
content.append("<html>");
content.append("<head><META content='text/html;charset=gbk'http-equiv=Content-Type>");
content.append("<META name=GENERATOR content='MSHTML 8.00.6001.18702'>");
content.append("<style type='text/css'>");
content.append(".STYLE1{color:#000000}");
content.append("TABLE{FONT-SIZE;12px;COLOR:#444444;LINE-HEIGHT:14px;FONT-FAMILY:'宋体','Arial';TEXT-DECORATTOM:none;}");
content.append(".STYLE3{font-size:13px;color:#FD9800;font-weight:bold;}");
content.append("</style>");
content.append("</head>");
//显示邮件内容
content.append("<body>");
content.append("<table width='749px' align='left'>");
content.append("<tr><td width='749' height='10px'></td></tr");
content.append("<tr><td><p align='left'><font size=2>for test!</font></p></td></tr>");
content.append("</table>");
//content.append();
content.append("</body>");
content.append("</html>");
System.out.println("content*******"+content);
setSmtpAuth(false);
if(setMailSub(MAIL_TITLE)==false){
return;
}
if(setMailBody(content.toString())==false){
return;
}
if(setMailTo(toAddress)==false){
return;
}
if(setMailFrom(fromAddress)==false){
return;
}
if(addAttach(fileAttach)==false){
return;
}
if(sendout()==false){
return;
}



}
public static void main(String[] args){
EdmMail m=new EdmMail("test");
//附件的位置
m.sendMail("E:\\张冰\\闲杂\\pic273.jpg");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值