下载oracle javaMail API:
下载:JavaMail API 1.4.7
解压缩将lib文件夹中的jar文件全部拷贝到
zhousp@ubuntu:~/Documents/eclipsedoc/workspace/javamail/javamailApi目录下。
在Pachage Explorer中,右击javamail,在Build Path中选择Configure Build Path
Add External JARs
New-class
编写代码:
package javamail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
publicclass Sendmail {
/**
* @param args
* @throws Exception
*/
publicstaticvoid main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.126.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
ts.connect("smtp.126.com", "用户名", "password");
//4、创建邮件
Message message = createSimpleMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
publicstatic MimeMessage createSimpleMail(Session session)
throws Exception {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress("xxxx@126.com"));
//指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xxxx@126.com"));
//邮件的标题
message.setSubject("只包含文本的简单邮件");
//邮件的文本内容
message.setContent("你好啊!", "text/html;charset=UTF-8");
//返回创建好的邮件对象
returnmessage;
}
}
Run As – Java Application
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.126.com", port 25, isSSL false
220 126.com Anti-spam GT for Coremail System (126com[20140526])
DEBUG SMTP: connected to host "smtp.126.com", port: 25
EHLO ubuntu
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrM5ErrUCa0xDrUUUUj
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "coremail", arg "1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrM5ErrUCa0xDrUUUUj"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<zhoushuopeng@126.com>
250 Mail OK
RCPT TO:<zhoushuopeng@126.com>
250 Mail OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP: zhoushuopeng@126.com
DATA
354 End data with <CR><LF>.<CR><LF>
From: zhoushuopeng@126.com
To: zhoushuopeng@126.com
Message-ID: <526696804.0.1461464122621.JavaMail.zhousp@smtp.126.com>
Subject: =?UTF-8?B?5Y+q5YyF5ZCr5paH5pys55qE566A5Y2V6YKu5Lu2?=
MIME-Version: 1.0
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: base64
5L2g5aW95ZWK77yB
.
250 Mail OK queued as smtp2,DMmowEDpxEI5LBxX388gAA--.16606S2 1461464122
QUIT
221 Bye
发送带附件的邮件:
package javamail2;
import java.io.FileOutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
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;
publicclass sendmail {
/**
* @param args
* @throws Exception
*/
publicstaticvoid main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.126.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、连上邮件服务器
ts.connect("smtp.126.com", "用户名", "password");
//4、创建邮件
Message message = createAttachMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
publicstatic MimeMessage createAttachMail(Session session) throws Exception{
MimeMessage message = new MimeMessage(session);
//设置邮件的基本信息
//发件人
message.setFrom(new InternetAddress("xxxx@126.com"));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("zzzz@qq.com"));
//邮件标题
message.setSubject("JavaMail邮件发送测试");
//创建邮件正文,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码
MimeBodyPart text = new MimeBodyPart();
text.setContent("使用JavaMail创建的带附件的邮件", "text/html;charset=UTF-8");
//创建邮件附件
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src/1.jpg"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName()); //
//创建容器描述数据关系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
//将创建的Email写入到E盘存储
message.writeTo(new FileOutputStream("src/attachMail.eml"));
//返回生成的邮件
returnmessage;
}
}
DEBUG: setDebug: JavaMail version 1.4.7
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.126.com", port 25, isSSL false
220 126.com Anti-spam GT for Coremail System (126com[20140526])
DEBUG SMTP: connected to host "smtp.126.com", port: 25
EHLO ubuntu
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-coremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrXlMBNUCa0xDrUUUUj
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "coremail", arg "1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrXlMBNUCa0xDrUUUUj"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<zhoushuopeng@126.com>
250 Mail OK
RCPT TO:<391902958@qq.com>
250 Mail OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP: 391902958@qq.com
DATA
354 End data with <CR><LF>.<CR><LF>
From: zhoushuopeng@126.com
To: 391902958@qq.com
Message-ID: <2014432310.1.1461467389841.JavaMail.zhousp@smtp.126.com>
Subject: =?UTF-8?Q?JavaMail=E9=82=AE=E4=BB=B6=E5=8F=91=E9=80=81=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_86955769.1461467389824"
------=_Part_0_86955769.1461467389824
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: base64
5L2/55SoSmF2YU1haWzliJvlu7rnmoTluKbpmYTku7bnmoTpgq7ku7Y=
------=_Part_0_86955769.1461467389824
Content-Type: application/octet-stream; name=1.jpg
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=1.jpg
/9j/4Ra1RXhpZgAASUkqAAgAAAARAAABAwABAAAA0AwAAAEBAwABAAAAQBMAAAIBAwADAAAA2gAA
AAYBAwABAAAAAgAAAA8BAgASAAAA4AAAABABAgAJAAAA8gAAABIBAwABAAAAAQAAABUBAwABAAAA
….....
KT7T0fw/Gn+ry6+V/vH/AJmPmv8Aw9s7/wC76s9mW1f8k9f+aCf8dHUc3f8AuR/t/wDL1eX8bf8A
izYn/qBi/wChE9kt78T/AOrz6G1j/YD/AEo62mfgx/zK6j/5YD/oU+2LX+x/2vRgfhP29P8A3T/x
cJ/+Cy+z3ZuE/wDpT/k6Lt0+GH/V5dBhhP8Ai+bU/wCWqf8AEexUn/JPk/0v+ToMt/uVF9vR8No/
5+b/AILF/wBFe4t3j/cj8z1PPJ//ACTR9vQoeyTy6FnX/9k=
------=_Part_0_86955769.1461467389824--
.
250 Mail OK queued as smtp1,C8mowECZKEX2OBxX0n++AA--.33107S2 1461467417
QUIT
221 Bye