寫在前面
本文采用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;