Commit 51ea2333 by zhiwei

增加100M以下文件发送

parent 4b464581
<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.3-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 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.4-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 com.zhiwei.sendmail.bean.MailInfo;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
public class SimpleMailSender {
/**
* @Title: sendTextMail
* @Description: (发送文本样式的邮件)
* @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: (发送网页形式的邮件)
* @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(), mailInfo.getFromName());
// 设置邮件消息的发送者
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;
import com.zhiwei.sendmail.bean.MailInfo;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
public class SimpleMailSender {
/**
* @Title: sendTextMail
* @Description: (发送文本样式的邮件)
* @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: (发送网页形式的邮件)
* @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(), mailInfo.getFromName());
// 设置邮件消息的发送者
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);
}
}
// 发送邮件
mailMessage.saveChanges();
Transport.send(mailMessage, mailMessage.getAllRecipients());
return true;
}
}
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) throws Exception {
// 这个类主要是设置邮件
String mailServerHost = "smtp.ym.163.com";
String mailServerPort = "25";
String fromAddress = "zhangzhiwei@zhiweidata.com";
String fromName = "测试";
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,fromName, toAddress, userName, password,
validate, subject, content, copyAddress, attachFileNames);
boolean f = false;
// 这个类主要来发送邮件
f = SendMail.sendMailByHtml(mailInfo);// 发送文体格式
System.out.println("f==========="+f);
}
}
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) throws Exception {
// 这个类主要是设置邮件
String mailServerHost = "smtphz.qiye.163.com";
String mailServerPort = "25";
String fromAddress = "zhangzhiwei@zhiweidata.com";
String fromName = "测试";
String toAddress = "yuchengyi@zhiweidata.com";
String userName = "zhangzhiwei@zhiweidata.com";
String password = "olp1437z..";
boolean validate = true;
String subject = "带附件的邮件测试程序";
String content = "发送邮件测试";
String copyAddress = "chenweiyang@zhiweidata.com";
List<String> attachFileNames = new ArrayList<String>();
attachFileNames.add("C:\\Users\\admin\\Desktop\\质检数据-大词-2020-04-13.xlsx");
MailInfo mailInfo = new MailInfo(mailServerHost, mailServerPort, fromAddress,fromName, 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