本文概要:主要简单介绍 javamail发送邮件,通过一个简单的例子,可以很容易的学会怎么样用java发送邮件.文章最后提供本事例的源代码下载

 

Test.java

 package org.zzp.mail.mine; 

public class Test { public static void main(String[] args) { MyMailSender m

本文概要:主要简单介绍 javamail发送邮件,通过一个简单的例子,可以很容易的学会怎么样用java发送邮件.
文章最后提供本事例的源代码下载

Test.java

[java]</p> <p>package org.zzp.mail.mine;</p> <p>public class Test {</p> <p>public static void main(String[] args) {</p> <p>MyMailSender mail=new MyMailSender();</p> <p>mail.setSenderAccount("发件人用户名");</p> <p>mail.setSenderPassword("发件人密码");</p> <p>mail.setSenderName("发件人显示姓名");</p> <p>mail.setSenderAddress("发件邮箱地址");</p> <p>mail.setHost("发件人smtp服务器地址");</p> <p>mail.setSubject("邮件主题");</p> <p>mail.setBody("邮件正文");</p> <p>mail.addTo("收件人地址");</p> <p>mail.addTo("收件人地址");</p> <p>mail.addCC("抄送地址");</p> <p>mail.addBCC("密送地址");</p> <p>try {</p> <p>System.out.println("正在发送");</p> <p>mail.send();</p> <p>System.out.println("成功");</p> <p>} catch (Exception e) {</p> <p>e.printStackTrace();</p> <p>}</p> <p>}</p> <p>}</p><p>[/java]

SenderAccount.java

[java]</p><p>package org.zzp.mail.mine;</p> <p>import javax.mail.Authenticator;</p> <p>import javax.mail.PasswordAuthentication;</p> <p>public class SenderAccount extends Authenticator {</p> <p>private String username;</p> <p>private String password;</p> <p>private String senderAddress;</p> <p>private String senderName;</p> <p>private String host;</p> <p>&nbsp;</p> <p>public SenderAccount(){</p> <p>super();</p> <p>}</p> <p>public String getUsername() {</p> <p>return username;</p> <p>}</p> <p>public void setUsername(String username) {</p> <p>this.username = username;</p> <p>}</p> <p>public String getPassword() {</p> <p>return password;</p> <p>}</p> <p>public void setPassword(String password) {</p> <p>this.password = password;</p> <p>}</p> <p>public String getSenderAddress() {</p> <p>return senderAddress;</p> <p>}</p> <p>public void setSenderAddress(String fromAddress) {</p> <p>senderAddress = fromAddress;</p> <p>}</p> <p>public String getSenderName() {</p> <p>return senderName;</p> <p>}</p> <p>public void setSenderName(String fromName) {</p> <p>senderName = fromName;</p> <p>}</p> <p>public String getHost() {</p> <p>return host;</p> <p>}</p> <p>public void setHost(String host) {</p> <p>this.host = host;</p> <p>}</p> <p>public PasswordAuthentication getPasswordAuthentication(){</p> <p>return new PasswordAuthentication(username,password);</p> <p>}</p> <p>}</p><p>[/java]

MyMailSender.java

[java]</p><p>package org.zzp.mail.mine;</p> <p>import java.util.HashSet;</p> <p>import java.util.Properties;</p> <p>import java.util.Set;</p> <p>import javax.mail.Message;</p> <p>import javax.mail.Session;</p> <p>import javax.mail.Transport;</p> <p>import javax.mail.internet.InternetAddress;</p> <p>import javax.mail.internet.MimeMessage;</p> <p>public class MyMailSender {</p> <p>private Set&lt;String&gt; to;</p> <p>private Set&lt;String&gt; cc;</p> <p>private Set&lt;String&gt; bcc;</p> <p>private String subject;</p> <p>private String body;</p> <p>private SenderAccount senderAccount;</p> <p>public MyMailSender() {</p> <p>to = new HashSet&lt;String&gt;();</p> <p>cc = new HashSet&lt;String&gt;();</p> <p>bcc = new HashSet&lt;String&gt;();</p> <p>senderAccount = new SenderAccount();</p> <p>&gt; }</p> <p>public void setSenderAccount(String username) {</p> <p>this.senderAccount.setUsername(username);</p> <p>}</p> <p>public void setSenderPassword(String senderPassword) {</p> <p>this.senderAccount.setPassword(senderPassword);</p> <p>}</p> <p>public void setSenderAddress(String senderAddress) {</p> <p>this.senderAccount.setSenderAddress(senderAddress);</p> <p>}</p> <p>public void setSenderName(String senderName) {</p> <p>this.senderAccount.setSenderName(senderName);</p> <p>}</p> <p>public void setHost(String host) {</p> <p>this.senderAccount.setHost(host);</p> <p>}</p> <p>public String getHost() {</p> <p>return this.senderAccount.getHost();</p> <p>}</p> <p>public String getSubject() {</p> <p>return subject;</p> <p>}</p> <p>public void setSubject(String title) {</p> <p>this.subject = title;</p> <p>}</p> <p>public String getBody() {</p> <p>return body;</p> <p>}</p> <p>public void setBody(String body) {</p> <p>this.body = body;</p> <p>}</p> <p>public void addTo(String to) {</p> <p>this.to.add(to);</p> <p>}</p> <p>public void addCC(String e) {</p> <p>this.cc.add(e);</p> <p>}</p> <p>public void addBCC(String e) {</p> <p>this.bcc.add(e);</p> <p>}</p> <p>// 发送</p> <p>public void send() throws Exception {</p> <p>Properties props = new Properties();</p> <p>props.put("mail.smtp.host", senderAccount.getHost());</p> <p>props.put("mail.smtp.auth", "true");</p> <p>Session session = Session.getDefaultInstance(props, senderAccount);</p> <p>MimeMessage message = new MimeMessage(session);</p> <p>message.setSubject(subject);</p> <p>message.setText(body);</p> <p>message.setFrom(new InternetAddress(senderAccount.getSenderAddress(),</p> <p>senderAccount.getSenderName()));</p> <p>// 添加收件人</p> <p>for (String s : to) {</p> <p>message.addRecipient(Message.RecipientType.TO, new InternetAddress(</p> <p>s));</p> <p>}</p> <p>// 添加抄送</p> <p>for (String s : cc) {</p> <p>message.addRecipient(Message.RecipientType.CC,</p> <p>new InternetAddress(s));</p> <p>}</p> <p>// 添加密送</p> <p>for (String s : bcc) {</p> <p>message.addRecipient(Message.RecipientType.BCC,</p> <p>new InternetAddress(s));</p> <p>}</p> <p>// message.setSentDate(new Date(1988,10,12));</p> <p>Transport.send(message);</p> <p>}</p> <p>}</p><p>[/java]

附件下载:
javamail_example.zip (事例源代码)

javamail.jar包  javamail.zip