java outlook 发送邮件_基于java使用JavaMail发送邮件
一、郵件的相關(guān)概念
郵件協(xié)議。主要包括:
SMTP協(xié)議:Simple Mail Transfer Protocol,即簡(jiǎn)單郵件傳輸協(xié)議,用于發(fā)送電子郵件
POP3協(xié)議:Post Office Protocol 3,即郵局協(xié)議的第三個(gè)版本,用于接收郵件
IMAP協(xié)議:Internet Message Access Protocol,即互聯(lián)網(wǎng)消息訪問(wèn)協(xié)議,是POP3的替代協(xié)議
--------------------------------------------------------------------------------
二、搭建James郵件服務(wù)器
James是Apache的一個(gè)開(kāi)源項(xiàng)目,純Java實(shí)現(xiàn)
搭建James服務(wù)器
① 下載apache-james-2.3.2.zip解壓
② 運(yùn)行bin目錄下的run.bat即可啟動(dòng)服務(wù)器[Telnet? localhost 4555]
③ 通過(guò)apps\james\SAR-INF\config.xml配置服務(wù)器
注:先到bin下run一道。 放如非中文目錄, 得再控制面板開(kāi)啟Telnet客戶端
--------------------------------------------------------------------------------
三、安裝OutLook[郵件客戶端]
產(chǎn)品秘鑰:PQDV9-GPDV4-CRM4D-PHDTH-4M2MT
創(chuàng)建用戶賬號(hào)
一、使用telnet連接James的Remote Administration Tool
二、以管理員身份登錄
三、使用adduser命令添加用戶
--------------------------------------------------------------------------------
四、配置outlook郵件客戶端
為了方便查看,可以配置Microsoft Outlook郵件客戶端,保證James郵件服務(wù)器是啟動(dòng)狀態(tài),啟動(dòng)Microsoft Outlook.
選擇“工具”->“選項(xiàng)”,打開(kāi)“選項(xiàng)”面板。選擇“郵件設(shè)置”并點(diǎn)擊“電子郵件賬戶”,打開(kāi)“賬號(hào)設(shè)置”面板。在“電子郵件”選項(xiàng)卡下新建郵件賬戶
--------------------------------------------------------------------------------
五、案例[搭建James郵件服務(wù)器]
需求說(shuō)明:
在本機(jī)搭建James郵件服務(wù)器,自定義服務(wù)器的名稱。
創(chuàng)建兩個(gè)測(cè)試用戶。
在Microsoft Outlook中配置其中一個(gè)測(cè)試用戶為Outlook郵件賬戶
--------------------------------------------------------------------------------
六、使用JavaMail發(fā)送電子郵件(案例)
需求:
使用JavaMail技術(shù),實(shí)現(xiàn)從A賬戶給B賬戶發(fā)送一封電子郵件,標(biāo)題為“會(huì)議通知”,郵件內(nèi)容為“XX你好!請(qǐng)于明天下午16:00 準(zhǔn)時(shí)到B01會(huì)議室召開(kāi)技術(shù)討論會(huì)。”通過(guò)Outlook 客戶端查看郵件程序發(fā)送的郵件是否發(fā)送成功
關(guān)鍵代碼:
創(chuàng)建一個(gè)類EmailAuthenticator并繼承自Authenticator,并植入用戶名和密碼
創(chuàng)建Mail類設(shè)置郵件信息:
public class Mail {
private String mailServer,from,to,mailSubject,mailContent;
private String username,password;
public Mail(){
//設(shè)置郵件信息
//進(jìn)行認(rèn)證登錄的用戶名
username="hq@mail.com";
//認(rèn)證密碼
password="hq";
//認(rèn)證的郵箱對(duì)應(yīng)的郵件服務(wù)器
mailServer="192.168.17.176";
//發(fā)件人信息
from="wj";
//收件人信息
to="wj@mail.com";
//郵件標(biāo)題
mailSubject="我們都是好孩子333";
//郵件內(nèi)容
mailContent="這是一封測(cè)試郵件!如有雷同,純屬不可能";
}
//設(shè)置郵件服務(wù)器
@SuppressWarnings("static-access")
public void send(){
Properties prop=System.getProperties();
//指定郵件server
prop.put("mail.smtp.host", mailServer);
//是否開(kāi)啟認(rèn)證
prop.put("mail.smtp.auth", "true");
//smtp協(xié)議的
prop.put("mail.smtp.port", "25");
//產(chǎn)生Session服務(wù)
EmailAuthenticator mailauth=new EmailAuthenticator(username, password);
Session mailSession=Session.getInstance(prop,(Authenticator)mailauth);
try {
//封裝Message對(duì)象
Message message=new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from)); //發(fā)件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//收件人
message.setSubject(mailSubject);
//設(shè)置內(nèi)容(設(shè)置字符集處理亂碼問(wèn)題)
message.setContent(mailContent,"text/html;charset=gbk");
message.setSentDate(new Date());
//創(chuàng)建Transport實(shí)例,發(fā)送郵件
Transport tran=mailSession.getTransport("smtp");
tran.send(message,message.getAllRecipients());
tran.close();
} catch (Exception e) {
e.printStackTrace();
}
}
測(cè)試類:
public class MyTest {
public static void main(String[] args) {
Mail mail=new Mail();
mail.send();
System.out.println("success!");
}
}
--------------------------------------------------------------------------------
七、發(fā)送帶附件的Mail
public class MailWithAttachment {
private JavaMailSender mailSender; //必須使用 JavaMailSender
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void send() throws MessagingException,IOException{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setFrom("hq@mail.com");
helper.setTo("wj@mail.com");
helper.setSubject("哈哈哈");
helper.setText("每日一笑,開(kāi)開(kāi)心心!!!");
//添加附件1
ClassPathResource file1 = new ClassPathResource(
"/cn/bdqn/attachfiles/test.doc");
helper.addAttachment(file1.getFilename(), file1.getFile());
//添加附件2:附件的文件名為中文時(shí),需要對(duì)文件名進(jìn)行編碼轉(zhuǎn)換,解決亂碼問(wèn)題
ClassPathResource file2 = new ClassPathResource(
"/cn/bdqn/attachfiles/附件測(cè)試文件.doc");
helper.addAttachment(MimeUtility.encodeWord(file2.getFilename()),file2.getFile());
mailSender.send(mimeMessage);
}
}
測(cè)試類:
public class MailTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/*測(cè)試帶附件的郵件*/
try{
MailWithAttachment mailWithAttach = (MailWithAttachment)context.getBean("mailWithAttachment");
mailWithAttach.send();
}catch(Exception e){
System.out.print(e.toString());
}
}
}
applicationContext.xml:大配置
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
總結(jié)
以上是生活随笔為你收集整理的java outlook 发送邮件_基于java使用JavaMail发送邮件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: materialrefeshlayout
- 下一篇: java 文件通配符_Java中泛型通配