写在前面
本文采用Eclipse编写代码,用maven依赖jar包,实现java 邮件发送功能,功能比较简单。如有疑问,请指出,大家一同探讨学习。
正文
整个流程包括
- 引入jar包
- 初始化邮件发件方信息
- 设置邮件相关相信
- 发送。校验数据,封装数据,发送。
1.pom.xml
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
2.发送方式:
(1) 纯文本邮件
public boolean send(String title,String Content,List Address);
(2) 带附件邮件
private boolean getInputData(String title2, String content2, List address, List files);
发送:
public boolean send(String title,String Content,List<String> Address,List<File> Files)
{
if(!getInputData(title,Content,Address,Files))
{
return false;
}
if(!checkData())
{
//数据校验
return false;
}
if(!dealData())
{
//数据处理
return false;
}
return true;
}
- 文件发送处理:
//发送邮件
System.out.println("收件人个数:"+Recivers.size());
for(int i=0;i<Recivers.size();i++)
{
Transport tTransport=null;
try
{
Properties tProperties = System.getProperties();
Session tSendMailSession;
tProperties.put("mail.smtp.auth", "true");
tProperties.put("mail.smtp.host", gServerName); //主机名
tProperties.put("mail.smtp.user", gPostAccount); // 发送方邮件地址。
tProperties.put("mail.smtp.password", gPostPassword); // 邮件密码
PopupAuthenticator tPop=new PopupAuthenticator();
tPop.performCheck(gPostAccount, gPostPassword);
tSendMailSession=Session.getInstance(tProperties, tPop);
//建立整个邮件
Message tEamil=new MimeMessage(tSendMailSession);
tEamil.setFrom(new InternetAddress(gPostAccount)); //设置发件人
Address[] tReceiver = InternetAddress.parse(Recivers.get(i), false);
tEamil.setRecipients(Message.RecipientType.TO, tReceiver); //设置收件人
tEamil.setSubject(title); //设置主题
tEamil.setSentDate(new Date()); //设置发件时间
//添加附件
String tContent="<div >"+content+"<div>";
if(attachFiles!=null&&attachFiles.size()>0)
{
Multipart tMultipart = new MimeMultipart();
//设置邮件正文
BodyPart tContentPart = new MimeBodyPart();
tContentPart.setContent(tContent, "text/html;charset=UTF-8");
tMultipart.addBodyPart(tContentPart);
for(int j=0;j<attachFiles.size();j++)
{
File tFile = attachFiles.get(j);
if(tFile.exists())
{
BodyPart tBodyPart = new MimeBodyPart();
DataSource tDataSource = new FileDataSource(tFile);
tBodyPart.setDataHandler(new DataHandler(tDataSource));
tBodyPart.setFileName(MimeUtility.encodeWord(tFile.getName()) );
tMultipart.addBodyPart(tBodyPart);
}
tEamil.setContent(tMultipart);
tEamil.saveChanges();
}
}
else
{
tEamil.setContent(tContent,"text/html;charset=\"GB2312\""); //设置正文内容
}
tTransport=tSendMailSession.getTransport("smtp");
//发送
System.out.println("开始发送!");
tTransport.send(tEamil);
System.out.println("发送成功!");
}
catch (AddressException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
continue;
}
catch (MessagingException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("发送失败!");
continue;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
if(tTransport!=null)
{
tTransport.close();
}
// 加上1s限制,避免因发送频繁而失败
Thread.sleep(1000L);
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
}
return true;