import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
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;
/**
* @author Administrator
* @date Dec 31, 2011
*/
public class SendMail {
public SendMail() {
}
public static void main(String[] args) {
try {
Authenticator auth = new PopupAuthenticator();
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", "smtp.yeah.net");
mailProps.put("mail.smtp.auth", "true");
mailProps.put("username", "ruanqiangbeyond");
mailProps.put("password", "1050110085.");
Session mailSession = Session.getInstance(mailProps, auth);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("ruanqiangbeyond@yeah.net"));
//发送
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"ruanqiangbeyond@yeah.net"));
//抄送
message.setRecipient(Message.RecipientType.CC, new InternetAddress(
"490667462@qq.com"));
//密送
message.setRecipient(Message.RecipientType.BCC, new InternetAddress(
"490667462@qq.com"));
message.setSubject("Mail Test");
MimeMultipart multi = new MimeMultipart();
BodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setText("Hello World!");
multi.addBodyPart(textBodyPart);
message.setContent(multi);
// message.saveChanges();
Transport transport = mailSession.getTransport("smtp");
transport.connect("smtp.yeah.net", "ruanqiangbeyond@yeah.net", "1050110085.");
Transport.send(message);
transport.close();
} catch (Exception ex) {
System.err.println("邮件发送失败的原因是:" + ex.getMessage());
System.err.println("具体错误原因:");
ex.printStackTrace(System.err);
}
}
}
class PopupAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = "ruanqiangbeyond"; // 163邮箱登录帐号
String pwd = "1050110085."; // 登录密码
return new PasswordAuthentication(username, pwd);
}
}
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;
/**
* @author Administrator
* @date Jan 3, 2012
*/
public class Mail {
// 发送人地址
private String mailFrom = null;
// 收件人地址(如多人发送则用逗号隔开)
private String mailTo = null;
// 抄送人地址、密送人地址
private String mailccTo = null;
private String mailbccTo = null;
// SMTP主机地址
private String smtpHost = null;
// 是否采用调试方式
private boolean debug = false;
// 登陆SMTP服务器的用户名和密码
private String smtpUser = "ruanqiangbeyond@yeah.net";
private String smtpPassword = "1050110085.";
private String messageBasePath = null;
// Mail主题
private String subject;
// Mail内容
private String msgContent;
private Vector attachedFileList;
private String messageContentMimeType = "text/html; charset=gb2312";
public Mail() {
}
private void fillMail(MimeMessage msg) throws Exception {
String fileName = null;
if (mailFrom != null) {
msg.setFrom(new InternetAddress(mailFrom));
} else {
throw new Exception("Addresser was not maintained!");
}
if (mailTo != null) {
InternetAddress[] address = InternetAddress.parse(mailTo);
msg.setRecipients(Message.RecipientType.TO, address);
} else {
throw new Exception("Addresser was not maintained!");
}
if (mailccTo != null) {
InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);
msg.setRecipients(Message.RecipientType.CC, ccaddress);
}
if (mailbccTo != null) {
InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);
msg.setRecipients(Message.RecipientType.BCC, bccaddress);
}
// 加入主题
msg.setSubject(subject);
// 加入内容
Multipart mPart = new MimeMultipart();
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);
mBodyPart.setDataHandler(new DataHandler(fds));
mBodyPart.setFileName(fileName);
mPart.addBodyPart(mBodyPart);
}
}
msg.setContent(mPart);
}
/**
* 发送邮件
*
* @return true表成功,false为失败
* @throws Exception
*/
public boolean sendMail() {
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
MailAuthenticator auth = new MailAuthenticator();
MailAuthenticator.MAIL_USER = smtpUser;
MailAuthenticator.MAIL_PASSWORD = smtpPassword;
Session session = Session.getInstance(props, auth);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
Transport trans = null;
try {
fillMail(msg);
trans = session.getTransport("smtp");
try {
trans.connect(smtpHost, smtpUser, smtpPassword);
} catch (AuthenticationFailedException e) {
throw e;
} catch (MessagingException e) {
throw e;
}
Transport.send(msg);
return true;
} catch (MessagingException mex) {
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
}
return false;
} catch (Exception ex) {
return false;
} finally {
try {
if (trans != null && trans.isConnected())
trans.close();
} catch (Exception e) {
}
}
}
public void setAttachedFileList(java.util.Vector filelist) {
attachedFileList = filelist;
}
public void setDebug(boolean debugFlag) {
debug = debugFlag;
}
public void setMailAccount(String 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) {
}
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 void setSmtpPassword(String smtpPassword) {
this.smtpPassword = smtpPassword;
}
public void setSmtpUser(String smtpUser) {
this.smtpUser = smtpUser;
}
public static void main(String[] argv) throws Exception {
Mail sm = new Mail();
sm.setSMTPHost("smtp.yeah.net");
sm.setMailFrom("ruanqiangbeyond@yeah.net");
sm.setMailTo("775746615@qq.com,490667462@qq.com");
sm.setMailccTo("490667462@qq.com");
sm.setMsgContent("自动邮件内容");
sm.setSubject("标题");
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);
}
}