邮件是在日常生活中,这篇文章主要介绍了java实现基于SMTP发送邮件的方法,实例分析了java基于SMTP服务发送邮件的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下.
1.写一个 MailSendProper 类封装需要的属性
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
|
import java.util.Properties; public class MailSendProper { private String Host ; //发送邮件服务器的ip private String Port ; //发送邮件服务器的端口号 private String SendAddress ; //邮件发送者的地址 private String ReceiveAddress ; //邮件接受者的地址 private String username ; //登录发送邮箱的用户名 private String password ; //登录发送邮箱的密码 private boolean isvalidate = true ; //是否需要身份验证 private String subject ; //邮件标题 private String content ; //邮件内容 public String getSubject() { return subject; } public void setSubject(String subject) { this .subject = subject; } public String getContent() { return content; } public void setContent(String content) { this .content = content; } public String getHost() { return Host; } public void setHost(String host) { Host = host; } public String getPort() { return Port; } public void setPort(String port) { Port = port; } public String getSendAddress() { return SendAddress; } public void setSendAddress(String sendAddress) { SendAddress = sendAddress; } public String getReceiveAddress() { return ReceiveAddress; } public void setReceiveAddress(String receiveAddress) { ReceiveAddress = receiveAddress; } public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } public boolean isIsvalidate() { return isvalidate; } public void setIsvalidate( boolean isvalidate) { this .isvalidate = isvalidate; } public Properties getProperties(){ Properties properties = new Properties() ; properties.put( "mail.smtp.host" , this .Host) ; properties.put( "mail.smtp.port" , this .Port) ; properties.put( "mail.smtp.auth" , isvalidate? "true" : "false" ) ; return properties ; } } |
2、写一个 EmailMessage 封装 发送信息
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
|
public class EmailMessage { private String title ; private String context ; private String toEmail ; public EmailMessage() { super (); } public EmailMessage(String title, String context, String toEmail) { super (); this .title = title; this .context = context; this .toEmail = toEmail; } public String getTitle() { return title; } public void setTitle(String title) { this .title = title; } public String getContext() { return context; } public void setContext(String context) { this .context = context; } public String getToEmail() { return toEmail; } public void setToEmail(String toEmail) { this .toEmail = toEmail; } } |
3、写一个 MailAttorney 邮箱密码验证器 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class MailAttorney extends Authenticator { private String username ; private String password ; public MailAttorney(String username,String password) { this .username = username ; this .password = password ; } //覆盖父类方法,获取密码认证器 @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username,password) ; } } |
4 、 写一个 MailSend 邮件工具类
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
|
import java.util.Date; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.VTBBS.entity.EmailMessage; public class MailSend { public static boolean mailTest(MailSendProper mailsender){ MailAttorney attorney = null ; if (mailsender.isIsvalidate()){ //判断是否需要身份认证 attorney = new MailAttorney(mailsender.getUsername(),mailsender.getPassword()) ; } //根据邮箱会话属性和密码验证器构造一个发送邮箱的seesion Session session = Session.getInstance(mailsender.getProperties(),attorney) ; //根据session创建一个邮件消息 Message mailMessage = new MimeMessage(session) ; try { //创建邮件发送者的地址 Address from = new InternetAddress(mailsender.getSendAddress()) ; //设置邮箱消息的发送者 mailMessage.setFrom(from); //创建邮件接收这的地址,并设置到邮件消息中 Address to = new InternetAddress(mailsender.getReceiveAddress()) ; mailMessage.setRecipient(Message.RecipientType.TO, to); mailMessage.setSubject(mailsender.getSubject()); //设置邮件标题 mailMessage.setSentDate( new Date()); //设置发送邮件时间 mailMessage.setText(mailsender.getContent()); //设置邮件内容 Transport.send(mailMessage); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public static boolean sendEmail(EmailMessage message){ MailSendProper mail = new MailSendProper() ; mail.setHost( "smtp.126.com" ); //smtp简单的邮件传输协议,默认端口号是25, mail.setPort( "25" ); mail.setIsvalidate( true ); //需呀身份验证 mail.setUsername( "" ); //设置登录用户名 mail.setPassword( "" ); //设置发送者的密码 mail.setSendAddress( "" ); //设置发送这的地址 发送者地址和登录用户名是同一个 mail.setReceiveAddress(message.getToEmail()); //设置接收者的地址 mail.setSubject(message.getTitle()); //设置邮箱标题 mail.setContent(message.getContext()); //设置邮箱的内容 return mailTest(mail) ; } } |
注意:使用的邮箱必须开启POP3/SMTP服务 才能发送成功,不同的邮箱,邮箱的传输协议不一样如:
QQ邮箱:SMTP传输协议是 smtp.qq.com 端口 25
POP3传输协议是 pop3.qq.com 端口 110
5、使用方法测试
1
2
3
4
5
6
7
8
|
public static void main(String[] args) { EmailMessage message = new EmailMessage() ; String code = String.valueOf(Math.random()).substring( 3 , 9 ) ; //生成验证码 message.setTitle( "邮箱验证" ); //邮件标题 message.setContext( "尊敬的用户你好,你的验证码为" +code+ "。" ); //邮件内容 message.setToEmail( "940202884@qq.com" ); //要发送给谁 System.out.println(MailSend.sendEmail(message)? "发送成功" : "发送失败" ) ; } |
希望本文所述对大家学习java程序设计有所帮助。