Java发邮件的例子,JavaMail实例代码,邮件发送在Java中不是难点,但很实用,很多程序都是基于网络的,所以邮件的发送已经成为基础功能了。
核心主文件代码如下:
001 | package com.pb.mail; |
002 | import java.util.Properties; |
003 | import javax.mail.Address; |
004 | import javax.mail.BodyPart; |
005 | import javax.mail.Message; |
006 | import javax.mail.Multipart; |
007 | import javax.mail.Session; |
008 | import javax.mail.Transport; |
009 | import javax.mail.internet.InternetAddress; |
010 | import javax.mail.internet.MimeBodyPart; |
011 | import javax.mail.internet.MimeMessage; |
012 | import javax.mail.internet.MimeMultipart; |
013 | public class Mail { |
014 | private MimeMessage mimeMsg; |
015 | private Session session; |
016 | private Properties props; |
017 | private String username; |
018 | private String password; |
019 | private Multipart mp; |
020 | public Mail(String smtp) { |
021 | setSmtpHost(smtp); |
022 | createMimeMessage(); |
023 | } |
024 | public void setSmtpHost(String hostName) { |
025 | System.out.println( "设置系统属性:mail.smtp.host=" + hostName); |
026 | if (props == null ) { |
027 | props = System.getProperties(); |
028 | } |
029 | props.put( "mail.smtp.host" , hostName); |
030 | } |
031 | public boolean createMimeMessage() { |
032 | try { |
033 | System.out.println( "准备获取邮件会话对象!" ); |
034 | session = Session.getDefaultInstance(props, null ); |
035 | } catch (Exception e) { |
036 | System.out.println( "获取邮件会话错误!" + e); |
037 | return false ; |
038 | } |
039 | System.out.println( "准备创建MIME邮件对象!" ); |
040 | try { |
041 | mimeMsg = new MimeMessage(session); |
042 | mp = new MimeMultipart(); |
043 |
044 | return true ; |
045 | } catch (Exception e) { |
046 | System.out.println( "创建MIME邮件对象失败!" + e); |
047 | return false ; |
048 | } |
049 | } |
050 |
051 | /*定义SMTP是否需要验证*/ |
052 | public void setNeedAuth(boolean need) { |
053 | System.out.println("设置smtp身份认证:mail.smtp.auth = " + need); |
054 | if (props == null) |
055 | props = System.getProperties(); |
056 | if (need) { |
057 | props.put("mail.smtp.auth", "true"); |
058 | } else { |
059 | props.put("mail.smtp.auth", "false"); |
060 | } |
061 | } |
062 | public void setNamePass(String name, String pass) { |
063 | username = name; |
064 | password = pass; |
065 | } |
066 |
067 | /*定义邮件主题*/ |
068 | public boolean setSubject(String mailSubject) { |
069 | System.out.println("定义邮件主题!"); |
070 | try { |
071 | mimeMsg.setSubject(mailSubject); |
072 | return true; |
073 | } catch (Exception e) { |
074 | System.err.println("定义邮件主题发生错误!"); |
075 | return false; |
076 | } |
077 | } |
078 |
079 | /*定义邮件正文*/ |
080 | public boolean setBody(String mailBody) { |
081 | try { |
082 | BodyPart bp = new MimeBodyPart(); |
083 | bp.setContent("" + mailBody, "text/html;charset=GBK"); |
084 | mp.addBodyPart(bp); |
085 | return true; |
086 | } catch (Exception e) { |
087 | System.err.println("定义邮件正文时发生错误!" + e); |
088 | return false; |
089 | } |
090 | } |
091 |
092 | /*设置发信人*/ |
093 | public boolean setFrom(String from) { |
094 | System.out.println("设置发信人!"); |
095 | try { |
096 | mimeMsg.setFrom(new InternetAddress(from)); //发信人 |
097 | return true; |
098 | } catch (Exception e) { |
099 | return false; |
100 | } |
101 | } |
102 |
103 | /*定义收信人*/ |
104 | public boolean setTo(String to) { |
105 | if (to == null) |
106 | return false; |
107 | System.out.println("定义收信人!"); |
108 | try { |
109 | mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); |
110 | return true; |
111 | } catch (Exception e) { |
112 | return false; |
113 | } |
114 | } |
115 |
116 | /*定义抄送人*/ |
117 | public boolean setCopyTo(String copyto) { |
118 | if (copyto == null) |
119 | return false; |
120 | try { |
121 | mimeMsg.setRecipients(Message.RecipientType.CC, (Address[]) InternetAddress |
122 | .parse(copyto)); |
123 | return true; |
124 | } catch (Exception e) { |
125 | return false; |
126 | } |
127 | } |
128 |
129 | /*发送邮件模块*/ |
130 | public boolean sendOut() { |
131 | try { |
132 | mimeMsg.setContent(mp); |
133 | mimeMsg.saveChanges(); |
134 | System.out.println("邮件发送中...."); |
135 | Session mailSession = Session.getInstance(props, null); |
136 | Transport transport = mailSession.getTransport("smtp"); |
137 | transport.connect((String) props.get("mail.smtp.host"), username, password); |
138 | transport.sendMessage(mimeMsg, mimeMsg |
139 | .getRecipients(Message.RecipientType.TO)); |
140 | System.out.println("发送成功!"); |
141 | transport.close(); |
142 | return true; |
143 | } catch (Exception e) { |
144 | System.err.println("邮件失败!" + e); |
145 | return false; |
146 | } |
147 | } |
148 |
149 | /*调用sendOut方法完成发送*/ |
150 | public static boolean sendAndCc(String smtp, String from, String to, String copyto, |
151 | String subject, String content, String username, String password) { |
152 | Mail theMail = new Mail(smtp); |
153 | theMail.setNeedAuth( true ); // 验证 |
154 | if (!theMail.setSubject(subject)) |
155 | return false ; |
156 | if (!theMail.setBody(content)) |
157 | return false ; |
158 | if (!theMail.setTo(to)) |
159 | return false ; |
160 | if (!theMail.setCopyTo(copyto)) |
161 | return false ; |
162 | if (!theMail.setFrom(from)) |
163 | return false ; |
164 | theMail.setNamePass(username, password); |
165 | if (!theMail.sendOut()) |
166 | return false ; |
167 | return true ; |
168 | } |
169 | } |
以下文件代码也必需的:
01 | package com.pb.mail; |
02 | public class SendMail { |
03 | public static void main(String[] args) { |
04 | String smtp = "smtp.126.com" ; // smtp服务器 |
05 | String from = "abcdefg@126.com" ; // 邮件显示名称 |
06 | String to = "123456@qq.com" ; // 收件人的邮件地址,必须是真实地址 |
07 | String copyto = "" ; // 抄送人邮件地址 |
08 | String subject = "测试邮件" ; // 邮件标题 |
09 | String content = "你好!" ; // 邮件内容 |
10 | String username = "abcdefg" ; // 发件人真实的账户名 |
11 | String password = "123456789" ; // 发件人密码 |
12 | Mail.sendAndCc(smtp, from, to, copyto, subject, content, username, password); |
13 | } |
14 | } |