JavaMail
来源:互联网 发布:识别图片文字的软件 编辑:程序博客网 时间:2024/05/22 02:23
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* @author chaosuper
* @file MailSenderInfo.java
* @date 2014年7月31日
*/
public class MailSenderInfo {
// 发送邮件的服务器的IP和端口
private String mailServerHost;
private String mailServerPort = "25";
// 邮件发送者的地址
private String fromAddress;
// 邮件接收者的地址
private List<String> toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
// 是否需要身份验证
private boolean validate = false;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content;
private String fileurl;
// 邮件附件的文件名
private String[] attachFileNames;
/**
* @return Properties
* @brief
*/
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");
return p;
}
/**
* @return String
* @brief
*/
public String getMailServerHost() {
return mailServerHost;
}
/**
* @param mailServerHost
* void
* @brief
*/
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
/**
* @return String
* @brief
*/
public String getMailServerPort() {
return mailServerPort;
}
/**
* @param mailServerPort
* void
* @brief
*/
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
/**
* @return boolean
* @brief
*/
public boolean isValidate() {
return validate;
}
/**
* @param validate
* void
* @brief
*/
public void setValidate(boolean validate) {
this.validate = validate;
}
/**
* @return String[]
* @brief
*/
public String[] getAttachFileNames() {
return attachFileNames;
}
/**
* @param fileNames
* void
* @brief
*/
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
/**
* @return String
* @brief
*/
public String getFromAddress() {
return fromAddress;
}
/**
* @param fromAddress
* void
* @brief
*/
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
/**
* @return String
* @brief
*/
public String getPassword() {
return password;
}
/**
* @param password
* void
* @brief
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return String
* @brief
*/
public List<String> getToAddress() {
return toAddress;
}
/**
* @param toAddress
* void
* @brief
*/
public void setToAddress(List<String> toAddress) {
this.toAddress = toAddress;
}
/**
* @return String
* @brief
*/
public String getUserName() {
return userName;
}
/**
* @param userName
* void
* @brief
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return String
* @brief
*/
public String getSubject() {
return subject;
}
/**
* @param subject
* void
* @brief
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* @return String
* @brief
*/
public String getContent() {
return content;
}
/**
* @param textContent
* void
* @brief
*/
public void setContent(String textContent) {
this.content = textContent;
}
public String getFileurl() {
return fileurl;
}
public void setFileurl(String fileurl) {
this.fileurl = fileurl;
}
}
package mail;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import javax.mail.*;
/**
* @author chaosuper
* @file MyAuthenticator.java
* @date 2014年7月31日
*/
public class MyAuthenticator extends Authenticator {
String userName = null;
String password = null;
/**
*
*/
public MyAuthenticator() {
}
/**
* @param username
* @param password
*/
public MyAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
package mail;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
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;
import javax.mail.internet.MimeUtility;
/**
* @author chaosuper
* @file SimpleMailSender.java
* @date 2014年7月31日
*/
public class SimpleMailSender {
/**
* 以文本格式发送邮件
*
* @param mailInfo
* 待发送的邮件的信息
*/
public boolean sendTextMail(MailSenderInfo 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);
// 创建邮件的接收者地址,并设置到邮件消息中
List<Address> list=getAddress(mailInfo.getToAddress());
Address[] data=new Address[list.size()];
for(int i=0;i<list.size();i++)
{
data[i]=list.get(i);
}
mailMessage.setRecipients(Message.RecipientType.TO, data);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
MimeMultipart msgMultipart = new MimeMultipart("mixed");
MimeBodyPart attch1 = createAttachMent(mailInfo.getAttachFileNames()[0]);
MimeBodyPart attch2=new MimeBodyPart();
attch2.setContent(mailContent, "text/html;charset=UTF-8");
msgMultipart.addBodyPart(attch1);
msgMultipart.addBodyPart(attch2);
mailMessage.setContent(msgMultipart);
// mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以HTML格式发送邮件
*
* @param mailInfo
* 待发送的邮件信息
*/
public boolean sendHtmlMail(MailSenderInfo 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);
// 创建邮件的接收者地址,并设置到邮件消息中
// Address to = new InternetAddress(mailInfo.getToAddress());
List<Address> list=getAddress(mailInfo.getToAddress());
Address[] data=new Address[list.size()];
for(int i=0;i<list.size();i++)
{
data[i]=list.get(i);
}
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipients(Message.RecipientType.TO, data);
// mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
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);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
}
return false;
}
public List<Address> getAddress(List<String> mess) {
List<Address> address = new ArrayList<Address>();
try {
for (int i = 0; i < mess.size(); i++) {
Address to = new InternetAddress(mess.get(i));
address.add(to);
}
} catch (Exception e) {
e.printStackTrace();
}
return address;
}
public MimeBodyPart createAttachMent(String path) throws MessagingException{
MimeBodyPart mimeBodyPart = new MimeBodyPart();
try {
FileDataSource dataSource = new FileDataSource( new File(path));
mimeBodyPart.setDataHandler(new DataHandler(dataSource));
mimeBodyPart.setFileName(MimeUtility.encodeText(dataSource.getName()));
} catch (UnsupportedEncodingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return mimeBodyPart;
}
public MimeBodyPart createContentAndPic(String content,String path) throws UnsupportedEncodingException, MessagingException
{
MimeMultipart mimeMutiPart=new MimeMultipart("related");
//图片
MimeBodyPart picBodyPart=new MimeBodyPart();
FileDataSource fileDataSource=new FileDataSource(new File(path));
picBodyPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));
mimeMutiPart.addBodyPart(picBodyPart);
//文本
MimeBodyPart contentBodyPart=new MimeBodyPart();
contentBodyPart.setContent(content,"text/html;charset=UTF-8");
mimeMutiPart.addBodyPart(contentBodyPart);
//图片和文本结合
MimeBodyPart allBodyPart=new MimeBodyPart();
allBodyPart.setContent(mimeMutiPart);
return allBodyPart;
}
}
- JavaMail
- JavaMail
- javaMail
- JavaMail
- javamail
- javamail
- javamail
- JAVAMAIL
- javamail
- JavaMail
- JAVAMail
- JavaMail
- javaMail
- javamail
- JavaMail
- javaMail
- javamail
- JavaMail
- 第11周项目6-回文素数
- core image
- 游戏玩家的学习日志
- php批量下载图片并加水印
- nyoj 一笔画问题
- JavaMail
- VC控件开发
- 外贸建站选择Zencart还是Magento?
- 为何new出的对象数组必须要用delete[]删除,而普通数组delete和delete[]都一样-------_CrtMemBlockHeader
- 【Android安全】Android开发环境搭建用于开发测试例程序
- 一个简单的string类实现
- Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 接收 TDS 预登录响应时发生 I/O 错误。
- Linux Basic Commands
- hbase rowKey设计