java mail邮件开发基本操作

来源:互联网 发布:新海诚风格知乎 编辑:程序博客网 时间:2024/06/06 09:09

利用java mail可以开发最基本的邮件发送与接收。
废话不多说了,直接上代码:

配置邮件的基本属性:
String protocol = “smtp”;
static String from=”*@sina.com”; //发送者地址
static String to=”*@qq.com”;//接收者地址
static String subject=”Html Test”;
static String body=”http://www.baidu.com>”+”欢迎访问百度”+” “+”“;//消息体

运行主类:
public static void main(String[] args) throws Exception {

String server = "smtp.sina.com";//新浪的smtp服务器,可以替换,例如qq:smtp.qq.com,但是qq需要第三方登陆码,163也一样    int port = 25;//smtp端口号    String user = "*******@sina.com";    String password = "*******";    HtmlMessageSender sender = new HtmlMessageSender();    Session session = sender.createSession();    MimeMessage message = new MimeMessage(session);//创建一个包括(htmlBodypart,imageBodyPart,attachBodyPart)的multipart对象,混合的    Multipart html_img_attach_multipart = new MimeMultipart("mixed");    html_img_attach_multipart.addBodyPart(sender.addHtmlAndImageMultipart(body, "C:\\Pictures\\Camera Roll\\girl.gif"));    html_img_attach_multipart.addBodyPart(sender.addAttachmentMultipart("H:\\data\\travel.xlsx"));    message.setContent(html_img_attach_multipart);    message.addFrom(new Address[]{new InternetAddress(from)});    message.setRecipient(RecipientType.TO, new InternetAddress(to));    message.setSubject(subject);    //获得Tranport对象,并连接到邮件服务器上发送邮件    Transport transport = session.getTransport();    transport.connect(server,port, user, password);transport.sendMessage(message,    message.getRecipients(RecipientType.TO));//保存对邮件操作    message.saveChanges();    transport.close();}

创建session对象:
private Session createSession(){
Properties props = new Properties();
props.setProperty(“mail.transport.protocol”, protocol);
/**
* 必须将mail.smtp.auth属性设置为true,SMTPTranport对象才会向
* SMTP服务器提交用户认证姿信息
*/
props.setProperty(“mail.smtp.auth”, “true”);
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
return session;
}

添加html代码与图片:
public MimeBodyPart addHtmlAndImageMultipart(String body,String imagePath) throws Exception{
//创建填充MimeBodyPart(包括html_img_Multipart)对象
MimeBodyPart html_img_bodypart = new MimeBodyPart();

//填充html_img_Multipart(包括htmlBodypart和imgBodypart)Multipart html_img_Multipart = new MimeMultipart("related");//创建htmlBodyPartMimeBodyPart htmlBodypart = new MimeBodyPart();htmlBodypart.setContent(body, "text/html;charset=utf-8");    html_img_Multipart.addBodyPart(htmlBodypart);//创建添加图片的MimeBodyPartMimeBodyPart imageBodypart = new MimeBodyPart();DataSource ds = new FileDataSource(new File(imagePath));    DataHandler dh = new DataHandler(ds);    imageBodypart.setDataHandler(dh);    imageBodypart.setContentID("beauty_girl_gif");    html_img_Multipart.addBodyPart(imageBodypart);    //填充MimeBodyPart(包括html_img_Multipart)    html_img_bodypart.setContent(html_img_Multipart);    return html_img_bodypart;}

邮件中添加附件
public MimeBodyPart addAttachmentMultipart(String attachPath) throws Exception{

    //创建attachBodyPart     MimeBodyPart attachBody = new MimeBodyPart();     FileDataSource dataSource = new FileDataSource(attachPath);     attachBody.setDataHandler(new DataHandler(dataSource));     attachBody.setFileName(dataSource.getName());     return attachBody;}

运行结果:

这里写图片描述

原创粉丝点击