`
daibalusu
  • 浏览: 343744 次
文章分类
社区版块
存档分类
最新评论

在java中发送Email自我总结

 
阅读更多

//首先是一个信息类

package com.util.mail;

/**
* 发送邮件需要使用的基本信息
*/
import java.util.Properties;

public class MailSenderInfo {
// 发送邮件的服务器的IP和端口
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 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", 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[] getAttachFileNames() {
return attachFileNames;
}

public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}

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;
}
}

//验证用户名,密码等信息的类

package com.util.mail;

import javax.mail.*;

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);
}
public static void main(String[] args){
//这个类主要是设置邮件
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost("smtp.126.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("wjmgood@126.com"); //发件人邮箱
mailInfo.setPassword("***********"); //发件人邮箱的密码
mailInfo.setFromAddress("wjmgood@126.com");
mailInfo.setToAddress("wjmgood@163.com");
mailInfo.setSubject("设置邮箱标题");
mailInfo.setContent("设置邮箱内容,%%%%%%%%%%%%%%%%");
//这个类主要来发送邮件
SimpleMailSender sms = new SimpleMailSender();
sms.sendTextMail(mailInfo);//发送文体格式
// sms.sendHtmlMail(mailInfo);//发送html格式
}
}

package com.util.mail;

import java.util.Date;
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 SimpleMailSender {
public boolean sendTextMail(MailSenderInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(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());
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}

public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(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());
BodyPart html = new MimeBodyPart();
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
mailMessage.setContent(mainPart);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics