package com.onlinebookstore.util.mail;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
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;
}
public void sendEmail(String toMailAdr,String transport,String mailSubject,String htmlContent) {
try {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.auth", "true"); //授权
this.content=htmlContent;
Session ses = Session.getDefaultInstance(props, new MyAuthenticator(fromAddress,password));//构建与mail.host 的会话
InternetAddress from = new InternetAddress(fromAddress);//发送者邮件地址
InternetAddress to = new InternetAddress(toMailAdr); //接收者邮件地址
MimeMessage msg = new MimeMessage(ses); //创建信息
msg.setFrom(from); //信息由哪个地址发送
msg.addRecipient(Message.RecipientType.TO, to); //信息发送到哪个地址
msg.setSubject("html mail"); //信息主题
String content = createHtml(getContent()); //信息内容
msg.setContent(content,"text/html;charset=GBK");
Transport tran = ses.getTransport(transport); //获取传输类的实例
System.out.println("mailPassword: "+password);
tran.connect(fromAddress,password); //连接到发送者的邮箱
Transport.send(msg);
tran.close();
} catch (MessagingException e) {e.printStackTrace();}
}
public String createHtml(String body)
{
StringBuffer sb = new StringBuffer();
sb.append("<html>");
sb.append("<body>");
sb.append(body);
sb.append("</body>");
sb.append("</html>");
return sb.toString();
}
}
Java 发送邮件 中文不乱码
最新推荐文章于 2022-09-13 16:26:14 发布