Commit c076984a by zhiwei

初次提交发送邮件功能

parents
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhiwei</groupId>
<artifactId>sendmail</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sendmail</name>
<description>发送邮件</description>
<developers>
<developer>
<id>Bewilder ZW</id>
<name>zhiwei zhang</name>
<email>zhangzhiwei@zhiweidata.com</email>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
<!-- 分发管理:管理distribution和supporting files -->
<distributionManagement>
<snapshotRepository>
<id>nexus-releases</id>
<name>User Porject Snapshot</name>
<url>http://192.168.0.30:8081/nexus/content/repositories/snapshots/</url>
<uniqueVersion>true</uniqueVersion>
</snapshotRepository>
<repository>
<id>nexus-releases</id>
<name>User Porject Release</name>
<url>http://192.168.0.30:8081/nexus/content/repositories/releases/</url>
</repository>
</distributionManagement>
</project>
\ No newline at end of file
package com.zhiwei.sendmail;
import javax.mail.*;
public class MyAuthenticator extends Authenticator {
String userName = null;// 用户名
String password = null;// 密码
/*
* 无参构造
*/
public MyAuthenticator() {
}
/*
* 代参构造
*/
public MyAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
/*
*
*/
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
package com.zhiwei.sendmail;
import com.zhiwei.sendmail.bean.MailInfo;
/**
* @ClassName: SendMail
* @Description: TODO(发送邮件)
* @author Bewilder Z
* @date 2017年3月17日 上午11:30:49
*/
public class SendMail {
/**
* @Title: sendMailByHtml
* @Description: TODO(发送网页样式的邮件,支持附件功能)
* @param @param
* mailInfo
* @param @return
* 设定文件
* @return boolean 返回类型
*/
public static boolean sendMailByHtml(MailInfo mailInfo) {
try {
return SimpleMailSender.sendHtmlMail(mailInfo);// 发送文体格式
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @Title: sendMailByText
* @Description: TODO(发送文本样式的文件,不支持附件功能)
* @param @param
* mailInfo
* @param @return
* 设定文件
* @return boolean 返回类型
*/
public static boolean sendMailByText(MailInfo mailInfo) {
try {
return SimpleMailSender.sendTextMail(mailInfo);// 发送文体格式
} catch (Exception e) {
return false;
}
}
}
package com.zhiwei.sendmail;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
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;
import javax.mail.internet.MimeUtility;
import com.zhiwei.sendmail.bean.MailInfo;
public class SimpleMailSender {
/**
* @Title: sendTextMail
* @Description: TODO(发送文本样式的邮件)
* @param @param
* mailInfo
* @param @return
* 设定文件
* @return boolean 返回类型
* @throws MessagingException
*/
public static boolean sendTextMail(MailInfo mailInfo) throws MessagingException {
// 判断是否需要身份验证֤
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份验证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getInstance(pro, authenticator);
// 根据Session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址ߵ�ַ
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息发送的时间
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
}
/**
* @Title: sendHtmlMail
* @Description: TODO(发送网页形式的邮件)
* @param @param
* mailInfo
* @param @return
* 设定文件
* @return boolean 返回类型
* @throws MessagingException
* @throws UnsupportedEncodingException
*/
public static boolean sendHtmlMail(MailInfo mailInfo) throws UnsupportedEncodingException, MessagingException {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
// 如果需要身份认证,则创建一个密码验证器
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
// Message.RecipientType.TO属性表示接收者的类型为TO
InternetAddress[] to = InternetAddress.parse(mailInfo.getToAddress());
mailMessage.setRecipients(Message.RecipientType.TO, to);
// 抄送地址
if(mailInfo.getCopyAddress()!=null) {
InternetAddress[] cc = InternetAddress.parse(mailInfo.getCopyAddress());
mailMessage.setRecipients(Message.RecipientType.CC, cc);
}
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart multiPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart bodyPart = new MimeBodyPart();
// 设置HTML内容
bodyPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
multiPart.addBodyPart(bodyPart);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(multiPart);
if (null != mailInfo.getAttachFileNames() && mailInfo.getAttachFileNames().size() != 0) {
for (String attachFile : mailInfo.getAttachFileNames()) {
bodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachFile); // 得到数据源
bodyPart.setDataHandler(new DataHandler(fds)); // 得到附件本身并放入BodyPart
bodyPart.setFileName(MimeUtility.encodeText(fds.getName())); // 得到文件名并编码(防止中文文件名乱码)同样放入BodyPart
multiPart.addBodyPart(bodyPart);
}
}
// 发送邮件
Transport.send(mailMessage);
return true;
}
}
package com.zhiwei.sendmail.bean;
import java.io.Serializable;
import java.util.List;
import java.util.Properties;
/**
* @ClassName: MailSenderInfo
* @Description: TODO(发送邮件用户信息及邮件内容)
* @author Bewilder Z
* @date 2017年3月17日 上午11:24:18
*/
public class MailInfo implements Serializable {
private static final long serialVersionUID = -4456213215476182642L;
// 发送服务器的IP和端口
private String mailServerHost;
private String mailServerPort;
// 登录邮件发送服务器的用户名和密码
private String userName;
private String password;
// 邮件发送者地址
private String fromAddress;
// 邮件接受者的地址
private String toAddress;
// 是否需要身份验证
private boolean validate = false;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content;
//抄送邮件人信息,多人用逗号分隔
private String copyAddress;
// 邮件附件的文件名
private List<String> attachFileNames;
public MailInfo(String mailServerHost, String mailServerPort, String fromAddress, String toAddress,
String userName, String password, boolean validate, String subject, String content,
String copyAddress, List<String> attachFileNames) {
this.mailServerHost = mailServerHost;
this.mailServerPort = mailServerPort;
this.fromAddress = fromAddress;
this.toAddress = toAddress;
this.userName = userName;
this.password = password;
this.validate = validate;
this.subject = subject;
this.content = content;
this.copyAddress = copyAddress;
this.attachFileNames = attachFileNames;
}
/*
* 获得邮件会话属性
*
*/
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", validate ? "true" : "false");
return p;
}
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 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 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 isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
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 List<String> getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(List<String> attachFileNames) {
this.attachFileNames = attachFileNames;
}
public String getCopyAddress() {
return copyAddress;
}
public void setCopyAddress(String copyAddress) {
this.copyAddress = copyAddress;
}
}
package com.zhiwei.sendmail.test;
import java.util.ArrayList;
import java.util.List;
import com.zhiwei.sendmail.SendMail;
import com.zhiwei.sendmail.bean.MailInfo;
public class SendMailTest {
public static void main(String[] args) {
// 这个类主要是设置邮件
String mailServerHost = "smtp.ym.163.com";
String mailServerPort = "25";
String fromAddress = "zhangzhiwei@zhiweidata.com";
String toAddress = "859548429@qq.com";
String userName = "zhangzhiwei@zhiweidata.com";
String password = "olp1437z..";
boolean validate = true;
String subject = "带附件的邮件测试程序";
String content = "发送邮件测试";
String copyAddress = "825290417@qq.com,chenweiyang@zhiweidata.com";
List<String> attachFileNames = new ArrayList<String>();
attachFileNames.add("E://数据//2017年数据//9月份数据//南京组-美团日报数据//南京组-美团日报数据2017-09-28-微博.xlsx");
MailInfo mailInfo = new MailInfo(mailServerHost, mailServerPort, fromAddress, toAddress, userName, password,
validate, subject, content, copyAddress, attachFileNames);
boolean f = false;
// 这个类主要来发送邮件
f = SendMail.sendMailByHtml(mailInfo);// 发送文体格式
System.out.println("f==========="+f);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment