最近项目中需要用到java平台发送邮件,差了点资料,整理一下。
一、JavaMail
JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。我们可以基于JavaMail开发出类似于Microsoft Outlook的应用程序。
邮件传输的原理不得不提到SMTP协议:SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。SMTP协议属于TCP/IP协议族,它帮助每台计算机在发送或中转信件时找到下一个目的地。通过SMTP协议所指定的服务器,就可以把E-mail寄到收信人的服务器上了,整个过程只要几分钟。SMTP服务器则是遵循SMTP协议的发送邮件服务器,用来发送或中转发出的电子邮件。
二、一个简单的发送邮件的例子:
package useful; import java.util.Date; import java.util.Properties; import javax.mail.AuthenticationFailedException; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; /** * * @author AjunBoys * */ public class SendEmailUseJavaMail { private String host; private String username; private String password; private String from; private String to; private String subject; private String content; public String getHost() { return host; } public void setHost(String host) { this.host = host; } 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 String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } 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 SendEmailUseJavaMail(){ } //发送邮件 public boolean sendMail(){ try{ Properties props = new Properties();//存储连接参数 props.put("mail.smtp.host", host); //smtp主机 props.put("mail.smtp.auth", "true"); //需要身份验证 // props.put("mail.smtp.port", "465"); //gamil stmp port:465 Session session = Session.getInstance(props);//获取一个邮件session session.setDebug(true); MimeMessage message = new MimeMessage(session); //邮件会话新建一个邮件信息 //检查邮件地址是否合法 if(from == null || from ==""){ throw new Exception("Error Email Address in From."); } if(to == null || to ==""){ throw new Exception("Error Email Address in To."); } //设置源地址 //message.setFrom(new InternetAddress(from)); //等价于下面两条代码 InternetAddress addressFrom = new InternetAddress(from); message.setFrom(addressFrom); //设置目的地址 InternetAddress addressTo = new InternetAddress(to); message.setRecipient(Message.RecipientType.TO, addressTo); message.setSubject(subject); //message.setText(content); //邮件正文 Multipart mp = new MimeMultipart();//邮件内容 MimeBodyPart mbpContent = new MimeBodyPart(); mbpContent.setContent(content,"text/html"); //邮件格式 //向MimeMessage添加代表正文(Multipart) mp.addBodyPart(mbpContent); message.setContent(mp); message.setSentDate(new Date()); //设置发送的时间 Transport transport = session.getTransport("smtp"); //设置邮件传输 transport.connect((String)props.get("mail.smtp.host"), username, password); //连接主机 transport.sendMessage(message, message.getRecipients(MimeMessage.RecipientType.TO)); //发送邮件 System.out.println("Send Success !"); transport.close(); //关闭传输 return true; }catch(AuthenticationFailedException e){ e.printStackTrace(); return false; } catch(MessagingException e){ e.printStackTrace(); return false; }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } }