package Com.Amt.Wy.Pub;
import javax.mail.Transport;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import javax.mail.NoSuchProviderException;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.BodyPart;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.Multipart;
import javax.activation.DataHandler;
import Com.Amt.Wy.Servers.PubServers;
import Com.Amt.Wy.PubAssis.SendMailAssistant;
import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;
import Com.Amt.Wy.PubInterface.MyLogInterface;
/**
* <p>Title: 爱默特</p>
*
* <p>Description: 爱默特工作中总结的接口和类</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: amt</p>
*
* @author 凌微丰
* @version 1.0
*/
public class SendMailExec {
private boolean exeValid = false;
/**
* 发送页面邮件
* @param senderUrl String
* @param userName String
* @param password String
* @param receiverURL String
* @param mailTitle String
* @param Mailcontent String
* @return boolean
*/
public boolean sendPageMail(String senderUrl, String userName,
String password, String receiverURL,
String mailTitle, String Mailcontent) throws
Exception {
exeValid = new SendMailAssistant().checkMailAddr(senderUrl, receiverURL);
if (exeValid == false) {
return exeValid;
}
String recUrl[] = receiverURL.split("@");
String endsWith = recUrl[1]; //得到收件人邮箱地址@后的部分,
Properties props = new Properties();
props.put("mail.smtp.host", "smtp." + endsWith); //设置收件人smtp
props.put("mail.smtp.auth", "true");
PopupAuthenticator popupAuthenticator =new PopupAuthenticator();
PasswordAuthentication pop = popupAuthenticator.performCheck(userName,password); //smtp用户验证
Session s = Session.getInstance(props,popupAuthenticator);
s.setDebug(true);
//给消息对象设置发件人/收件人/主题/发信时间
MimeMessage message = new MimeMessage(s);
InternetAddress from = null;
try {
from = new InternetAddress(senderUrl);
message.setFrom(from);
} catch (Exception ex) {
PubServers.getInstance().getMyLogInterface().myError(
"加载发件人邮箱地址信息失败," + ex);
return false;
}
InternetAddress to = null;
try {
to = new InternetAddress(receiverURL);
message.setRecipient(Message.RecipientType.TO, to);
} catch (Exception ex1) {
PubServers.getInstance().getMyLogInterface().myError(
"加载收件人邮箱地址信息失败," + ex1);
return false;
}
try {
message.setSubject(mailTitle);
} catch (MessagingException ex3) {
PubServers.getInstance().getMyLogInterface().myError("加载邮件标题出错。" +
ex3);
return false;
}
try {
message.setSentDate(new Date());
} catch (MessagingException ex2) {
PubServers.getInstance().getMyLogInterface().myError("加载邮件发送时间出错。" +
ex2);
return false;
}
Multipart mm = new MimeMultipart(); //新建一个MimeMultipart对象用来存放多个BodyPart对象
MimeBodyPart mbp1 = new MimeBodyPart();
try {
mbp1.setContent(Mailcontent, "text/html;charset=gb2312");
mm.addBodyPart(mbp1);
message.setContent(mm); //把mm作为消息对象的内容
} catch (Exception ex4) {
PubServers.getInstance().getMyLogInterface().myError("加载邮件内容时出错。" +
ex4);
return false;
}
try {
message.saveChanges();
} catch (MessagingException ex5) {
PubServers.getInstance().getMyLogInterface().myError(
"服务器保存加载邮件信息时出错。" + ex5);
return false;
}
Transport transport = null;
try {
transport = s.getTransport("smtp");
String smtpSenderURL = senderUrl.split("@")[1]; //得到发件人邮箱地址@后的部分,
transport.connect("smtp." + smtpSenderURL, userName, password);
} catch (Exception ex6) {
PubServers.getInstance().getMyLogInterface().myError("加载发件人信息时出错。" +
ex6);
return false;
}
try {
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException ex7) {
PubServers.getInstance().getMyLogInterface().myError(
"服务器在发送邮件过程中出错。" + ex7);
return false;
}
return exeValid;
}
/**
* 发送带附件的邮件,附件从磁盘上选择的文件(用本地上的文件作为附件)
* @param senderUrl String
* @param userName String
* @param password String
* @param receiverURL String
* @param mailTitle String
* @param Mailcontent String
* @param accessoriesURL String
* @param fileTitle String
* @return boolean
*/
public boolean sendFileAccessoriesMail(String senderUrl, String userName,
String password, String receiverURL,
String mailTitle, String Mailcontent,
String accessoriesURL,
String fileTitle) {
exeValid = new SendMailAssistant().checkMailAddr(senderUrl, receiverURL);
MyLogInterface logs = PubServers.getInstance().getMyLogInterface();
fileTitle = PubServers.getInstance().getTypeTransitionInterface().toIso(fileTitle);
//logs.myInfo("fileTitle2 is ::"+PubServers.getInstance().getTypeTransitionInterface().toIso(fileTitle));
if (exeValid == false) {
return exeValid;
}
String smtpReceiverURL = receiverURL.split("@")[1]; //得到收件人邮箱地址@后的部分,
Properties props = new Properties();
props.put("mail.smtp.host", "smtp." + smtpReceiverURL); //设置收件人smtp
props.put("mail.smtp.auth", "true");
Session s = Session.getInstance(props);
s.setDebug(true);
MimeMessage message = new MimeMessage(s);
//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from = null;
try {
from = new InternetAddress(senderUrl);
message.setFrom(from);
} catch (Exception ex) {
PubServers.getInstance().getMyLogInterface().myError(
"给收件人发送邮件时,设置发件人邮箱地址错误!!" + ex);
return false;
}
InternetAddress to = null;
try {
to = new InternetAddress(receiverURL);
message.setRecipient(Message.RecipientType.TO, to);
} catch (Exception ex1) {
PubServers.getInstance().getMyLogInterface().myError(
"给收件人发送邮件时,设置收件人邮箱地址错误!!" + ex1);
return false;
}
try {
message.setSubject(mailTitle);
} catch (MessagingException ex2) {
PubServers.getInstance().getMyLogInterface().myError(
"给收件人发送邮件时,设置邮件标题出错!!" + ex2);
return false;
}
try {
message.setSentDate(new Date());
} catch (MessagingException ex3) {
很强大的java收发邮件
4星 · 超过85%的资源 需积分: 10 193 浏览量
2011-07-27
10:23:06
上传
评论
收藏 693KB RAR 举报
专业托
- 粉丝: 0
- 资源: 50
最新资源
- PHP端通过modbus协议跟第三方设备进行数据通信
- navicat安装包亲测可用
- 算法部署-使用OpenVINO部署MobileStyleGAN轻量化高保真图像合成算法-项目源码-优质项目实战.zip
- 基于java实现远程采集华为逆变器使用modbus tcp协议进行通讯的设备数据
- Unity画面共享Spout插件
- 基于C++用modbus实现的工业设备的数据采集程序,支持Tcp、串口
- 完结12章AI Agent智能应用从0到1定制开发
- 15白落梅:你是锦瑟我为流年:三毛的万水千山-3491776.mobi
- Federated Learning-Aided Prognostics in the Shipping 4.0: Princi
- OFDM 的鲁棒频率和定时同步文献部分阅读笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈