-新建工具类
package test;
import java.util.Date;
import java.util.Properties;
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;
public class GServerMail
{
public static String MAIL_FROM = "123@163.com"; //发件人邮箱
public static String MAIL_SERVER = "smtp.163.com"; // SMTP服务器端口号(端口25)
public static String MAIL_PROTOCOL = "smtp";
public static String MAIL_PORT = "25"; //端口号
public static String MAIL_USER = "123@163.com"; // 发件人邮箱
public static String MAIL_PASSWORD = "******"; //邮箱密码
public static String DISPLAYNAME = "邮件名称";
public static boolean NEEDAUTH = true;
public GServerMail()
{
}
public static void setMailServer(String host,String port, String mailbox,String user,String passwd,String displayName,boolean needAuth)
{
MAIL_SERVER = host;
MAIL_PORT = port;
MAIL_FROM = mailbox;
MAIL_USER = user;
MAIL_PASSWORD = passwd;
DISPLAYNAME = displayName;
NEEDAUTH = needAuth;
}
public static boolean sendHtmlMsg(String to,String subject,String content)
{
try
{
Properties props = new Properties();
props.put("mail.smtp.host",MAIL_SERVER);
props.put("mail.smtp.port",MAIL_PORT);
props.put("mail.smtp.localhost", "localhost");
AuthenticatorImple auth = null;
if(NEEDAUTH)
{
props.put("mail.smtp.auth","true");
auth = new AuthenticatorImple(MAIL_USER,MAIL_PASSWORD);
}
else
{
props.put("mail.smtp.auth","false");
}
Session session = Session.getInstance(props,auth);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(MAIL_FROM,DISPLAYNAME));
msg.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
msg.setSubject("=?GB2312?B?"+Base64.encode(subject.getBytes("GB2312"))+"?=");
msg.setSentDate(new Date());
// MiniMultipart是消息容器
Multipart mainPart = new MimeMultipart();
//装载HTML内容
BodyPart html = new MimeBodyPart();
//html.setContent(content, "text/html; charset=GB2312");
html.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ content, "text/html;charset=GB2312");
mainPart.addBodyPart(html);
msg.setContent(mainPart);
Transport transport = session.getTransport("smtp");
transport.connect(MAIL_SERVER, MAIL_USER,MAIL_PASSWORD);
transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
System.out.println("send mail successed");
transport.close();
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
}
return false;
}
public static void main(String[] args)
{
//JMail mail = new JMail();
GServerMail.sendHtmlMsg("发送人", "主题", "邮件内容");
}
}
-依赖AuthenticatorImple类
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class AuthenticatorImple extends Authenticator
{
String userName=null;
String password=null;
public AuthenticatorImple()
{
}
public AuthenticatorImple(String username, String password)
{
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, password);
}
}