如何实现Java程序发送邮件详解程序员

Java Mail发送邮件流程:

简介

  Java程序发送邮件是很频繁的操作,这两天整理一下,分两篇,第一篇介绍Java Mail发送一个简单的邮件需要哪些步骤以及准备工作。第二篇整理一下Java Mail发送带有照片、附件的邮件,然后封装一下作为一个小module在以后使用的地方使用。写完之后module会放到github上。

准备条件

  • 一个开通了smtp协议(关于smtp协议的内容可以参考一下维基百科)的邮箱帐号,这里以163邮箱为例(这里同样要注意一下,新申请的163邮箱默认是不开通smtp协议的,要手动开启,后面具体步骤会有)。
  • 下载Java Mail APIjar包,我项目中用的是maven

具体步骤

开通smtp协议的邮箱

申请163之后可以在下图中看到163 帐号查看smtp协议,新申请的邮箱默认是没有开通的,可以按照提示一步一步开启。

下载Java Mail

这里使用的是maven,方便、高效,不用手动去扒。

代码

核心发送代码

package org.oscar.mail.service.impl; 
http://rrmsyekdxm.inuobi.com/  http://pyxpuounhf.inuobi.com/  http://wjdskoqkag.inuobi.com/  http://bufwyojpgp.inuobi.com/ 
import org.oscar.mail.service.ICommunicate; 
import org.oscar.mail.support.Constants; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
http://zucinualem.inuobi.com/  http://uowoyekdwg.inuobi.com/  http://idmdnuzslu.inuobi.com/  http://xangepvhwe.inuobi.com/ 
import javax.mail.*; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Properties; 
http://sftyrlqkdo.inuobi.com/  http://khsphntmfn.inuobi.com/  http://gghyczsxdx.inuobi.com/  http://bzuikchnlv.inuobi.com/ 
/** 
* Description : 
* http://ivrjbhngzz.inuobi.com/  http://wbguvzortc.inuobi.com/  http://fxldvbhatz.inuobi.com/  http://abwogmfywq.inuobi.com/ 
* @author : oscar 
* @version :1.0, 2016/7/9 
*/ http://rqlmtjybsy.inuobi.com/  http://svdvauzsmy.inuobi.com/  http://tlgyqwcvon.inuobi.com/  http://wbjufbzydq.inuobi.com/ 
public class JavaMailImpl implements ICommunicate { 
http://zmgmclabiz.inuobi.com/  http://objxndwbtb.inuobi.com/  http://algynefuwr.inuobi.com/  http://emwgyejcwr.inuobi.com/ 
private static final Logger LOGGER = LoggerFactory.getLogger(JavaMailImpl.class); 
http://krotxcuntb.inuobi.com/  http://demxpibuau.inuobi.com/  http://jwumqjbmct.inuobi.com/  http://xomitrpbtd.inuobi.com/ 
@Override  http://yvrukdzcal.inuobi.com/  http://wtphlrhvzy.inuobi.com/  http://mqrmyvqral.inuobi.com/  http://kjipwabpqh.inuobi.com/ 
public String sendSimpleMail(List<String> addressList, String title, Object content) { 
LOGGER.debug("|- Start send simple email..."); 
http://jtbeiegvlw.inuobi.com/  http://anjreaiitp.inuobi.com/  http://iynzkjvkcm.inuobi.com/  http://whmrqscona.inuobi.com/ 
Properties props = new Properties(); 
//使用的邮件服务器类型  http://qmumwcibub.inuobi.com/  http://zblnqwizlx.inuobi.com/  http://bpdxpunegl.inuobi.com/ 
props.setProperty(Constants.MAIL_HOST_KEY, Constants.MAIL_HOST_VALUE); 
//使用的认证方式  http://foppulcgqj.inuobi.com/  http://rezrjpvgzg.inuobi.com/  http://wvnbvlaqsx.inuobi.com/ 
props.setProperty(Constants.MAIL_AUTH_KEY, Constants.MAIL_AUTH_VALUE); 
http://pyhzrlibvc.inuobi.com/  http://etvnfmexdk.inuobi.com/  http://izzsbmgauv.inuobi.com/  http://zgekufjqjc.inuobi.com/ 
Authenticator authenticator = new Authenticator() { 
public PasswordAuthentication getPasswordAuthentication() { 
return new PasswordAuthentication(Constants.MAIL_USERNAME, Constants.MAIL_PASSWORD); 
} http://ffbtdjpirf.inuobi.com/  http://ksiasyexqm.inuobi.com/  http://jvscuatlsb.inuobi.com/ 
};  http://nljbszepjp.inuobi.com/  http://yqabpgiqtl.inuobi.com/  http://egecozpxar.inuobi.com/  http://bvblchydjr.inuobi.com/ 
http://kirgtyujzs.inuobi.com/  http://rwgyqwcvgp.inuobi.com/  http://tptuildjpw.inuobi.com/  http://qbikldsupu.inuobi.com/ 
Session session = Session.getInstance(props, authenticator); 
MimeMessage msg = new MimeMessage(session); 
try { http://gwdehxzpwq.inuobi.com/  http://zxxxvvyhil.inuobi.com/   http://wzmbqvarvb.inuobi.com/  http://dgwullqvny.inuobi.com/ 
// 设置邮件的发件人  http://xbzqtlfzql.inuobi.com/  http://pvqsnfwotj.inuobi.com/  http://yxkcvbgztg.inuobi.com/ 
msg.setFrom(new InternetAddress(Constants.MAIL_FROM)); 
// 设置收件人  http://iawououngv.inuobi.com/ http://giqizyqjpb.inuobi.com/ http://jmhnhbttlu.inuobi.com/ 
Address[] adds = convertAddress(addressList); 
msg.setRecipients(Message.RecipientType.TO, adds); 
// 设置邮件标题  http://vqcvsmslfl.inuobi.com/ http://xieukoxcua.inuobi.com/ http://xudasmjoua.inuobi.com/ 
msg.setSubject(title); http://drmewcunmf.inuobi.com/ http://jeqiaumfmf.inuobi.com/ http://hngdfikzcj.inuobi.com/ 
// 设置邮件内容 http://rbdvntzsls.inuobi.com/ http://xtybmdiuom.inuobi.com/ http://dgcumslekj.inuobi.com/ 
msg.setContent(content, Constants.MAIL_TYPE); 
//发邮件 http://htsnvktqnx.inuobi.com/ http://bsjbefqrrp.inuobi.com/ http://aiiagzfzsg.inuobi.com/ http://viosvcuquf.inuobi.com/ 
Transport.send(msg); http://ioqhggrqcq.inuobi.com/ http://szbjtbzkxh.inuobi.com/ http://slqfblptyh.inuobi.com/ 
} catch (MessagingException e) { http://rfwptyakdy.inuobi.com/ http://grkjuvviae.inuobi.com/ http://jnjpvpvoaw.inuobi.com/ 
e.printStackTrace(); http://iqqiauatmi.inuobi.com/ http://smwbttzszt.inuobi.com/ http://bkfifyqhnv.inuobi.com/ 
return "Send failed, the reason:" + e.getMessage(); 
} http://yuxotmfljc.inuobi.com/ http://baqrlorvni.inuobi.com/ http://pkvbgtzfyx.inuobi.com/ http://poaeworiyq.inuobi.com/ 
return " Send success"; http://ganiromrom.inuobi.com/ http://pznbkewoif.inuobi.com/ http://xiqvnhngmm.inuobi.com/ 
} http://zbaqtloevr.inuobi.com/ http://rhcumgmfdy.inuobi.com/ http://yzulelqjvq.inuobi.com/ http://omhxlunsyt.inuobi.com/ 
http://tutaudxkff.inuobi.com/ http://dbxdicitnj.inuobi.com/  http://shfavhagzw.inuobi.com/ http://eyvkmpshkw.inuobi.com/ 
http://ugiqtdnhob.inuobi.com/ http://xwfkvpuohb.inuobi.com/ http://eazjpwcwck.inuobi.com/ http://dosxqxqxrz.inuobi.com/ 
http://qqntzzysaj.inuobi.com/ http://kmsldxdwqc.inuobi.com/ http://dvvoazbopb.inuobi.com/ http://jgskbshnut.inuobi.com/ 
/** http://ztvcvoevoh.inuobi.com/ http://mbwchbhavw.inuobi.com/ http://azewozrwpw.inuobi.com/ http://sxbxbewosp.inuobi.com/ 
* 将字符串类型邮箱地址转换为java mail能使用的邮箱地址 
* @param addressList 字符串类型邮箱地址集合 
* @return java mail使用的邮箱地址集合 
*/ http://bmyqwdbuoi.inuobi.com/ http://uisqvpibve.inuobi.com/ http://tdknewayqw.inuobi.com/ http://lzydzhzfli.inuobi.com/ 
private Address[] convertAddress(List<String> addressList) throws AddressException { 
List<Address> addresses = new ArrayList<>(); 
for (String addressStr : addressList) { 
//这里没有做邮箱的合法性校验,只是做了非空和null 
if (addressStr != null && addressStr.length() > 0) { 
addresses.add(new InternetAddress(addressStr)); 
} http://qbaxpvohav.inuobi.com/ http://qdalgpxksc.inuobi.com/ http://zrewovtzfz.inuobi.com/ http://zjsyqqwpwd.inuobi.com/ 
} http://gpmrkkdwqz.inuobi.com/ http://jnmiahgmut.inuobi.com/ http://xllvnuagzh.inuobi.com/ http://gvgtysrxdy.inuobi.com/ 
return addresses.toArray(new Address[addresses.size()]); 
} http://tcpitghqam.inuobi.com/  http://qdpwovtwzg.inuobi.com/ http://onjphpuoav.inuobi.com/ http://kurbhbhaho.inuobi.com/ 
} http://cgglchfwcw.inuobi.com/ http://cgcunuatnm.inuobi.com/ http://dalttpynqz.inuobi.com/ http://yielzjsolb.inuobi.com/ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

常量Constant.java

package org.oscar.mail.support; 
http://htsydpvove.inuobi.com/ http://edsedpbmza.inuobi.com/ http://bxhafntmxt.inuobi.com/ http://wxxlpydows.inuobi.com/ 
/** http://jrpibohovf.inuobi.com/ http://thbcjqjhaj.inuobi.com/ http://cwhzkdjcvr.inuobi.com/ http://kkxszddkdy.inuobi.com/ 
* Description : http://sflmvoetaw.inuobi.com/ http://riqudjbhai.inuobi.com/ http://ccqilpfqwf.inuobi.com/ 
* http://jjkqxlskto.inuobi.com/ http://qdnewpuzrb.inuobi.com/ http://qmjphgmslu.inuobi.com/ http://kwgnwrwcwk.inuobi.com/ 
* @author : oscar http://itloiacgki.inuobi.com/ http://enpunfydks.inuobi.com/ http://rclochbdhn.inuobi.com/ 
* @version :1.0, 2016/7/9 
*/ http://vuuwlcxzdi.inuobi.com/ http://gwggsserdi.inuobi.com/ http://kywbubzfzi.inuobi.com/ http://jgskqxdpif.inuobi.com/ 
public class Constants { http://ydmewqngau.inuobi.com/ http://xbdoipioir.inuobi.com/ http://ponflsyjdb.inuobi.com/ 
http://ultyyobchc.inuobi.com/  http://euintzrwuq.inuobi.com/ http://ezjohoufmu.inuobi.com/ http://qiuafrkdxe.inuobi.com/ 
/* http://hdbgzydjdx.inuobi.com/ http://npyejwbhnj.inuobi.com/ http://omvaysyrlt.inuobi.com/ http://cpllfcxghq.inuobi.com/ 
* 实际项目中可以将这段常量放在properties文件中。这里简单处理 
*/ http://dutdjqwpje.inuobi.com/ http://btdwojpngq.inuobi.com/ http://kvdvnhexrm.inuobi.com/ http://hfhzfmsmso.inuobi.com/ 
public static final String MAIL_HOST_KEY = "mail.host"; 
public static final String MAIL_HOST_VALUE = "smtp.163.com"; 
public static final String MAIL_AUTH_KEY = "mail.smtp.auth"; 
public static final String MAIL_AUTH_VALUE = "true"; 
public static final String MAIL_FROM = "[email protected]"; 
public static final String MAIL_TYPE = "text/html;charset=utf-8"; 
public static final String MAIL_USERNAME = "********"; 
public static final String MAIL_PASSWORD = "********"; 
}   http://fqbhzhmgnz.inuobi.com/ http://potmrlwpje.inuobi.com/ http://cllibhagzh.inuobi.com/ http://czpauivhuh.inuobi.com/ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

测试代码

package org.oscar.mail.service.impl; 
http://qwhlpvnfvs.inuobi.com/ http://glsniobxbo.inuobi.com/ http://hvamzktcmh.inuobi.com/ http://bjwrefkqwq.inuobi.com/ 
import org.junit.Before; 
import org.junit.Test; 
http://cirxpxvovd.inuobi.com/ http://urkxvwjvet.inuobi.com/ http://qugxsvkmqk.inuobi.com/ http://azatltmfzp.inuobi.com/ 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMultipart; 
import java.util.ArrayList; 
import java.util.List; 
http://vqyzbwmorv.inuobi.com/  http://kpyqjekdwz.inuobi.com/ http://zrqiaitivm.inuobi.com/ http://acevmchsmx.inuobi.com/ 
public class JavaMailImplTest { 
private JavaMailImpl javaMail; 
private List<String> addressList; 
http://zflacjouso.inuobi.com/ http://lezdjqvapr.inuobi.com/ http://yzzltdzcqy.inuobi.com/ http://yzjbgohflg.inuobi.com/ 
@Before 
public void setUp() { 
javaMail = new JavaMailImpl(); 
addressList = new ArrayList<>(); 
addressList.add("[email protected]"); 
addressList.add("[email protected]"); 
} 
http://ifshgjyqjr.inuobi.com/ http://khgbskbtki.inuobi.com/ 
@Test 
public void testSendMail() throws Exception { 
//使用中文是为了验证是否有乱码 
String title = "来自Java Mail的邮件"; 
String content = "<html><body><h1>您收到一封来自Java Mail的邮件,邮件内容是一张图片:</br></h1>" + 
"<img src=/"http://www.rgagnon.com/images/jht.gif/"/></body></html>"; 
String result = javaMail.sendSimpleMail(addressList, title, content); 
System.out.println(result); 
} 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

结果展示

QQ邮箱收到的邮件

如何实现Java程序发送邮件详解程序员

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/7405.html

(0)
上一篇 2021年7月17日
下一篇 2021年7月17日

相关推荐

发表回复

登录后才能评论