java发邮件新浪_JavaMail发送邮件

本文展示了如何使用JavaMail API发送包含文本内容、附件和图片的复杂邮件。通过设置SMTP属性,创建MimeMessage实例,并利用MimeBodyPart和MimeMultipart进行内容组织,实现了邮件的发送功能。示例代码中还包括了附件和图片的添加,以及自定义认证器MyAuthenticator用于身份验证。
摘要由CSDN通过智能技术生成

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值