下面是利用sun的javamail框架写的发送邮件代码:
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.URLDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailDemo {
public static void main(String[] args){
//设置服务器信息
String mailServerHost = "smtp.163.com";
String mailServerPort = "25";
boolean validate = true;
Properties pro = new Properties();
pro.put("mail.smtp.host", mailServerHost);
// pro.put("mai.smtp.port", mailServerPort);
pro.put("mail.smtp.auth", validate);
//设置邮箱用户名及密码
final String username = "xxxx@163.com";
final String password = "xxxx";
Session sendMailSession = Session.getDefaultInstance(pro, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(username, password);
}
});
sendMailSession.setDebug(true);
String from = "xxxx@163.com";
String to = "xxx@yy.com";
String subject = "subject";
String content = "<h1>hello</h1>";
try {
Message msg = new MimeMessage(sendMailSession);//创建一个邮件消息
msg.setFrom(new InternetAddress(from));//创建邮件发送者地址
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));//创建邮件接受者地址
msg.setSubject(subject);//设置主题
msg.setText(content + "1");//设置内容
msg.setSentDate(new Date());//设置发送日期
msg.setHeader("X-Mailer", "ligao");
//发送邮件
Transport.send(msg);
// Multipart mainPart = new MimeMultipart();
// BodyPart htmlPart = new MimeBodyPart();
// htmlPart.setContent(content+ "2", "text/html;charset=utf-8");
// mainPart.addBodyPart(htmlPart);
//
// BodyPart attachPart = new MimeBodyPart();
// ((MimeBodyPart) attachPart).attachFile(new File("user.json"));
// mainPart.addBodyPart(attachPart);
// BodyPart bp = new MimeBodyPart();
// URLDataSource source = new URLDataSource(new URL("http://www.sina.com"));
// bp.setFileName("nihao");
// DataHandler handler = new DataHandler(source);
// bp.setDataHandler(handler);
//
// mainPart.addBodyPart(bp);
//文件源
// BodyPart bp = new MimeBodyPart();
// FileDataSource source = new FileDataSource(new File("user.json"));
// DataHandler handler = new DataHandler(source);
// bp.setDataHandler(handler);
// mainPart.addBodyPart(bp);
// msg.setContent(mainPart);
//
// BodyPart bp = new MimeBodyPart();
// DataHandler handler = new DataHandler("hello", "text/plain;charset=gb2312");
// bp.setFileName(MimeUtility.encodeWord("你好", "gb2312", null));
// bp.setDataHandler(handler);
// mainPart.addBodyPart(bp);
// msg.setContent(mainPart);
// msg.setContent(mainPart);
// Transport.send(msg);
System.out.println("发送完成");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}