您的位置:首页 > 编程语言 > Java开发

使用JavaMail 代发邮件(测试邮箱:gmail)

2009-07-17 22:51 543 查看
1、首先,下载两个jar包:activation.jar和mail.jar,(下载地址:http://download.csdn.net/source/1498838)

2、项目添加Jar包,...

3、登录你的Gmail帐户,然后,设置->转发和POP/IMAP->启用IMAP;



4、编写sendMail函数

// 参数说明:toMail(你所要发送的邮箱地址),subject(你所发送的邮件主题),context(你所发送的邮件内容)
public static void sendEmail(String toMail, String subject, String context) {
try {
String fromMail = "hqccheng@gmail.com";		// 填写你的gmail帐号
String password = "password";							// 填写你的gmail密码

Properties props = new Properties();
props.put("mail.smtp.starttls.enable","true");		// 设置使用SSL
props.put("mail.smtp.host", "聆听");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);

// 给消息对象设置收件人、发件人、主题、发信时间
InternetAddress fromAddress = new  InternetAddress(fromMail);
message.setFrom(fromAddress);
InternetAddress toAddress = new  InternetAddress(toMail);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setSentDate(new Date());
// 新建一个MimeMultipart对象来存放多个BodyPart对象
Multipart multipart = new MimeMultipart();
// 新建一个存放信件内容的BodyPart对象
BodyPart mdp = new MimeBodyPart();
mdp.setContent(context, "text/html;charset=gb2312");
multipart.addBodyPart(mdp);
message.setContent(multipart);
message.saveChanges();
Transport transport = session.getTransport("smtp");
// 以smtp方式登陆邮箱,第1个参数是发送邮件用的邮件服务器SMTP地址,第2个参数为用户名,第3个参数为密码
transport.connect("smtp.gmail.com", fromMail, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}


5、新建一个main函数,调用。附上我的测试结果:

DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false
220 mx.google.com ESMTP x6sm3749506gvf.9
DEBUG SMTP: connected to host "smtp.gmail.com", port: 25

EHLO hqc-PC
250-mx.google.com at your service, [211.68.41.00]
250-SIZE 35651584
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35651584"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO hqc-PC
250-mx.google.com at your service, [211.68.41.00]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250 PIPELINING
DEBUG SMTP: Found extension "SIZE", arg "35651584"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
bGluZ3RpbmdCQlNAZ21haWwuY29t
334 UGFzc3dvcmQ6
bHRiYnMxMjM0NQ==
235 2.7.0 Accepted
DEBUG SMTP: use8bit false
MAIL FROM:<hqccheng@gmail.com>
250 2.1.0 OK x6sm3749506gvf.9
RCPT TO:<hqccheng@yahoo.cn>
250 2.1.5 OK x6sm3749506gvf.9
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   hqccheng@yahoo.cn
DATA
354  Go ahead x6sm3749506gvf.9
Date: Fri, 17 Jul 2009 22:47:40 +0800 (CST)
From: hqccheng@gmail.com
To: hqccheng@yahoo.cn
Message-ID: <27249769.01247842060351.JavaMail.hqc@hqc-PC>
Subject: =?GBK?B?8fbM/cLbzLMobHRiYnMpw9zC68ihu9goz7XNs9PKvP4p?=
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_0_15980197.1247842060156"

------=_Part_0_15980197.1247842060156
Content-Type: text/html;charset=gb2312
Content-Transfer-Encoding: base64

xPrU2vH2zP3C28yzKGx0YmJzKcv516Ky4bXE1cq6xaO6ZmRzYSC1xMPcwuvOqqO6MTIzNDU2IKGj
PGJyPsfrsaO53LrDxOO1xMPcwushICA8YnI+PGJyPrWxx7DPtc2zyrG85KO6MDktNy0xNyDPws7n
MTA6NDc=
------=_Part_0_15980197.1247842060156--

.
250 2.0.0 OK 1247842070 x6sm3749506gvf.9
QUIT
221 2.0.0 closing connection x6sm3749506gvf.9


好了,结束了,完成时间20分钟,现在时间 23:10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: