Java邮件发送

来源:互联网 发布:it outlet 编辑:程序博客网 时间:2024/06/05 19:47
Java邮件发送,下面的是简单的邮件发送代码,邮件服务器配置在mailServer.properties文件里面。通过简单的文本邮件发送方式发送邮件,暂未使用html格式的邮件发送方式。
javax.mail.jar下载地址:http://www.oracle.com/technetwork/java/javamail/index.html

注:常用发送邮件服务器名称及端口
163 smtp.163.com 25
126 smtp.126.com 25
qq smtp.qq.com 25 //注:若提示“发送邮件错误 454 Authentication failed, please open smtp flag first! ”,在QQ邮箱的设置里面,找到账户-》POP3/IMAP/SMTP选择开启POP3/SMTP服务
139 smtp.139.com或smtp.10086.cn 25 //注:454 Authentication failed

mailServer.properties属性文件代码如下:

mailServerHost=smtp.163.commailServerPort=25authValidate=trueuserName=test@163.comuserPassword= testtestfromAddress=test@163.comfromUserName=\u663E\u793A\u6211\u54C8\u54C8
上面几个参数是邮件服务器的配置文件,mailServerHost 为 发送邮件的服务器的IP,mailServerPort 为 发送邮件的服务器端口,userName 为  登陆邮件发送服务器的用户名,userPassword 为  登陆邮件发送服务器的密码,fromAddress 为 邮件发送者的地址,fromUserName 为  邮件发送者的名称即显示在他人邮件的发件人,authValidate 为  是否需要身份验证。

MailInfo.java代码如下,主要定义邮件基本信息:

package com.hsinghsu.test;import java.util.Properties;/** * 邮件信息类 * @author HsingHsu * */public class MailInfo {private String mailServerHost; // 发送邮件的服务器的IPprivate String mailServerPort = "25"; // 发送邮件的服务器端口private String userName; // 登陆邮件发送服务器的用户名private String userPassword; // 登陆邮件发送服务器的密码private String fromAddress; // 邮件发送者的地址private String toAddress; // 邮件接收者的地址private String fromUserName; // 邮件发送者的名称,显示在他人邮件的发件人private String mailSubject; // 邮件主题private String mailContent; // 邮件的文本内容private boolean authValidate = false; // 是否需要身份验证private Properties properties; // 邮件会话属性public String getMailServerHost() {return mailServerHost;}public void setMailServerHost(String mailServerHost) {this.mailServerHost = mailServerHost;}public String getMailServerPort() {return mailServerPort;}public void setMailServerPort(String mailServerPort) {this.mailServerPort = mailServerPort;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getUserPassword() {return userPassword;}public void setUserPassword(String userPassword) {this.userPassword = userPassword;}public String getFromAddress() {return fromAddress;}public void setFromAddress(String fromAddress) {this.fromAddress = fromAddress;}public String getToAddress() {return toAddress;}public void setToAddress(String toAddress) {this.toAddress = toAddress;}public String getMailSubject() {return mailSubject;}public void setMailSubject(String mailSubject) {this.mailSubject = mailSubject;}public String getMailContent() {return mailContent;}public void setMailContent(String mailContent) {this.mailContent = mailContent;}public String getFromUserName() {return fromUserName;}public void setFromUserName(String fromUserName) {this.fromUserName = fromUserName;}public boolean isAuthValidate() {return authValidate;}public void setAuthValidate(boolean authValidate) {this.authValidate = authValidate;}public Properties getProperties() {Properties p = new Properties();p.put("mail.smtp.host", this.mailServerHost);p.put("mail.smtp.port", this.mailServerPort);p.put("mail.smtp.auth", authValidate ? "true" : "false");return p;}public void setProperties(Properties properties) {this.properties = properties;}}

MailAuthenticator.java代码如下,主要是服务器用户的认证:

package com.hsinghsu.test;import javax.mail.Authenticator;import javax.mail.PasswordAuthentication;/** * 用来进行服务器对用户的认证 * @author HsingHsu * */public class MailAuthenticator extends Authenticator {String userName;String userPassword;public MailAuthenticator() {super();}public MailAuthenticator(String user, String pwd) {super();userName = user;userPassword = pwd;}public PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(userName, userPassword);}}
MailSender.java代码如下,主要用于邮件发送:
package com.hsinghsu.test;import java.io.UnsupportedEncodingException;import java.util.Date;import java.util.Properties;import javax.mail.Address;import javax.mail.Message;import javax.mail.MessagingException;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 HsingHsu * */public class MailSender {//发送邮件[邮件内容为文本格式]public boolean sendMail(MailInfo mailInfo) {// 判断是否需要身份认证MailAuthenticator authenticator = null;Properties pro = mailInfo.getProperties();if (mailInfo.isAuthValidate()) {// 如果需要身份认证,则创建一个密码验证器authenticator = new MailAuthenticator(mailInfo.getUserName(),mailInfo.getUserPassword());}// 根据邮件会话属性和密码验证器构造一个发送邮件的sessionSession sendMailSession = Session.getDefaultInstance(pro, authenticator);try {Message mailMessage = new MimeMessage(sendMailSession); // 根据session创建一个邮件消息Address from = new InternetAddress(mailInfo.getFromAddress(),mailInfo.getFromUserName()); // 创建邮件发送者地址Address to = new InternetAddress(mailInfo.getToAddress()); // 创建邮件的接收者地址,并设置到邮件消息中mailMessage.setFrom(from); // 设置邮件消息的发送者mailMessage.setRecipient(Message.RecipientType.TO, to);mailMessage.setSubject(mailInfo.getMailSubject()); // 设置邮件消息的主题mailMessage.setSentDate(new Date()); // 设置邮件消息发送的时间mailMessage.setText(mailInfo.getMailContent());// 设置邮件消息的主要内容Transport.send(mailMessage); // 发送邮件System.out.println("send ok!");return true;} catch (MessagingException ex) {ex.printStackTrace();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}return false;}    //邮件内容为html格式    public static boolean sendHtmlMail(MailInfo mailInfo) {// 判断是否需要身份认证MailAuthenticator authenticator = null;Properties pro = mailInfo.getProperties();// 如果需要身份认证,则创建一个密码验证器if (mailInfo.isAuthValidate()) {authenticator = new MailAuthenticator(mailInfo.getUserName(),mailInfo.getUserPassword());}// 根据邮件会话属性和密码验证器构造一个发送邮件的sessionSession sendMailSession = Session.getDefaultInstance(pro, authenticator);try {Message mailMessage = new MimeMessage(sendMailSession);// 根据session创建一个邮件消息Address from = new InternetAddress(mailInfo.getFromAddress());// 创建邮件发送者地址mailMessage.setFrom(from);// 设置邮件消息的发送者Address to = new InternetAddress(mailInfo.getToAddress());// 创建邮件的接收者地址,并设置到邮件消息中mailMessage.setRecipient(Message.RecipientType.TO, to);// Message.RecipientType.TO属性表示接收者的类型为TOmailMessage.setSubject(mailInfo.getMailSubject());// 设置邮件消息的主题mailMessage.setSentDate(new Date());// 设置邮件消息发送的时间Multipart mainPart = new MimeMultipart();// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象BodyPart html = new MimeBodyPart();// 创建一个包含HTML内容的MimeBodyParthtml.setContent(mailInfo.getMailContent(),"text/html; charset=utf-8");// 设置HTML内容mainPart.addBodyPart(html);mailMessage.setContent(mainPart);// 将MiniMultipart对象设置为邮件内容Transport.send(mailMessage);// 发送邮件return true;} catch (MessagingException ex) {ex.printStackTrace();}return false;}}
MailTest.java主要用于邮件发送测试:
package com.hsinghsu.test;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class MailTest {public static void main(String[] args) {//以下参数从properties文件读取String mailServerHost = null; // 发送邮件的服务器的IPString mailServerPort = null; // 发送邮件的服务器端口String userName = null; // 登陆邮件发送服务器的用户名String userPassword = null; // 登陆邮件发送服务器的密码String fromAddress = null; // 邮件发送者的地址String fromUserName = null; // 邮件发送者的名称,显示在他人邮件的发件人boolean authValidate = false; // 是否需要身份验证InputStream in = Object.class.getResourceAsStream("/mailServer.properties"); Properties prop = new Properties();try {prop.load(in);mailServerHost = prop.getProperty("mailServerHost").trim();mailServerPort = prop.getProperty("mailServerPort").trim();userName = prop.getProperty("userName").trim();userPassword = prop.getProperty("userPassword").trim();fromAddress = prop.getProperty("fromAddress").trim();fromUserName = prop.getProperty("fromUserName").trim();authValidate = prop.getProperty("authValidate").trim().equalsIgnoreCase("true");} catch (IOException e) {System.out.println("读取邮箱服务配置文件出错");e.printStackTrace();} MailInfo mailInfo = new MailInfo();mailInfo.setMailServerHost(mailServerHost);//"smtp.163.com"mailInfo.setMailServerPort(mailServerPort);//"25"mailInfo.setAuthValidate(authValidate);//truemailInfo.setUserName(userName);//"test@163.com"mailInfo.setUserPassword(userPassword);// "test"mailInfo.setFromAddress(fromAddress);//"test@163.com"mailInfo.setFromUserName(fromUserName);//"显示我哈哈"mailInfo.setToAddress("666666@qq.com");//666666@qq.commailInfo.setMailSubject("邮件标题是这个");mailInfo.setMailContent("邮件内容是这个");// 这个类主要来发送邮件MailSender sms = new MailSender();sms.sendMail(mailInfo);// 发送文体格式}}

原创粉丝点击