java mail邮件开发基本操作

利用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");

//创建htmlBodyPart
MimeBodyPart htmlBodypart = new MimeBodyPart();
htmlBodypart.setContent(body, "text/html;charset=utf-8");
    html_img_Multipart.addBodyPart(htmlBodypart);

//创建添加图片的MimeBodyPart
MimeBodyPart 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;
}

运行结果:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值