您的位置:首页 > 编程语言 > Java开发

java jmail

2015-12-16 00:00 525 查看
摘要: jmail

发送邮件的主要步骤

1.设置发送的协议,也就是设置smtp和验证机制(一般协议都是通过Properties键值形式来设置)

2.发送邮件需要的几个重要类Session ,Message,Transport

3.Session对象可以通过Session的getInstance(java.util.Properties props)

或getInstance(java.util.Properties props, Authenticator authenticator) Authenticator 可以理解为密码和用户名的验证器

或getDefaultInstance(java.util.Properties props)

或getDefaultInstance(java.util.Properties props, Authenticator authenticator)

4.邮件的一些重要内容都是通过Message设置,比例内容,主题,接收人..

接收人可以通过Message的setRecipients(Message.RecipientType type,Address address)

Message.RecipientType 有三个重要属性 1.BCC(密送),2.CC(抄送) ,3.TO(收件人)

5.可以通过Transport.send(Message msg)发送邮件

一般发送简单的邮件上面几个步骤就可以实现了,但是要发送一封复杂的邮件(有附件和文本,图片)还需要以下步骤

A.发送复杂邮件需要这个两个重要类MimeBodyPart 和 MimeMultipart

先要搞清楚文本和图片的关系是related,而文本和图片再加上附件的关系就是mixed

B.文本用一个MimeBodyPart对象保存

C.图片也是用一个MimeBodyPart对象保存

D.用一个MimeMultipart A对象来保存,保存文本的MimeBodyPart对象和保存图片的MimeBodyPart对象

这个MimeMultipart对象 通过构造函数 new MimeMultipart("related")获取

E.附件也是用一个MimeBodyPart C对象保存

F.用一个总MimeBodyPart B对象来保存MimeMultipart A对象

G.还得用一个总的MimeMultipart all对象来保存MimeBodyPart C对象和MimeBodyPart B对象

这个all对象通过构造函数 new MimeMultipart("mixed")获取

H.最后用Message.setContent(Multipart mp)方法保存总的邮件

I.可以通过Transport.send(Message msg)发送邮件

现在发送一份简单的邮件,邮件内容只含有一些文字

Code:

package com.michael.email;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

// @author Michael.Wu

//JavaMail 发送邮件

public class JavaEmail {

public static void main(String[] args) throws AddressException, MessagingException {

Properties properties = new Properties();

properties.setProperty("mail.transport.protocol", "smtp");//发送邮件协议

properties.setProperty("mail.smtp.auth", "true");//需要验证

// properties.setProperty("mail.debug", "true");//设置debug模式 后台输出邮件发送的过程

Session session = Session.getInstance(properties);

session.setDebug(true);//debug模式

//邮件信息

Message messgae = new MimeMessage(session);

messgae.setFrom(new InternetAddress("whyao@sina.cn"));//设置发送人

messgae.setText("what's up man");//设置邮件内容

messgae.setSubject("哥们该吃饭了");//设置邮件主题

//发送邮件

Transport tran = session.getTransport();

// tran.connect("smtp.sohu.com", 25, "wuhuiyao@sohu.com", "xxxx");//连接到新浪邮箱服务器

tran.connect("smtp.sina.com", 25, "whyao@sina.cn", "xxxxxxx");//连接到新浪邮箱服务器

// tran.connect("smtp.qq.com", 25, "Michael8@qq.vip.com", "xxxx");//连接到QQ邮箱服务器

tran.sendMessage(messgae, new Address[]{ new InternetAddress("Michael8@qq.vip.com")});//设置邮件接收人

tran.close();

}

}

====================================================================================

把邮件的用户名和密码改为你们自己的,就可以发送邮件了。有一个地方得注意,设置发送人和和你当前 发送邮件用的账号要相同,不然会报异常或会收不到邮件。以前发送人和发送邮件的账号是可以不同的现 在就不行,可能是邮箱服务商不允许的原因吧。

接下来发送一份复杂的邮件(含有附件和图片,文本)

Code:

package com.michael.email;

import java.io.File;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.Message.RecipientType;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

//@author Michael.wu

// 发送复杂的邮件(文本内容,附件,图片)

public class JavaEmail3 {

public static void main(String[] args) throws MessagingException {

//发送邮件的协议

Properties properties = new Properties();

properties.setProperty("mail.smtp.auth","true");//设置验证机制

properties.setProperty("mail.transport.protocol","smtp");//发送邮件协议

properties.setProperty("mail.smtp.host","smtp.sina.com");//设置邮箱服务器地址

properties.setProperty("mail.smtp.port","25");

Session session = Session.getInstance(properties,new MyAuthenticator());

session.setDebug(true);

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress("whyao@sina.cn"));

message.setSubject("一封复杂的邮件");

message.setRecipients(RecipientType.TO,InternetAddress.parse("michael8@vip.qq.com"));//接收人

message.setRecipients(RecipientType.CC,InternetAddress.parse("1348800595@qq.com"));//抄送人

message.setRecipients(RecipientType.BCC,InternetAddress.parse("1348800595@qq.com"));//密送人

MimeBodyPart bodyPartAttch = createAttachMent("C:\\Users\\Administrator\\Desktop\\mail.jar");//附件

MimeBodyPart bodyPartContentAndPic = createContentAndPic("I just want to Fuck","C:\\Users\\Administrator\\Desktop\\0.jpg");//文本内容

MimeMultipart mimeMuti = new MimeMultipart("mixed");

mimeMuti.addBodyPart(bodyPartAttch);

mimeMuti.addBodyPart(bodyPartContentAndPic);

message.setContent(mimeMuti);

message.saveChanges();

//message.setContent("Michael", "text/html;charset=gbk");

Transport.send(message);

}

//创建附件

public static MimeBodyPart createAttachMent(String path) throws MessagingException{

MimeBodyPart mimeBodyPart = new MimeBodyPart();

FileDataSource dataSource = new FileDataSource( new File(path));

mimeBodyPart.setDataHandler(new DataHandler(dataSource));

mimeBodyPart.setFileName(dataSource.getName());

return mimeBodyPart;

}

//创建文本和图片

public static MimeBodyPart createContentAndPic(String content,String path) throws MessagingException{

MimeMultipart mimeMutiPart = new MimeMultipart("related");

//图片

MimeBodyPart picBodyPart = new MimeBodyPart();

FileDataSource fileDataSource = new FileDataSource( new File(path));

picBodyPart.setDataHandler(new DataHandler(fileDataSource));

picBodyPart.setFileName(fileDataSource.getName());

mimeMutiPart.addBodyPart(picBodyPart);

//文本

MimeBodyPart contentBodyPart = new MimeBodyPart();

contentBodyPart.setContent(content,"text/html;charset=gbk");

mimeMutiPart.addBodyPart(contentBodyPart);

//图片和文本结合

MimeBodyPart allBodyPart = new MimeBodyPart();

allBodyPart.setContent(mimeMutiPart);

return allBodyPart;

}

}

Code:

package com.michael.email;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

public class MyAuthenticator extends Authenticator {

private static final String userName = "whyao@sina.cn";

private static final String passWord = "xxxxxxx";

// * @author Michael.wu

//* 密码和用户的验证

public MyAuthenticator() {

super();

}

@Override

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(userName, passWord);

}

}

//注意上面通过Main方法发送出去的邮件,图片会以附件的形式显示。如果你想把图片当做背景图或者是想在文本中插一张图片,还要把上面的createContentAndPic方法改改。

code:

//创建文本和图片

public static MimeBodyPart createContentAndPic(String content,String path) throws MessagingException, UnsupportedEncodingException{

MimeMultipart mimeMutiPart = new MimeMultipart("related");

//图片

MimeBodyPart picBodyPart = new MimeBodyPart();

FileDataSource fileDataSource = new FileDataSource( new File(path));

picBodyPart.setDataHandler(new DataHandler(fileDataSource));

picBodyPart.setFileName(MimeUtility.encodeText("米克个人照"));//解决中文乱码问题

picBodyPart.setHeader("Content-Location", "http://www.michael.com/mike.jpg");

//http://www.michael.com/mike.jpg这个路径是后面文本图片的路径

//文本

MimeBodyPart contentBodyPart = new MimeBodyPart();

//img的src要和setHeader中设置的值一样

contentBodyPart.setContent(content+"<img src='http://www.michael.com/mike.jpg'></img>","text/html;charset=gbk");

mimeMutiPart.addBodyPart(contentBodyPart);

mimeMutiPart.addBodyPart(picBodyPart);

//图片和文本结合

MimeBodyPart allBodyPart = new MimeBodyPart();

allBodyPart.setContent(mimeMutiPart);

return allBodyPart;

}

//通过上面的程序发送出的邮件,在Foxmail中打开,图片会在文本中正常显示。而在QQ邮箱 新浪邮箱 打开这封邮件,图片会以附件的形式显示,在文本中显示不出来。这到底是什么原因,或是还要设置什么。我还没有弄明白。希望看到这篇博客的网友,知道是什么原因的,还得请你们也告诉我一声,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: