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

[置顶] JAVA实现发送邮件(图片和附件)

2016-09-06 17:10 639 查看

java实现发送邮件功能

已网易为例

首先注册网易账号–>网易注册

在邮箱设置里面把POP3开启,只有开启了才能使用程序来发送邮件了

POP3全名为“Post Office Protocol - Version 3”,即“邮局协议版本3”。是TCP/IP协议族中的一员,由RFC1939 定义。本协议主要用于支持使用客户端远程管理在服务器上的电子邮件。提供了SSL加密的POP3协议被称为POP3S。–百度百科



准备工作做好后开始写代码啦!

代码实现功能

1.普通邮件

**下面写的Password就是刚才设置的授权密码

代码

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
* 发送普通邮件
*
* @author Administrator
*
*/
public class Email {
public static void main(String[] args) throws Exception {
Properties props = new Properties();

// 开启debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.163.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");

Session session = Session.getInstance(props);

Message msg = new MimeMessage(session);
msg.setSubject("小张调试邮件");
StringBuilder builder = new StringBuilder();
builder.append("url = " + "http://www.baidu.com");
builder.append("\n欢迎您!注册易买网仅此一封呵呵");

msg.saveChanges();
msg.setText(builder.toString());
msg.setFrom(new InternetAddress("13155903710@163.com"));

Transport transport = session.getTransport();
transport.connect("smtp.163.com", "13155903710@163.com", "z1234567");

transport.sendMessage(msg, new Address[] { new InternetAddress(
"978108040@qq.com") });
transport.close();
}

public static MimeBodyPart createContent(String body, String fileName)
throws Exception {
// 用于保存最终正文部分
MimeBodyPart contentBody = new MimeBodyPart();
// 用于组合文本和图片,"related"型的MimeMultipart对象
MimeMultipart contentMulti = new MimeMultipart("related");

// 正文的文本部分
MimeBodyPart textBody = new MimeBodyPart();
textBody.setContent(body, "text/html;charset=gbk");
contentMulti.addBodyPart(textBody);

// 正文的图片部分
MimeBodyPart jpgBody = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileName);
jpgBody.setDataHandler(new DataHandler(fds));
jpgBody.setContentID("logo_jpg");
contentMulti.addBodyPart(jpgBody);

// 将上面"related"型的 MimeMultipart 对象作为邮件的正文
contentBody.setContent(contentMulti);
return contentBody;
}

}


效果图



2.带图片的邮件

代码

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
* 发送内嵌图片的邮件
*
* @author Administrator
*
*/
public class emailImage {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "978108040@qq.com";

// Sender's email ID needs to be mentioned
String from = "13155903710@163.com";
final String username = "13155903710@163.com";// change accordingly
final String password = "z1234567";//授权密码非邮箱登录密码

// Assuming you are sending email through relay.jangosmtp.net
String host = "relay.jangosmtp.net";

Properties props = new Properties();
// 开启debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.163.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

// Create a default MimeMessage object.
Message message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));

// Set Subject: header field
message.setSubject("Testing Subject");

// This mail has 2 part, the BODY and the embedded image
MimeMultipart multipart = new MimeMultipart("related");

// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><img src='cid:image'>";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);

// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource("D:\\java.png");

messageBodyPart.setDataHandler(new DataHandler(fds));

messageBodyPart.setHeader("Content-ID", "<image>");

// add image to the multipart
multipart.addBodyPart(messageBodyPart);

// put everything together
message.setContent(multipart);
// Send message
Transport.send(message);

System.out.println("Sent message successfully....");

} catch (MessagingException e) {
throw new RuntimeException(e);
}

}
}


效果图



3.带附件的邮件

代码

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
* 发送带附件的邮件
*
* @author Administrator
*
*/
public class emailZip {
public static void main(String[] args) {
// Recipient's email ID needs to be mentioned.
String to = "978108040@qq.com";

// Sender's email ID needs to be mentioned
String from = "13155903710@163.com";
final String username = "13155903710@163.com";// change accordingly
final String password = "z1234567";//授权密码非邮箱登录密码

// Assuming you are sending email through relay.jangosmtp.net
String host = "relay.jangosmtp.net";

Properties props = new Properties();
// 开启debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.163.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session);

// Set From: header field of the header.
message.setFrom(new InternetAddress(from));

// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));

// Set Subject: header field
message.setSubject("Testing Subject");

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Now set the actual message
messageBodyPart.setText("This is message body");

// Create a multipar message
Multipart multipart = new MimeMultipart();

// Set text message part
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "D://1.ZIP";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);

// Send the complete message parts
message.setContent(multipart);

// Send message
Transport.send(message);

System.out.println("Sent message successfully....");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}


效果图



源码链接–>java实现发送邮件 下载,这三个功能开发中基本上够用了

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: