📄 sendmail.java
字号:
package com.niis.myprice.net;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 org.apache.log4j.Logger;
import com.niis.myprice.util.sys.SysConfig;
public class SendMail {
private String mailTo = null;
private String mailFrom = null;
private String smtpHost = null;
private boolean debug = false;
private String messageBasePath = null;
private String subject;
private String msgContent;
private Vector attachedFileList;
private String mailAccount = null;
private String mailPass = null;
private String messageContentMimeType = "text/plain; charset=utf-8";
private String mailbccTo = null;
private String mailccTo = null;
private static Logger logger = Logger.getLogger(SendMail.class);
public SendMail() {
super();
}
private void fillMail(Session session, MimeMessage msg) throws IOException,
MessagingException {
String fileName = null;
Multipart mPart = new MimeMultipart();
if (mailFrom != null) {
msg.setFrom(new InternetAddress(mailFrom));
logger.debug("Mail from:" + mailFrom);
} else {
logger.debug("no mail from!");
return;
}
if (mailTo != null) {
InternetAddress[] address = InternetAddress.parse(mailTo);
msg.setRecipients(Message.RecipientType.TO, address);
logger.debug("Mail to:" + mailTo);
} else {
logger.debug("no mail to!");
return;
}
if (mailccTo != null) {
InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);
logger.debug("cc mail address:" + mailccTo);
msg.setRecipients(Message.RecipientType.CC, ccaddress);
}
if (mailbccTo != null) {
InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);
logger.debug("bcc mail address:" + mailbccTo);
msg.setRecipients(Message.RecipientType.BCC, bccaddress);
}
msg.setSubject(subject);
InternetAddress[] replyAddress = { new InternetAddress(mailFrom) };
msg.setReplyTo(replyAddress);
MimeBodyPart mBodyContent = new MimeBodyPart();
if (msgContent != null)
mBodyContent.setContent(msgContent, messageContentMimeType);
else
mBodyContent.setContent("", messageContentMimeType);
mPart.addBodyPart(mBodyContent);
if (attachedFileList != null) {
for (Enumeration fileList = attachedFileList.elements(); fileList
.hasMoreElements();) {
fileName = (String) fileList.nextElement();
MimeBodyPart mBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(messageBasePath
+ fileName);
logger.debug("mail Attach:" + messageBasePath + fileName);
mBodyPart.setDataHandler(new DataHandler(fds));
mBodyPart.setFileName(fileName);
mPart.addBodyPart(mBodyPart);
}
}
msg.setContent(mPart);
msg.setSentDate(new Date());
}
public void init() {
}
public int sendMail() throws IOException, MessagingException {
int loopCount;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
MailAuthenticator auth = new MailAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
Transport trans = null;
try {
fillMail(session, msg);
trans = session.getTransport("smtp");
try {
trans.connect(smtpHost, MailAuthenticator.MAIL_USER,
MailAuthenticator.MAIL_PASSWORD);// ,
} catch (AuthenticationFailedException e) {
e.printStackTrace();
logger.debug("coneect mail server error:");
return 3;
} catch (MessagingException e) {
logger.debug("coneect mail server error:");
return 3;
}
trans.send(msg);
trans.close();
} catch (MessagingException mex) {
logger.debug("send mail failure:");
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
logger.debug(ex.toString());
ex.printStackTrace();
}
return 3;
} finally {
try {
if (trans != null && trans.isConnected())
trans.close();
} catch (Exception e) {
logger.debug(e.toString());
}
}
logger.debug("send success!");
return 0;
}
public void setAttachedFileList(java.util.Vector filelist) {
attachedFileList = filelist;
}
public void setDebug(boolean debugFlag) {
debug = debugFlag;
}
public void setMailAccount(String strAccount) {
mailAccount = strAccount;
}
public void setMailbccTo(String bccto) {
mailbccTo = bccto;
}
public void setMailccTo(String ccto) {
mailccTo = ccto;
}
public void setMailFrom(String from){
mailFrom = from;
}
public void setMailPass(String strMailPass) {
mailPass = strMailPass;
}
public void setMailTo(String to){
mailTo = to;
}
public void setMessageBasePath(String basePath){
messageBasePath = basePath;
}
public void setMessageContentMimeType(String mimeType){
messageContentMimeType = mimeType;
}
public void setMsgContent(String content){
msgContent = content;
}
public void setSMTPHost(String host){
smtpHost = host;
}
public void setSubject(String sub){
subject = sub;
}
public boolean sendMailToServer(String content){
try{
MailAuthenticator.MAIL_USER = SysConfig.user_name;
MailAuthenticator.MAIL_PASSWORD = SysConfig.password;
SendMail sm = new SendMail();
sm.setSMTPHost(SysConfig.smtp_host);
sm.setMailFrom(SysConfig.mail_from);
sm.setMailTo(SysConfig.mail_to);
sm.setMsgContent(content);
logger.debug("content="+content);
sm.setSubject(new Date().toString());
sm.sendMail();
return true;
}catch(Exception e){
e.printStackTrace();
}
return false;
}
public static void main(String[] argv) throws Exception{
MailAuthenticator.MAIL_USER ="youmail@service.com.cn";
MailAuthenticator.MAIL_PASSWORD="yourpassword";
SendMail sm = new SendMail();
sm.setSMTPHost("pop.163.com");
sm.setMailFrom("yourmail@service.com");
sm.setMailTo("njtuslx@163.com");
sm.setMsgContent("content");
sm.setSubject("title");
sm.sendMail();
}
}
class MailAuthenticator extends Authenticator{
public static String MAIL_USER = "";
public static String MAIL_PASSWORD = "";
public MailAuthenticator(){
}
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(MAIL_USER,
MAIL_PASSWORD);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -