您的位置:首页 > 编程语言 > Java开发

公司内部邮箱服务器发送邮件 java版

2016-11-26 15:59 423 查看
背景:

由于需要给合作方以压缩包的形式每天返回数据,基于我们自己写的分布式程序,而月末通过返回的数据,来与合作方进行对账,所以每天数据返回的成败就至关重要了,但又懒得每天去查看日志,刚好借助一下公司内部的邮件服务器。

go、go、go

package test.util;

import org.apache.log4j.Logger;

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;

/**
* Created by shengjk1 on 2016/11/4.
* Blog Address:http://blog.csdn.net/jsjsjs1789
*/
public class SendMail {
private static Logger logger = Logger.getLogger(SendMail.class);
private static SendMail instance = null;

private SendMail() {

}

public static SendMail getInstance() {
if (instance == null) {
instance = new SendMail();
}
return instance;
}

public void send(String to[], String cs[], String ms[], String subject,
String content, String formEmail, String fileList[]) {
try {
Properties p = new Properties(); // Properties p =
// System.getProperties();
p.put("mail.smtp.auth", "true");
p.put("mail.transport.protocol", "smtp");
p.put("mail.smtp.ho
4000
st", "xxxxx");//邮件服务器的地址
p.put("mail.smtp.port", "25");
// 建立会话
Session session = Session.getInstance(p);
Message msg = new MimeMessage(session); // 建立信息
BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
msg.setFrom(new InternetAddress(formEmail)); // 发件人

String toList = null;
String toListcs = null;
String toListms = null;

// 发送,
if (to != null) {
toList = getMailList(to);
new InternetAddress();
InternetAddress[] iaToList = InternetAddress
.parse(toList);
msg.setRecipients(Message.RecipientType.TO, iaToList); // 收件人
}

// 抄送
if (cs != null) {
toListcs = getMailList(cs);
new InternetAddress();
InternetAddress[] iaToListcs = InternetAddress
.parse(toListcs);
msg.setRecipients(Message.RecipientType.CC, iaToListcs); // 抄送人
}

// 密送
if (ms != null) {
toListms = getMailList(ms);
new InternetAddress();
InternetAddress[] iaToListms = InternetAddress
.parse(toListms);
msg.setRecipients(Message.RecipientType.BCC, iaToListms); // 密送人
}
msg.setSentDate(new Date()); // 发送日期
msg.setSubject(subject); // 主题
msg.setText(content); // 内容
// 显示以html格式的文本内容
messageBodyPart.setContent(content, "text/html;charset=utf-8");
multipart.addBodyPart(messageBodyPart);

// 2.保存多个附件
if (fileList != null) {
addTach(fileList, multipart);
}

msg.setContent(multipart);
// 邮件服务器进行验证
Transport tran = session.getTransport("smtp");
tran.connect("xxx", //邮件服务器地址
"shengjk1@xxx.cn",//邮箱地址
"p@ssw0rd");//邮箱的密码
tran.sendMessage(msg, msg.getAllRecipients()); // 发送
System.out.println("邮件发送成功");

} catch (Exception e) {
logger.info("邮件发送时异常",e);
}
}

// 添加多个附件
public void addTach(String fileList[], Multipart multipart)
throws MessagingException, UnsupportedEncodingException {
for (int index = 0; index < fileList.length; index++) {
MimeBodyPart mailArchieve = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileList[index]);
mailArchieve.setDataHandler(new DataHandler(fds));
mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(),
"utf-8", "B"));
multipart.addBodyPart(mailArchieve);
}
}

private String getMailList(String[] mailArray) {

StringBuffer toList = new StringBuffer();
int length = mailArray.length;
if (mailArray != null && length < 2) {
toList.append(mailArray[0]);
} else {
for (int i = 0; i < length; i++) {
toList.append(mailArray[i]);
if (i != (length - 1)) {
toList.append(",");
}

}
}
return toList.toString();

}
public static void forSend(String subject,String content){
SendMail send = SendMail.getInstance();
String to[] = { "shengjk1@xxx.cn"};//收件人的地址
String cs[] = null;
String ms[] = null;

if(content==null||content.length()==0){
content = "这是邮件内容,仅仅是测试,不需要回复";
}
String fromEmail = "shengjk1@xxx.cn";//发件人的地址
String[] arrArchievList = null;

// 2.保存多个附件
send.send(to, cs, ms, subject, content, fromEmail, arrArchievList);
}

//  public static void main(String args[]) {
//      forSend(null);
//  }

}


pom.xml文件

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: