`
chenbj920
  • 浏览: 3616 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

java发送Email

 
阅读更多
import java.io.File;

import java.io.FileNotFoundException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;
import com.sun.mail.smtp.SMTPTransport;

public class EmailHelper {

private static Logger logger = Logger.getLogger(EmailHelper.class);

private MailInfo mailInfo;

public MailInfo getMailInfo() {
return mailInfo;
}

public void setMailInfo(MailInfo mailInfo) {
this.mailInfo = mailInfo;
}

public EmailHelper() {
}

public EmailHelper(MailInfo mailInfo) {
this.mailInfo = mailInfo;
}

public void sendMail(String[] receivers, String subject, String content)
throws MessagingException, FileNotFoundException {
sendMail(receivers, subject, content, new String[]{});
}

public void sendMail(String[] receivers, String subject, String content,
String[] attachmentFile) throws MessagingException,
FileNotFoundException {
File []file = new File[attachmentFile.length];
for (int i = 0; i < attachmentFile.length; i++) {
file[i] = new File(attachmentFile[i]);
}

sendMail(receivers, null, subject, content, file);
}

public void sendMail(String[] receivers, String[] cc, String subject,
String content, File[] attachmentFile) throws MessagingException,
FileNotFoundException {

logger.debug(mailInfo);
logger.debug("send email.....");
if (mailInfo == null)
mailInfo = new MailInfo();

logger.debug(mailInfo.getFromAddress());

Properties props = mailInfo.getProperties();

Session session = null;
if (mailInfo.isAuth()) {
session = Session.getInstance(props, new SimpleAuthenticator(
mailInfo.getFromAddress(), mailInfo.getPassword()));
} else {
session = Session.getInstance(props);
}
session.setDebug(false);

Message msg = new MimeMessage(session);
msg.setSubject(subject);

msg.setFrom(new InternetAddress(mailInfo.getFromAddress()));

Address[] tos = null;
if (receivers != null) {
tos = new InternetAddress[receivers.length];
for (int i = 0; i < receivers.length; i++) {
tos[i] = new InternetAddress(receivers[i]);
}
}
msg.setRecipients(Message.RecipientType.TO, tos);

Address[] cce = null;
if (cc != null) {
cce = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++) {
cce[i] = new InternetAddress(cc[i]);
}
}
msg.setRecipients(Message.RecipientType.CC, cce);

// Set email content
Multipart mp = new MimeMultipart();

MimeBodyPart mbp = new MimeBodyPart();
if (mailInfo.isHtml()) {
mbp.setContent(content,
"text/html;charset=" + mailInfo.getCharset());
} else {
mbp.setText(content, mailInfo.getCharset());
}

mp.addBodyPart(mbp);

// Set attachments
if (attachmentFile != null && attachmentFile.length > 0) {
for (int i = 0; i < attachmentFile.length; i++) {
if (!attachmentFile[i].exists()) {
throw new FileNotFoundException("not found the document:"
+  attachmentFile[i].getName());
}
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(attachmentFile[i]) {
public String getContentType() {
return "application/octet-stream";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(attachmentFile[i].getName());
mp.addBodyPart(attachmentPart);
}
}

msg.setContent(mp);
msg.setSentDate(new java.util.Date());

Transport tr = null;
try {
if (mailInfo.isAuth()) {
tr = new SMTPTransport(session, new URLName(
mailInfo.getProtocol(), mailInfo.getHost(),
mailInfo.getPort(), null, mailInfo.getFromAddress(),
mailInfo.getPassword()));
tr.connect(mailInfo.getHost(), mailInfo.getPort(),
mailInfo.getFromAddress(), mailInfo.getPassword());
} else {
tr = session.getTransport(mailInfo.getProtocol());
tr.connect();
}
tr.sendMessage(msg, msg.getAllRecipients());

} finally {
if (tr != null)
tr.close();
}

logger.debug("send mail succeed...");

}
}




import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

class SimpleAuthenticator extends Authenticator {
private String user;
private String pwd;

public SimpleAuthenticator(String user, String pwd) {
this.user = user;
this.pwd = pwd;
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pwd);
}
}



import java.security.Security;

import java.util.Properties;

public class MailInfo {

private String protocol = "smtp";
private Integer port = 25;
private Integer timeOut = 30000;
private Integer connectionTimeOut = 60000;
private String charset = "utf-8";
private boolean auth = true;
private boolean isSSL = false;
private boolean isHtml = false;

private String host;
private String fromAddress;
private String password;

public MailInfo(){}
public MailInfo(String host,String fromAddress,String password){
this.host = host;
this.fromAddress = fromAddress;
this.password = password;
}

public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public Integer getTimeOut() {
return timeOut;
}
public void setTimeOut(Integer timeOut) {
this.timeOut = timeOut;
}
public Integer getConnectionTimeOut() {
return connectionTimeOut;
}
public void setConnectionTimeOut(Integer connectionTimeOut) {
this.connectionTimeOut = connectionTimeOut;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public boolean isAuth() {
return auth;
}
public void setAuth(boolean auth) {
this.auth = auth;
}
public boolean isSSL() {
return isSSL;
}
public void setSSL(boolean isSSL) {
this.isSSL = isSSL;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isHtml() {
return isHtml;
}
public void setHtml(boolean isHtml) {
this.isHtml = isHtml;
}

public Properties getProperties(){
Properties props = System.getProperties();
props.put("mail.transport.protocol", this.getProtocol());
props.put("mail.smtp.connectiontimeout", this.getConnectionTimeOut());
props.put("mail.smtp.timeout", this.getTimeOut());
props.put("mail.mime.charset", this.getCharset());
props.put("mail.smtp.host", this.getPort());
props.put("mail.smtp.auth",this.isAuth());
if (this.isSSL()) {
//props.put("mail.smtp.starttls.enable", true);
//props.put("mail.smtp.socketFactory.fallback", false);
//props.put("mail.smtp.socketFactory.port",this.getPort());
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory", "false");
props.setProperty("mail.smtp.socketFactory.port", "465");

}

return props;
}
}
分享到:
评论

相关推荐

    java发送email超级大封装

    我发现网络中的关于java发送email的文章很多没有给全包,我下了三次才下全,现在我整理全了发给大家。所以我的项目所用的包都很常见。如果你在拷包时,发现报告重复,那么就不要拷了,说明你的系统中有此包。

    Java发送email:spring email、微软ews

    本篇文章将深入探讨如何使用Java结合Spring框架和微软的Exchange Web Services (EWS)来发送电子邮件。 首先,让我们从Spring Email说起。Spring Framework提供了`spring-context-support`模块,其中包含了一个Email...

    java发送email邮件.

    demo介绍:http://blog.csdn.net/love_xiolan/article/details/56016576 java发送email邮件 1.发送普通email邮件 2.发送带附件的email邮件 3.邮件抄送和密送 4.发送html邮件 5.发送模板文件

    java发送Email_群发email.rar

    这个名为"java发送Email_群发email.rar"的压缩包文件很可能包含了关于如何使用Java进行邮件发送,特别是群发邮件的相关教程或代码示例。 在Java中,发送电子邮件主要依赖于JavaMail API。这是一个强大的库,它提供...

    java 发送Email 需要的包

    总的来说,`activation-1.1.jar`、`mail-1.4.jar`和`commons-email-1.2.jar`这三个库是Java应用程序发送电子邮件的基础。它们提供了处理MIME类型、连接邮件服务器以及构建和发送邮件所需的所有功能,极大地简化了...

    使用java发送Email

    ### 使用Java发送Email 在Java开发中,电子邮件(Email)是一种常见的通信手段,尤其是在系统通知、用户注册验证等场景中非常实用。本文将详细介绍如何利用Java编写程序来发送Email。 #### 一、所需库及配置 为了...

    java发送email

    Java提供了多种库来实现这一功能,最常用的莫过于JavaMail API。本文将深入探讨如何使用Java发送电子邮件,包括基本原理、所需的组件、代码...通过阅读提供的"java发送Email文档",你可以获取更详细的指导和解决方案。

    [总结]Java发送Email

    本文将深入探讨如何使用Java发送Email,并基于给出的标签和压缩包文件来解析相关知识点。 首先,标题 "[总结]Java发送Email" 暗示我们将讨论Java中的邮件API以及其使用方法。在Java中,发送邮件主要依赖于JavaMail ...

    java发送Email邮件

    本文将详细讲解如何使用Java发送Email邮件,包括必要的库、步骤和关键概念。 首先,发送电子邮件需要用到JavaMail API,这是一个Java库,提供了一组接口和类来处理SMTP(简单邮件传输协议)和其他邮件协议。...

    通过Java发送Email ,简单入门。

    本篇文章将引导你入门如何使用Java发送Email,通过一个简单的实例来展示具体步骤。 首先,我们需要导入JavaMail API库,它提供了发送邮件所需的所有功能。在你的`pom.xml`(如果你使用的是Maven)或者`build.gradle...

    Java 发送 Email

    Java发送Email是一个常见的任务,尤其在企业级应用中,用于通知、验证或提供服务更新。在Struts2这个流行的Java Web框架中集成邮件发送功能,可以让应用程序自动化处理这些通信需求。下面将详细介绍如何在Struts2...

    java 发送email demo

    本文将详细讲解如何使用Java发送带有附件、抄送和密送功能的电子邮件,基于提供的"sendMail"文件名,我们可以推测这是一个包含示例代码的程序。 首先,Java Mail API是实现邮件发送功能的核心库。你需要引入以下...

    java发送email(HOT)本人测试过了, 带原码

    本人写的java发送Email类 可发三类Email: 1. 含有文本的 2. 含有HTML标签的 3. 带有附件的 如有问题Q我或Email QQ:13697654 Email:jiaoer840214@163.com ######### 一起学习,一起成长 ############

    java发送Email示例.rar

    Java发送电子邮件是一个常见的任务,尤其在自动化通知、系统间通信或者用户验证等场景下。本示例代码将向您展示如何使用Java实现这一功能。在Java中,我们主要依赖JavaMail API来处理邮件的发送。 JavaMail API是...

    java发送email依赖jar包

    以下是一个简单的Java发送邮件的示例,演示如何使用这三个jar包: ```java import org.apache.commons.mail.*; public class EmailSender { public static void main(String[] args) { SimpleEmail email = new ...

    java发送Email的成功例子

    NULL 博文链接:https://zhaoshijie.iteye.com/blog/804332

    java发送email举例

    Java发送电子邮件是一个常见的任务,尤其在自动化通知、订阅服务或者数据传输等方面。在这个例子中,我们看到一个关于如何使用Java发送电子邮件的实例。关键依赖是一个名为`mail-1.4.7.jar`的库,它是JavaMail API的...

    java 发送email 邮件

    本教程将深入探讨如何使用Java发送带有附件、支持多接收者且解决乱码问题的电子邮件。 首先,我们需要导入JavaMail API,这是一个用于处理电子邮件的开放源代码库。在Java项目中,可以通过以下Maven依赖引入: ```...

Global site tag (gtag.js) - Google Analytics