- 浏览: 72658 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
liyuanhoa:
SSH 和FreeMarker 动态网页生成静态技术 事例 -
xiao-qiang163:
做的项目,界面很花哨, “好看”但不中用。
做的项目的截图
package com.mail;
import java.util.Properties;
/**
* @desc 设置邮件基本信息
*/
public class MailInfo {
// 发送邮件的服务器的IP和端口[大多默认为25,gmail:465]
private String mailServerHost;
private String mailServerPort = "25";
//邮件发送者的地址
private String fromAddress;
//邮件接收者的地址[支持多用户]
private String []toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
//是否需要身份验证
private boolean validate = false;
//邮件主题⡡
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名[上传文件路径]
private String[] 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");
//p.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");//gmail需要
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 boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
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 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 getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}
package com.mail;
import java.util.Properties;
/**
* @desc 设置邮件基本信息
*/
public class MailInfo {
// 发送邮件的服务器的IP和端口[大多默认为25,gmail:465]
private String mailServerHost;
private String mailServerPort = "25";
//邮件发送者的地址
private String fromAddress;
//邮件接收者的地址[支持多用户]
private String []toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
//是否需要身份验证
private boolean validate = false;
//邮件主题⡡
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名[上传文件路径]
private String[] 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");
//p.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");//gmail需要
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 boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
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 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 getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}
2.邮件发送器
Java代码
package com.mail;
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;
/**
* 简单邮件(不带附件的邮件)发送器
*/
public class SendMail {
/**
* 以文本格式发送邮件
* @param mailInfo
* 待发送的邮件的信息
*/
public boolean sendTextMail(MailInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
//根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
//创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
//设置邮件消息的发送者
mailMessage.setFrom(from);
//创建邮件的接收者地址,并设置到邮件消息中
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
InternetAddress[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
//设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
//设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
//设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
//发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以HTML格式发送邮件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
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);
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
Address[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
//创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
//设置HTML内容
html.setContent(mailInfo.getContent(),"text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
//发送邮件
mailMessage.saveChanges();
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(mailInfo.getMailServerHost(),mailInfo.getUserName(),mailInfo.getPassword());
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
transport.close();
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 发送附件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendAttach(MailInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
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);
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
Address[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
//创建一个包含HTML内容的MimeBodyPart
BodyPart mbp = new MimeBodyPart();
//设置HTML内容
mbp.setContent(mailInfo.getContent(),"text/html; charset=utf-8");
mainPart.addBodyPart(mbp);
String attachFiles[]=mailInfo.getAttachFileNames();
for(String f:attachFiles){
mbp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(f); //得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart
mainPart.addBodyPart(mbp);
}
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
//发送邮件
//mailMessage.saveChanges();
//Transport transport = sendMailSession.getTransport("smtp");
//transport.connect(mailInfo.getMailServerHost(),mailInfo.getUserName(),mailInfo.getPassword());
//transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
//transport.close();
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
}
package com.mail;
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;
/**
* 简单邮件(不带附件的邮件)发送器
*/
public class SendMail {
/**
* 以文本格式发送邮件
* @param mailInfo
* 待发送的邮件的信息
*/
public boolean sendTextMail(MailInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
//根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
//创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
//设置邮件消息的发送者
mailMessage.setFrom(from);
//创建邮件的接收者地址,并设置到邮件消息中
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
InternetAddress[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
//设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
//设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
//设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
//发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以HTML格式发送邮件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
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);
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
Address[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
//创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
//设置HTML内容
html.setContent(mailInfo.getContent(),"text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
//发送邮件
mailMessage.saveChanges();
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(mailInfo.getMailServerHost(),mailInfo.getUserName(),mailInfo.getPassword());
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
transport.close();
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 发送附件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendAttach(MailInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
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);
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
Address[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
//创建一个包含HTML内容的MimeBodyPart
BodyPart mbp = new MimeBodyPart();
//设置HTML内容
mbp.setContent(mailInfo.getContent(),"text/html; charset=utf-8");
mainPart.addBodyPart(mbp);
String attachFiles[]=mailInfo.getAttachFileNames();
for(String f:attachFiles){
mbp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(f); //得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart
mainPart.addBodyPart(mbp);
}
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
//发送邮件
//mailMessage.saveChanges();
//Transport transport = sendMailSession.getTransport("smtp");
//transport.connect(mailInfo.getMailServerHost(),mailInfo.getUserName(),mailInfo.getPassword());
//transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
//transport.close();
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
}
3.身份认证
Java代码
package com.mail;
import javax.mail.*;
public class MyAuthenticator extends Authenticator {
private String username = "";
private String password = "";
public MyAuthenticator() {
}
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
package com.mail;
import javax.mail.*;
public class MyAuthenticator extends Authenticator {
private String username = "";
private String password = "";
public MyAuthenticator() {
}
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
4.测试使用
Java代码
package com.mail;
public class TestMail {
public static void main(String[] args) {
MailInfo mailinfo=new MailInfo();
//--设置邮件服务器开始
mailinfo.setMailServerHost("smtp.gmail.com");
mailinfo.setMailServerPort("465");
mailinfo.setValidate(true);
mailinfo.setUserName("zhaozhi3758");
mailinfo.setPassword("XXXXXXXX");
//--设置邮件服务器结束
mailinfo.setFromAddress("zhaozhi3758@sina.com");//邮件发送者的地址
//设置接受用户
String []ToAddress={"zhaozhi3758@163.com"};
mailinfo.setToAddress(ToAddress);
//设置附件
String []attach={"F:\\login.properties"};
mailinfo.setAttachFileNames(attach);
mailinfo.setSubject("-----邮件测试----");
mailinfo.setContent("<a href='#'>hiao的和</a>");//网页内容
SendMail sm=new SendMail();
if(sm.sendAttach(mailinfo))
System.out.println("邮件发送成功");
else
System.out.println("邮件发送失败");
}
}
声明:
转载自:http://zhaozhi3758.iteye.com/blog/584517
import java.util.Properties;
/**
* @desc 设置邮件基本信息
*/
public class MailInfo {
// 发送邮件的服务器的IP和端口[大多默认为25,gmail:465]
private String mailServerHost;
private String mailServerPort = "25";
//邮件发送者的地址
private String fromAddress;
//邮件接收者的地址[支持多用户]
private String []toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
//是否需要身份验证
private boolean validate = false;
//邮件主题⡡
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名[上传文件路径]
private String[] 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");
//p.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");//gmail需要
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 boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
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 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 getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}
package com.mail;
import java.util.Properties;
/**
* @desc 设置邮件基本信息
*/
public class MailInfo {
// 发送邮件的服务器的IP和端口[大多默认为25,gmail:465]
private String mailServerHost;
private String mailServerPort = "25";
//邮件发送者的地址
private String fromAddress;
//邮件接收者的地址[支持多用户]
private String []toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
//是否需要身份验证
private boolean validate = false;
//邮件主题⡡
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名[上传文件路径]
private String[] 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");
//p.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");//gmail需要
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 boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
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 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 getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}
2.邮件发送器
Java代码
package com.mail;
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;
/**
* 简单邮件(不带附件的邮件)发送器
*/
public class SendMail {
/**
* 以文本格式发送邮件
* @param mailInfo
* 待发送的邮件的信息
*/
public boolean sendTextMail(MailInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
//根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
//创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
//设置邮件消息的发送者
mailMessage.setFrom(from);
//创建邮件的接收者地址,并设置到邮件消息中
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
InternetAddress[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
//设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
//设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
//设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
//发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以HTML格式发送邮件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
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);
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
Address[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
//创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
//设置HTML内容
html.setContent(mailInfo.getContent(),"text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
//发送邮件
mailMessage.saveChanges();
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(mailInfo.getMailServerHost(),mailInfo.getUserName(),mailInfo.getPassword());
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
transport.close();
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 发送附件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendAttach(MailInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
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);
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
Address[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
//创建一个包含HTML内容的MimeBodyPart
BodyPart mbp = new MimeBodyPart();
//设置HTML内容
mbp.setContent(mailInfo.getContent(),"text/html; charset=utf-8");
mainPart.addBodyPart(mbp);
String attachFiles[]=mailInfo.getAttachFileNames();
for(String f:attachFiles){
mbp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(f); //得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart
mainPart.addBodyPart(mbp);
}
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
//发送邮件
//mailMessage.saveChanges();
//Transport transport = sendMailSession.getTransport("smtp");
//transport.connect(mailInfo.getMailServerHost(),mailInfo.getUserName(),mailInfo.getPassword());
//transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
//transport.close();
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
}
package com.mail;
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;
/**
* 简单邮件(不带附件的邮件)发送器
*/
public class SendMail {
/**
* 以文本格式发送邮件
* @param mailInfo
* 待发送的邮件的信息
*/
public boolean sendTextMail(MailInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
//根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
//创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
//设置邮件消息的发送者
mailMessage.setFrom(from);
//创建邮件的接收者地址,并设置到邮件消息中
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
InternetAddress[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
//设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
//设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
//设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
//发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以HTML格式发送邮件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
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);
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
Address[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
//创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
//设置HTML内容
html.setContent(mailInfo.getContent(),"text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
//发送邮件
mailMessage.saveChanges();
Transport transport = sendMailSession.getTransport("smtp");
transport.connect(mailInfo.getMailServerHost(),mailInfo.getUserName(),mailInfo.getPassword());
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
transport.close();
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 发送附件
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendAttach(MailInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
sendMailSession.setDebug(true);
try {
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);
String []toArray=mailInfo.getToAddress();
if(toArray.length==0)return false;
Address[] toAddrArray = new InternetAddress[toArray.length];
for (int i = 0; i < toArray.length; i++) {
toAddrArray[i] = new InternetAddress(toArray[i]);
}
mailMessage.setRecipients(Message.RecipientType.TO, toAddrArray);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
//创建一个包含HTML内容的MimeBodyPart
BodyPart mbp = new MimeBodyPart();
//设置HTML内容
mbp.setContent(mailInfo.getContent(),"text/html; charset=utf-8");
mainPart.addBodyPart(mbp);
String attachFiles[]=mailInfo.getAttachFileNames();
for(String f:attachFiles){
mbp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(f); //得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart
mainPart.addBodyPart(mbp);
}
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
//发送邮件
//mailMessage.saveChanges();
//Transport transport = sendMailSession.getTransport("smtp");
//transport.connect(mailInfo.getMailServerHost(),mailInfo.getUserName(),mailInfo.getPassword());
//transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
//transport.close();
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
}
3.身份认证
Java代码
package com.mail;
import javax.mail.*;
public class MyAuthenticator extends Authenticator {
private String username = "";
private String password = "";
public MyAuthenticator() {
}
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
package com.mail;
import javax.mail.*;
public class MyAuthenticator extends Authenticator {
private String username = "";
private String password = "";
public MyAuthenticator() {
}
public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
4.测试使用
Java代码
package com.mail;
public class TestMail {
public static void main(String[] args) {
MailInfo mailinfo=new MailInfo();
//--设置邮件服务器开始
mailinfo.setMailServerHost("smtp.gmail.com");
mailinfo.setMailServerPort("465");
mailinfo.setValidate(true);
mailinfo.setUserName("zhaozhi3758");
mailinfo.setPassword("XXXXXXXX");
//--设置邮件服务器结束
mailinfo.setFromAddress("zhaozhi3758@sina.com");//邮件发送者的地址
//设置接受用户
String []ToAddress={"zhaozhi3758@163.com"};
mailinfo.setToAddress(ToAddress);
//设置附件
String []attach={"F:\\login.properties"};
mailinfo.setAttachFileNames(attach);
mailinfo.setSubject("-----邮件测试----");
mailinfo.setContent("<a href='#'>hiao的和</a>");//网页内容
SendMail sm=new SendMail();
if(sm.sendAttach(mailinfo))
System.out.println("邮件发送成功");
else
System.out.println("邮件发送失败");
}
}
声明:
转载自:http://zhaozhi3758.iteye.com/blog/584517
发表评论
-
java读取text乱码
2011-06-16 09:01 1294Java读取TXT文本文件乱码 ... -
java swing 列别介绍
2011-06-15 12:43 753在IBM的论坛上看到一篇比较好的文章晒出来给大家看看 [url ... -
URL 中的#号的作用
2011-04-07 09:30 733本文转载于: http://news.cnblogs.com/ ... -
类的序列话 和反序列话
2011-03-30 11:40 754public static String SerializeT ... -
一种做输入的校验 同是可以验证不让粘贴复制
2011-03-29 17:28 829jQuery(function($){ $(" ... -
div中的字体溢出 保证制动换行 ie 火狐兼容
2011-03-25 12:04 1011在定义div是加如下样式,可能还会有好多的情况 这个就要视情况 ... -
div 的自动扩充 火狐 ie的兼容
2011-03-22 16:35 750好久没有发表文章了 今天在修改页面的时候用到了 div 的自动 ... -
Hibernate中的lazy的使用说明
2010-12-21 22:50 759好久没有看有关hibernate ... -
DWR+JSON实现的万能的N级联动
2010-12-19 19:35 896在我们的项目中用到了一个查询分类的地方要用到N级联动具体是多少 ... -
js 实现的div点击上下 进行滚动翻页的function
2010-12-11 22:58 1219... -
各种浏览器之间的兼容性问题
2010-12-06 13:24 576http://blog.csdn.net/vince6799/ ... -
Struts文件上传
2010-11-29 00:46 821今天在项目中遇到了一个非常恶心的问题,用到了struts2的文 ... -
在tomcat中直接配置加载workSpace中的项目
2010-11-27 12:48 1121在service.xml 文件中添加 一下配置即可,免去了从新 ... -
在项目中发短信的小例子
2010-11-21 15:32 670... -
java中日志的处理
2010-11-13 09:49 722在web.xml文件中配置 log4j <!-- 上下 ... -
java创建解析xml
2010-11-10 07:29 899这几天老是有人问如何用java 生成xml文件,以前自己也看到 ... -
java
2010-11-09 09:26 733public class EncryptUtil { pri ... -
JDBC的数据类型
2010-10-14 15:10 680数值型整型 JDBCtinyint ja ... -
Hibernate 处理Oracle的大对象Clob
2010-10-12 08:20 903Hibernate+Oracle+CLOB的读写其实只要这样做 ... -
Java读取Properties文件
2010-10-11 17:28 659使用J2SEAPI读取Properties文件的六种方法 1 ...
相关推荐
Java发送电子邮件是一个常见的任务,尤其在自动化通知、报表发送或者用户验证等场景中。下面将详细解释如何使用Java来发送邮件,并提供相关的知识点。 首先,Java通过JavaMail API提供了发送邮件的功能。JavaMail ...
java发送邮件工具类,用于java程序实现邮件的发送功能
本文将详细讲解如何使用Java发送邮件,并着重介绍消息格式化的概念及其重要性。 首先,发送邮件的核心类是JavaMail API提供的`javax.mail`包中的`Session`, `Message`, `Transport`等类。为了发送邮件,我们需要...
描述中提到的“java发送邮件所需要的包”即是指JavaMail API,它包含了一系列的类和接口,例如`javax.mail.Session`,它是邮件会话对象,用于配置邮件服务器的连接参数;`javax.mail.Transport`,用于发送邮件;`...
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发送邮件是一个常见的任务,尤其在自动化通知、报表发送或者用户验证等场景中。下面将详细解释这个过程,包括SMTP(Simple Mail Transfer Protocol)的工作原理、JavaMail API的使用以及可能遇到的问题及其解决...
总的来说,Java发送邮件涉及到了网络通信、邮件协议(如SMTP)、JavaMail API的使用,以及可能的认证机制。理解这些知识点对于实现一个稳定的邮件发送功能至关重要。在实际操作时,确保正确配置邮件服务信息,并对...
本文将详细讲解如何使用Java发送邮件,包括所需的库、基本概念以及实际示例代码。 首先,JavaMail API是Java平台用于处理邮件服务的核心库。要发送邮件,你需要在项目中引入JavaMail的依赖。如果你使用的是Maven,...
Java发送邮件是软件开发中常见的需求,特别是在自动化通知、用户验证和客户服务等方面。在这个主题下,我们将探讨三种Java实现邮件发送的方式,以及如何通过提供的代码和参考资料进行实践。 首先,让我们了解一下...
Java发送邮件是一个常见的任务,尤其在自动化通知、报表发送或者用户验证等场景中。要实现这个功能,开发者通常需要引入一些特定的库,也就是我们所说的jar包。在"java发送邮件所需jar包.zip"这个压缩文件中,很可能...
本示例“java发送邮件Demo”旨在教你如何利用JavaMail API实现这个功能。JavaMail API是一个开源库,允许开发者通过SMTP(简单邮件传输协议)发送、接收和管理邮件。 首先,确保在项目中引入JavaMail API的相关依赖...
JavaMail SDK是Java发送邮件的核心工具,通过配置Session,创建并设置Message,以及使用Transport发送邮件,开发者可以轻松地在Java应用中实现邮件功能。同时,JavaMail还提供了丰富的功能,如HTML邮件、附件、抄送...
Java发送邮件是一个常见的任务,尤其在自动化通知、订阅服务或者数据传输中。在这个"java发送邮件完整程序"中,我们将会探讨如何使用Java语言来构建一个邮件发送功能,包括所需的库、步骤以及关键代码片段。 首先,...
本示例主要关注如何使用Java发送邮件,并结合`Mail.jar`库来实现这一功能。`Mail.jar`通常包含了JavaMail API,这是一个用于处理邮件的开源库。 首先,我们需要在项目中引入JavaMail API的相关依赖。如果是在Maven...
在Java编程语言中,发送邮件是一项常见的任务,尤其在开发Web应用、自动化通知或系统集成时。...Java发送邮件的功能十分强大,可以根据需求进行各种定制。通过学习和实践,你可以轻松地在Java应用中实现邮件服务功能。
Java发送邮件功能主要依赖于两个库,即javax.mail.jar和activation.jar。这两个JAR文件包含了JavaMail API,它是Java平台中用于发送和接收电子邮件的标准API。让我们深入了解一下这两个库及其在Java邮件处理中的作用...
使用java发送邮件的方法,直接可以拿来用,最近找了个例子,稍微整理了一下