📄 mail.java
字号:
package com.trulytech.mantis.util;
import javax.mail.*;
import javax.mail.internet.*;
import com.trulytech.mantis.system.*;
import java.util.ArrayList;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
/**
* <p>Title: Mail</p>
* <p>Description: 邮件类</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author wangxian
* @version 1.2
*/
public class Mail {
private Session session = null; //邮件会话对象
private MimeMessage mimeMsg = null; //MIME邮件对象
private java.util.Properties props = null; //系统属性
private boolean needAuth = false; //smtp是否需要认证
private String username = ""; //smtp认证用户名和密码
private String password = "";
private String SMTPServer = ""; //SMTP Server地址
private String Subject = ""; //Subject
private String from = ""; //From
private String to = ""; //to
private String Msg = "";
private String contentType; //是text还是html,例如"text/html;charset=gb2312"
private ArrayList AttachFile; //附件绝对路径名
public Mail() {
needAuth = com.trulytech.mantis.system.Properties.SMTPAuth;
username = com.trulytech.mantis.system.Properties.SMTPUser;
password = com.trulytech.mantis.system.Properties.SMTPPwd;
SMTPServer = com.trulytech.mantis.system.Properties.SMTPServer;
this.contentType = "text";
this.AttachFile = new ArrayList();
}
/**
* 设置附件文件名(全路径)
* @param Filename String[] 文件名
*/
public void setAttachFile(String[] Filename) {
if (Filename != null) {
for (int i = 0; i < Filename.length; i++)
this.AttachFile.add(Filename[i]);
}
}
/**
* 设置邮件主题
* @param Subject 主题
*/
public void setSubject(String Subject) {
this.Subject = Subject;
}
/**
* 设置发件人
* @param From 发件人
*/
public void setFrom(String From) {
this.from = From;
}
/**
* 设置收件人
* @param To 收件人
*/
public void setTo(String To) {
this.to = To;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
/**
* 设置消息
* @param Msg 消息
*/
public void setMsg(String Msg) {
this.Msg = Msg;
}
/**
* 发送消息邮件
* @throws Exception
*/
public void sendMsgMail() throws Exception {
props = new java.util.Properties();
props.put("mail.smtp.host", SMTPServer);
if (needAuth) {
props.put("mail.smtp.auth", "true");
PopupAuthenticator Auth = new PopupAuthenticator();
Auth.performCheck(username, password);
session = Session.getInstance(props, Auth);
}
else
session = Session.getInstance(props, null);
mimeMsg = new MimeMessage(session);
mimeMsg.setSubject(Subject); //设置邮件主题
mimeMsg.setFrom(new InternetAddress(from)); //设置发信人
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
//设置附件
if (AttachFile.size() > 0) {
MimeBodyPart messageBodyPart =
new MimeBodyPart();
if (contentType.equalsIgnoreCase("text"))
messageBodyPart.setText(Msg);
else
messageBodyPart.setContent(Msg, contentType);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
int nSize=AttachFile.size();
for (int i = 0; i < nSize; i++) {
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource( (String) AttachFile.get(i));
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(new String(getFileName( (String) AttachFile.
get(i)).getBytes("GBK"), "ISO8859_1"));
multipart.addBodyPart(messageBodyPart);
}
mimeMsg.setContent(multipart);
}
//无附件
else {
if (contentType.equalsIgnoreCase("text"))
mimeMsg.setText(Msg);
else
mimeMsg.setContent(Msg, contentType);
}
Transport.send(mimeMsg);
//写日志
logWriter.Info("发送邮件到 '" + to + "'");
}
/**
*
* <p>Title: PopupAuthenticator</p>
* <p>Description: JavaMail认证类</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author WangXian
* @version 1.0
*/
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 String getContentType() {
return contentType;
}
private String getFileName(String FilePath) {
String FileName = "";
int i = FilePath.lastIndexOf(System.getProperty("file.separator"));
if (i > 0)
return FilePath.substring(i + 1, FilePath.length());
else
return FilePath;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -