准备工作
1、Java架包(javax.mail-1.5.0.jar)
2、搭建Java工程
3、对应图片创建文件目录
com.ding.test---测试类
com.ding.tools---邮件类
mail.properties---邮件配置类
开始代码
1、先看看要写哪些类
2、代码走起
Constants.java
package com.ding.tools;
import java.io.IOException;
import java.util.Properties;
public class Constants {
private static String filename = "mail.properties";
public static String mailHost = "";
public static String mailPort = "";
public static String userName = "";
public static String mailPwd = "";
public static String fromAddr = "";
public static String mailurl = "";
static {
Properties prop = new Properties();
try {
prop.load(Constants.class.getClassLoader().getResourceAsStream(filename));
mailHost = prop.getProperty("mail.mailHost").trim();
mailPort = prop.getProperty("mail.mailPort").trim();
userName = prop.getProperty("mail.userName").trim();
mailPwd = prop.getProperty("mail.pwd").trim();
fromAddr = prop.getProperty("mail.fromAddr").trim();
} catch (IOException e) {
e.printStackTrace();
}
}
}
MailAuthenticator.java
package com.ding.tools;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MailAuthenticator extends Authenticator {
String userName = null;
String password = null;
public MailAuthenticator() {
}
public MailAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.userName, this.password);
}
}
MailSend.java
package com.ding.tools;
import java.util.List;
public class MailSend {
public static boolean ExecuteMailSends(String mailSubject, String mailContent, List<String> emails) {
boolean flag = false;
String mailHost = Constants.mailHost;
String mailPort = Constants.mailPort;
String userName = Constants.userName;
String pwd = Constants.mailPwd;
String fromAddr = Constants.fromAddr;
if ((mailHost == null) || ("".equals(mailHost)) || (mailPort == null) || ("".equals(mailPort))
|| (userName == null) || ("".equals(userName)) || (pwd == null) || ("".equals(pwd))
|| (fromAddr == null) || ("".equals(fromAddr))) {
return flag;
}
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost(mailHost);
mailInfo.setMailServerPort(mailPort);
mailInfo.setValidate(true);
mailInfo.setUserName(userName);
mailInfo.setPassword(pwd);
mailInfo.setFromAddress(fromAddr);
mailInfo.setSubject(mailSubject);
mailInfo.setContent(mailContent);
flag = SimpleMailSenderImpl.sendHtmlMails(mailInfo, emails);
return flag;
}
public static boolean ExecuteMailSend(String mailSubject, String mailContent, String email) {
boolean flag = false;
String mailHost = Constants.mailHost;
String mailPort = Constants.mailPort;
String userName = Constants.userName;
String pwd = Constants.mailPwd;
String fromAddr = Constants.fromAddr;
if ((mailHost == null) || ("".equals(mailHost)) || (mailPort == null) || ("".equals(mailPort))
|| (userName == null) || ("".equals(userName)) || (pwd == null) || ("".equals(pwd))
|| (fromAddr == null) || ("".equals(fromAddr))) {
return flag;
}
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost(mailHost);
mailInfo.setMailServerPort(mailPort);
mailInfo.setValidate(true);
mailInfo.setUserName(userName);
mailInfo.setPassword(pwd);
mailInfo.setFromAddress(fromAddr);
mailInfo.setSubject(mailSubject);
mailInfo.setContent(mailContent);
mailInfo.setToAddress(email);
flag = SimpleMailSenderImpl.sendHtmlMail(mailInfo);
return flag;
}
private static String lpad(int length, int number) {
String f = "%0" + length + "d";
return String.format(f, new Object[] { Integer.valueOf(number) });
}
}
MailSenderInfo.java
package com.ding.tools;
import java.util.Properties;
public class MailSenderInfo {
private String mailServerHost;
private String mailServerPort;
private String fromAddress;
private String toAddress;
private String userName;
private String password;
private boolean validate = false;
private String subject;
private String content;
private String[] attachFileNames;
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", this.validate ? "true" : "false");
return p;
}
public String getMailServerHost() {
return this.mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return this.mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public boolean isValidate() {
return this.validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return this.attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return this.fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToAddress() {
return this.toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSubject() {
return this.subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return this.content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}
SimpleMailSenderImpl.java
package com.ding.tools;
import java.util.Date;
import java.util.List;
import java.util.Properties;
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;
public class SimpleMailSenderImpl {
public boolean sendTextMail(MailSenderInfo mailInfo) {
MailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
Message mailMessage = new MimeMessage(sendMailSession);
Address from = new InternetAddress(mailInfo.getFromAddress());
mailMessage.setFrom(from);
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
boolean flag = false;
MailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
Message mailMessage = new MimeMessage(sendMailSession);
Address from = new InternetAddress(mailInfo.getFromAddress());
mailMessage.setFrom(from);
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
Multipart mainPart = new MimeMultipart();
BodyPart html = new MimeBodyPart();
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
flag = false;
ex.getMessage();
ex.printStackTrace();
}
return flag;
}
public static boolean sendHtmlMails(MailSenderInfo mailInfo, List<String> recipients) {
boolean flag = false;
MailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
Message mailMessage = new MimeMessage(sendMailSession);
Address from = new InternetAddress(mailInfo.getFromAddress());
mailMessage.setFrom(from);
int num = recipients.size();
Address[] addss = new InternetAddress[num];
for (int i = 0; i < num; i++) {
addss[i] = new InternetAddress((String) recipients.get(i));
}
mailMessage.setRecipients(Message.RecipientType.TO, addss);
mailMessage.setSubject(mailInfo.getSubject());
mailMessage.setSentDate(new Date());
Multipart mainPart = new MimeMultipart();
BodyPart html = new MimeBodyPart();
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
flag = false;
ex.getMessage();
ex.printStackTrace();
}
return flag;
}
}
mail.properties邮件配置
##邮件发送服务器
mail.mailHost=smtp.sina.com
##邮件发送服务器端口号
mail.mailPort=25
##邮件用户名
mail.userName=newsisyphus
##密码
mail.pwd=youjianmima
##邮件的地址(用于发送邮件)
mail.fromAddr=newsisyphus@sina.com
注意:##
1、有些代理发送的,要到邮箱中设置开启代理发件服务
2、对应的端口号,地址,用户名,请参考网上配置
Test.java
package com.ding.test;
import java.util.ArrayList;
import java.util.List;
import com.ding.tools.MailSend;
public class Test {
public static void main(String[] args) {
String email = "shoujianren@sss.com";//收件邮箱
List<String> emails = new ArrayList();
emails.add(email);
MailSend tMail = new MailSend();
String mailinfo = "<h1>test</h1>test<p>test</p><a href=\"#\">test</a>";//邮件内容
String mailItem = "邮件主题";//邮件主题
boolean sendflag = tMail.ExecuteMailSends(mailItem , mailinfo, emails);//发送邮件
if (sendflag) {//判断是否成功
System.out.println("success!!");
}
}
}
tMail.ExecuteMailSends(mailItem , mailinfo, emails);可以配置多个邮件
tMail.ExecuteMailSend方法发送单个邮件
对于邮件发送还有什么问题,可以联系我qq:461874689
转载请注明出处,谢谢大家!