`
tanglizhi
  • 浏览: 9069 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java发送邮件

    博客分类:
  • java
阅读更多
package com.joyveb.export.action;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class JavaMail {
private String SMTPServer = new String();// 邮件服务器
private String user = new String();// 用户名
private String password = new String();// 密码
private String addresser = new String();// 发送者
private String addressTO = null;// 接收者
private String addressCC = null;// 抄送者
private String addressBCC = null;// 密秘抄送者
private String subject = new String();// 标题
private String content = new String();// 内容
private String filename = null; // 文件名

//一个附件时
private java.io.File fileAttachment = null;

// 当有多个附件时
private Vector<File> files = new Vector<File>();// 附件文件集合
private Vector<String> filenames = new Vector<String>();// 附件文件名

public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
}

// 往附件组合中添加附件
public void attachfile(File file, String filename) {
files.addElement(file);
filenames.addElement(filename);
}

public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
}

public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, String addressCC) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.addressCC = addressCC;
}

public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, java.io.File fileAttachment, String filename) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filename = filename;
this.fileAttachment = fileAttachment;
}

public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, String addressCC, java.io.File fileAttachment,
String filename) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filename = filename;
this.fileAttachment = fileAttachment;
this.addressCC = addressCC;
}
public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, Vector<File> files, Vector<String> filenames) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filenames = filenames;
this.files = files;
}

public JavaMail(String smtpServer, String user, String password,
String addresser, String subject, String content, String addressTO,
String addressBCC, String addressCC, Vector<File> files,
Vector<String> filenames) {
this.SMTPServer = smtpServer;
this.user = user;
this.password = password;
this.addresser = addresser;
this.subject = subject;
this.content = content;
this.addressTO = addressTO;
this.addressBCC = addressBCC;
this.filenames = filenames;
this.files = files;
this.addressCC = addressCC;
}


// 发送邮件
public void sendMail() {
try {
Properties props = new Properties();
Session sendMailSession;
Transport transport;

PopupAuthenticator popAuthenticator = new PopupAuthenticator();
PasswordAuthentication pop = popAuthenticator.performCheck(user,
password);

props.put("mail.smtp.host", SMTPServer);
props.put("mail.smtp.auth", "true");
sendMailSession = Session.getInstance(props, popAuthenticator);
sendMailSession.setDebug(false);

MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(addresser));
newMessage.setSubject(subject, "GBK");
newMessage.setSentDate(new java.util.Date());

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(content, "GBK");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
if (fileAttachment != null) {
messageBodyPart = new MimeBodyPart();
messageBodyPart.setFileName(filename);
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));

multipart.addBodyPart(messageBodyPart);
}
// 添加多个附件
if (files.size() != 0) {
Enumeration efile = files.elements();
Enumeration names = filenames.elements();
while (efile.hasMoreElements() && names.hasMoreElements()) {
MimeBodyPart mbpFile = new MimeBodyPart();
String filestr = efile.nextElement().toString();
String namestr = names.nextElement().toString();
System.out.println(filestr);
System.out.println(namestr);
FileDataSource fds = new FileDataSource(filestr);
mbpFile.setDataHandler(new DataHandler(fds));
mbpFile.setFileName(namestr);
// 向MimeMessage添加(Multipart代表附件)
multipart.addBodyPart(mbpFile);

}
files.removeAllElements();
filenames.removeAllElements();
}

newMessage.setContent(multipart);

if (addressTO != null) {
newMessage.setRecipients(RecipientType.TO,
InternetAddress.parse(addressTO));
}
if (addressCC != null) {
newMessage.setRecipients(RecipientType.CC,
InternetAddress.parse(addressCC));
}
if (addressBCC != null) {
newMessage.setRecipients(RecipientType.BCC,
InternetAddress.parse(addressBCC));
}
transport = sendMailSession.getTransport("smtp");
transport.connect(SMTPServer, user, password);
transport.send(newMessage);
} catch (Exception e) {
e.printStackTrace();
}
}

public class PopupAuthenticator extends Authenticator {
String username = null;
String password = null;

public PopupAuthenticator() {
}

public PasswordAuthentication performCheck(String user, String pass) {
username = user;
password = pass;
return getPasswordAuthentication();
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}

public static void main(String[] args) throws UnsupportedEncodingException {
String smtpServer = "smtp.qq.com";
String user = "1025725439@qq.com";
String password = "**********";
String addresser = "1025725439@qq.com";
String subject = "你好,jone.";
String addressTO = "qwert1990817@126.com";
String addressBCC = "";
String addressCC = "";
String content = "Hello Greatjone!";
File file = new File("E:\\person.xls");
File file1 = new File("E:\\human.xls.xls");
Vector<File>files=new Vector<File>();
files.add(file);
files.add(file1);
Vector<String> filenames = new Vector<String>();

String filename  = new String("person.xls".getBytes("gbk"), "iso-8859-1");
String filename1 = new String("human.xls.xls".getBytes("gbk"), "iso-8859-1");
filenames.add(filename);
filenames.add(filename1);
JavaMail mail = new JavaMail(smtpServer, user, password, addresser,
subject, content, addressTO, addressBCC, addressCC, files,
filenames);
mail.sendMail();
System.out.println("发送成功");
}

}
分享到:
评论

相关推荐

    java发邮件 java发email

    Java发送电子邮件是一个常见的任务,尤其在自动化通知、报表发送或者用户验证等场景中。下面将详细解释如何使用Java来发送邮件,并提供相关的知识点。 首先,Java通过JavaMail API提供了发送邮件的功能。JavaMail ...

    java发送邮件工具

    java发送邮件工具类,用于java程序实现邮件的发送功能

    JAVA发送邮件实现,消息格式化

    本文将详细讲解如何使用Java发送邮件,并着重介绍消息格式化的概念及其重要性。 首先,发送邮件的核心类是JavaMail API提供的`javax.mail`包中的`Session`, `Message`, `Transport`等类。为了发送邮件,我们需要...

    javamail"java发送邮件所需要的包

    描述中提到的“java发送邮件所需要的包”即是指JavaMail API,它包含了一系列的类和接口,例如`javax.mail.Session`,它是邮件会话对象,用于配置邮件服务器的连接参数;`javax.mail.Transport`,用于发送邮件;`...

    java发送邮件功能

    Java发送邮件功能是Java开发中常见的一项任务,它允许开发者通过编程的方式实现电子邮件的发送,包括正文内容、附件以及抄送等高级功能。在Java中,我们主要利用JavaMail API来实现这一目标。 JavaMail API是一个...

    java发送邮件jar包2个

    java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个java发送邮件jar包2个...

    java发送邮件实例

    Java发送邮件是一个常见的任务,尤其在自动化通知、报表发送或者用户验证等场景中。下面将详细解释这个过程,包括SMTP(Simple Mail Transfer Protocol)的工作原理、JavaMail API的使用以及可能遇到的问题及其解决...

    java发送邮件的实例

    总的来说,Java发送邮件涉及到了网络通信、邮件协议(如SMTP)、JavaMail API的使用,以及可能的认证机制。理解这些知识点对于实现一个稳定的邮件发送功能至关重要。在实际操作时,确保正确配置邮件服务信息,并对...

    JAVA发送邮件示例代码

    本文将详细讲解如何使用Java发送邮件,包括所需的库、基本概念以及实际示例代码。 首先,JavaMail API是Java平台用于处理邮件服务的核心库。要发送邮件,你需要在项目中引入JavaMail的依赖。如果你使用的是Maven,...

    java实现邮件发送(三种发送方式都有)

    Java发送邮件是软件开发中常见的需求,特别是在自动化通知、用户验证和客户服务等方面。在这个主题下,我们将探讨三种Java实现邮件发送的方式,以及如何通过提供的代码和参考资料进行实践。 首先,让我们了解一下...

    java发送邮件所需jar包.zip

    Java发送邮件是一个常见的任务,尤其在自动化通知、报表发送或者用户验证等场景中。要实现这个功能,开发者通常需要引入一些特定的库,也就是我们所说的jar包。在"java发送邮件所需jar包.zip"这个压缩文件中,很可能...

    java发送邮件Demo

    本示例“java发送邮件Demo”旨在教你如何利用JavaMail API实现这个功能。JavaMail API是一个开源库,允许开发者通过SMTP(简单邮件传输协议)发送、接收和管理邮件。 首先,确保在项目中引入JavaMail API的相关依赖...

    java 发送邮件sdk

    JavaMail SDK是Java发送邮件的核心工具,通过配置Session,创建并设置Message,以及使用Transport发送邮件,开发者可以轻松地在Java应用中实现邮件功能。同时,JavaMail还提供了丰富的功能,如HTML邮件、附件、抄送...

    java发送邮件完整程序

    Java发送邮件是一个常见的任务,尤其在自动化通知、订阅服务或者数据传输中。在这个"java发送邮件完整程序"中,我们将会探讨如何使用Java语言来构建一个邮件发送功能,包括所需的库、步骤以及关键代码片段。 首先,...

    java发送邮件源码

    本示例主要关注如何使用Java发送邮件,并结合`Mail.jar`库来实现这一功能。`Mail.jar`通常包含了JavaMail API,这是一个用于处理邮件的开源库。 首先,我们需要在项目中引入JavaMail API的相关依赖。如果是在Maven...

    JAVA发送邮件实现

    在Java编程语言中,发送邮件是一项常见的任务,尤其在开发Web应用、自动化通知或系统集成时。...Java发送邮件的功能十分强大,可以根据需求进行各种定制。通过学习和实践,你可以轻松地在Java应用中实现邮件服务功能。

    java发送邮件jar

    Java发送邮件功能主要依赖于两个库,即javax.mail.jar和activation.jar。这两个JAR文件包含了JavaMail API,它是Java平台中用于发送和接收电子邮件的标准API。让我们深入了解一下这两个库及其在Java邮件处理中的作用...

    使用java发送邮件的方法

    使用java发送邮件的方法,直接可以拿来用,最近找了个例子,稍微整理了一下

Global site tag (gtag.js) - Google Analytics