因为工作需要发送邮件。所以上网找了例子,整理了一下发表出来。
封装邮件信息的一个实体类。
- package com.base.myproject.mail;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
import java.util.Properties;
-
-
public class MailSenderInfo {
-
-
public static String TEXT = "text/plain;charset=gb2312";
-
public static String HTML = "text/html;charset=gb2312";
-
-
private String mailServerHost;
-
private String mailServerPort = "25";
-
-
private String fromAddress;
-
private String toAddress;
-
-
private String userName;
-
private String password;
-
-
private boolean validate = false;
-
-
private String subject;
-
private String content;
-
private List<Object> attaches = new ArrayList<Object>();
-
-
private String cc;
-
private String bc;
-
-
-
-
-
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", validate ? "true" : "false");
-
return p;
- }
-
public String getMailServerHost() {
-
return mailServerHost;
- }
-
public void setMailServerHost(String mailServerHost) {
-
this.mailServerHost = mailServerHost;
- }
-
public String getMailServerPort() {
-
return mailServerPort;
- }
-
public void setMailServerPort(String mailServerPort) {
-
this.mailServerPort = mailServerPort;
- }
-
public boolean isValidate() {
-
return validate;
- }
-
public void setValidate(boolean validate) {
-
this.validate = validate;
- }
-
public String getFromAddress() {
-
return fromAddress;
- }
-
public void setFromAddress(String fromAddress) {
-
this.fromAddress = fromAddress;
- }
-
public String getPassword() {
-
return password;
- }
-
public void setPassword(String password) {
-
this.password = password;
- }
-
public String getToAddress() {
-
return toAddress;
- }
-
public void setToAddress(String toAddress) {
-
this.toAddress = toAddress;
- }
-
public String getUserName() {
-
return userName;
- }
-
public void setUserName(String userName) {
-
this.userName = userName;
- }
-
public String getSubject() {
-
return subject;
- }
-
public void setSubject(String subject) {
-
this.subject = subject;
- }
-
public String getContent() {
-
return content;
- }
-
public void setContent(String textContent) {
-
this.content = textContent;
- }
-
public List<Object> getAttaches() {
-
return attaches;
- }
-
public void setAttaches(List<Object> attaches) {
-
this.attaches = attaches;
- }
-
public String getCc() {
-
return cc;
- }
-
public void setCc(String cc) {
-
this.cc = cc;
- }
-
public String getBc() {
-
return bc;
- }
-
public void setBc(String bc) {
-
this.bc = bc;
- }
-
-
- }
邮件用户名密码认证
- Java 代码复制内容到剪贴板
-
-
package com.base.myproject.mail;
-
-
import javax.mail.Authenticator;
-
import javax.mail.PasswordAuthentication;
-
-
public class MyAuthenticator extends Authenticator{
-
-
String userName = null;
-
String password = null;
-
-
public MyAuthenticator(){
-
- }
-
public MyAuthenticator(String username, String password) {
-
this.userName = username;
-
this.password = password;
- }
-
protected PasswordAuthentication getPasswordAuthentication(){
-
return new PasswordAuthentication(userName, password);
- }
- }
发送邮件类
- Java 代码复制内容到剪贴板
-
-
package com.base.myproject.mail;
-
-
import java.net.URL;
-
import java.util.Date;
-
import java.util.Properties;
-
-
import javax.activation.DataHandler;
-
import javax.activation.FileDataSource;
-
import javax.activation.URLDataSource;
-
import javax.mail.BodyPart;
-
import javax.mail.Message;
-
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;
-
import javax.mail.internet.MimeUtility;
-
-
public class SendEmail {
-
-
-
private BodyPart body;
-
private MailSenderInfo mailInfo;
-
-
public SendEmail(MailSenderInfo mailInfo){
-
this.mailInfo = mailInfo;
- }
-
-
-
-
-
public void setBody(String string, String contentType) {
-
try {
-
body = new MimeBodyPart();
-
DataHandler dh = new DataHandler(string, contentType);
- body.setDataHandler(dh);
-
} catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
public void setBodyAsText(String string) {
- setBody(string, MailSenderInfo.TEXT);
- }
-
-
-
public void setBodyAsHTML(String string) {
- setBody(string, MailSenderInfo.HTML);
- }
-
-
-
public void setBodyFromFile(String filename) {
-
try {
-
BodyPart mdp = new MimeBodyPart();
-
FileDataSource fds = new FileDataSource(filename);
-
DataHandler dh = new DataHandler(fds);
- mdp.setDataHandler(dh);
- mailInfo.getAttaches().add(mdp);
-
} catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
public void setBodyFromUrl(String url) {
-
try {
-
BodyPart mdp = new MimeBodyPart();
-
URLDataSource ur = new URLDataSource(new URL(url));
-
DataHandler dh = new DataHandler(ur);
- mdp.setDataHandler(dh);
- mailInfo.getAttaches().add(mdp);
-
} catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
-
public void addAttachFromString(String string, String showname) {
-
try {
-
BodyPart mdp = new MimeBodyPart();
-
DataHandler dh = new DataHandler(string, MailSenderInfo.TEXT);
-
mdp.setFileName(MimeUtility.encodeWord(showname, "gb2312", null));
- mdp.setDataHandler(dh);
- mailInfo.getAttaches().add(mdp);
-
} catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
-
-
public void addAttachFromFile(String filename, String showname) {
-
try {
-
BodyPart mdp = new MimeBodyPart();
-
FileDataSource fds = new FileDataSource(filename);
-
DataHandler dh = new DataHandler(fds);
-
mdp.setFileName(MimeUtility.encodeWord(showname, "gb2312", null));
- mdp.setDataHandler(dh);
- mailInfo.getAttaches().add(mdp);
-
} catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
-
public void addAttachFromUrl(String url, String showname) {
-
try {
-
BodyPart mdp = new MimeBodyPart();
-
URLDataSource ur = new URLDataSource(new URL(url));
-
DataHandler dh = new DataHandler(ur);
-
mdp.setFileName(MimeUtility.encodeWord(showname, "gb2312", null));
- mdp.setDataHandler(dh);
- mailInfo.getAttaches().add(mdp);
-
} catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
public void send() throws Exception {
-
try {
-
MyAuthenticator authenticator = null;
-
- Properties props = mailInfo.getProperties();
-
if (mailInfo.isValidate()) {
-
-
authenticator = new MyAuthenticator(mailInfo.getUserName(),
- mailInfo.getPassword());
- }
- Session session = Session.getInstance(props,authenticator);
-
-
MimeMessage msg = new MimeMessage(session);
-
msg.setSubject(mailInfo.getSubject());
-
msg.setSentDate(new Date());
-
-
if (mailInfo.getFromAddress() != null)
-
msg.addFrom(InternetAddress.parse(mailInfo.getFromAddress()));
-
else
-
throw new Exception("没有指定发件人");
-
if (mailInfo.getToAddress() != null)
-
msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(mailInfo.getToAddress()));
-
else
-
throw new Exception("没有指定收件人地址");
-
if (mailInfo.getCc() != null)
-
msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(mailInfo.getCc()));
-
if (mailInfo.getBc() != null)
-
msg.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(mailInfo.getBc()));
-
-
Multipart mm = new MimeMultipart();
-
if (body != null)
-
mm.addBodyPart(body);
-
if(mailInfo.getAttaches() != null){
-
for (int i = 0; i < mailInfo.getAttaches().size(); i++) {
- BodyPart part = (BodyPart) mailInfo.getAttaches().get(i);
- mm.addBodyPart(part);
- }
- }
-
msg.setContent(mm);
-
-
-
msg.saveChanges();
- Transport.send(msg);
-
System.out.println("发送成功");
-
} catch (Exception e) {
- e.printStackTrace();
-
throw new Exception("发送邮件失败:", e);
- }
- }
- }
测试类
- Java 代码复制内容到剪贴板
-
-
package com.base.myproject.mail;
-
-
public class SendEmailTest {
-
-
-
-
-
public static void main(String[] args) {
-
MailSenderInfo mailInfo = new MailSenderInfo();
-
mailInfo.setMailServerHost("smtp.qq.com");
-
mailInfo.setValidate(true);
-
mailInfo.setUserName("452655443@qq.com");
-
mailInfo.setPassword("********");
-
-
mailInfo.setFromAddress("452655443@qq.com");
-
mailInfo.setToAddress("1302758061@qq.com");
-
-
mailInfo.setSubject("Send Email TEST");
-
-
SendEmail sms = new SendEmail(mailInfo);
-
-
-
sms.setBodyAsHTML("邮件测试啊。");
-
-
try {
- sms.send();
-
} catch (Exception e) {
-
- e.printStackTrace();
- }
- }
-
- }
本文转自06peng 51CTO博客,原文链接:http://blog.51cto.com/06peng/962467,如需转载请自行联系原作者
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。