天天看点

使用JavaMail SMTP协议发送邮件 使用JavaMail SMTP协议发送邮件

使用JavaMail SMTP协议发送邮件

最近需要实现通过发送邮件让用户找回密码的功能,自己用Socket写了SMTP协议的邮件发送程序,但是很多邮件服务器的anti-spam需要验证发送邮箱的合法性,所以只得放弃,后来发现用javamail包可以很方便的实现。示例程序使用gmail的邮件服务器来发送邮件。关于SMTP端口等配置见下面链接:

https://support.google.com/mail/bin/answer.py?hl=en&answer=13287

注:下面程序需导入javaee-api-6.0.jar 跟 mail.jar

1.使用TLS发送邮件

?

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

<span style=

"font-size:16px;"

>

import

java.util.Properties;

import

javax.mail.Message;

import

javax.mail.MessagingException;

import

javax.mail.PasswordAuthentication;

import

javax.mail.Session;

import

javax.mail.Transport;

import

javax.mail.internet.InternetAddress;

import

javax.mail.internet.MimeMessage;

public

class

SendMailTLS {

public

static

void

main(String[] args) {

final

String username =

"[email protected]"

;

final

String password =

"password"

;

Properties props =

new

Properties();

props.put(

"mail.smtp.auth"

,

"true"

);

props.put(

"mail.smtp.starttls.enable"

,

"true"

);

props.put(

"mail.smtp.host"

,

"smtp.gmail.com"

);

props.put(

"mail.smtp.port"

,

"587"

);

Session session = Session.getInstance(props,

new

javax.mail.Authenticator() {

protected

PasswordAuthentication getPasswordAuthentication() {

return

new

PasswordAuthentication(username, password);

}

});

try

{

Message message =

new

MimeMessage(session);

message.setFrom(

new

InternetAddress(

"[email protected]"

));

message.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(

"[email protected]"

));

message.setSubject(

"Testing Subject"

);

message.setText(

"Dear Mail Crawler,"

+

"\n\n No spam to my email, please!"

);

Transport.send(message);

System.out.println(

"Done"

);

}

catch

(MessagingException e) {

throw

new

RuntimeException(e);

}

}

}</span>

2.使用SSL发送邮件

?

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

<span style=

"font-size:18px;"

><span style=

"font-size:16px;"

>

import

java.util.Properties;

import

javax.mail.Message;

import

javax.mail.MessagingExcept</span>ion;

import

javax.mail.PasswordAuthentication;

import

javax.mail.Session;

import

javax.mail.Transport;

import

javax.mail.internet.InternetAddress;

import

javax.mail.internet.MimeMessage;

public

class

SendMailSSL {

public

static

void

main(String[] args) {

Properties props =

new

Properties();

props.put(

"mail.smtp.host"

,

"smtp.gmail.com"

);

props.put(

"mail.smtp.socketFactory.port"

,

"465"

);

props.put(

"mail.smtp.socketFactory.class"

,

"javax.net.ssl.SSLSocketFactory"

);

props.put(

"mail.smtp.auth"

,

"true"

);

props.put(

"mail.smtp.port"

,

"465"

);

Session session = Session.getDefaultInstance(props,

new

javax.mail.Authenticator() {

protected

PasswordAuthentication getPasswordAuthentication() {

return

new

PasswordAuthentication(

"username"

,

"password"

);

}

});

try

{

Message message =

new

MimeMessage(session);

message.setFrom(

new

InternetAddress(

"[email protected]"

));

message.setRecipients(Message.RecipientType.TO,

InternetAddress.parse(

"[email protected]"

));

message.setSubject(

"Testing Subject"

);

message.setText(

"Dear Mail Crawler,"

+

"\n\n No spam to my email, please!"

);

Transport.send(message);

System.out.println(

"Done"

);

}

catch

(MessagingException e) {

throw

new

RuntimeException(e);

}

}

}</span>