java 邮件发送


<!-- 邮件发送 -->
<dependency >  
   <groupId >javax.mail </groupId >  
   <artifactId >mail </artifactId >  
   <version >1.4.5 </version >  
</dependency >  

<dependency >  
   <groupId >com.sun.mail </groupId >  
   <artifactId >javax.mail </artifactId >  
   <version >1.5.4 </version >  
</dependency > 








package com.dt.util;

 /**
 *@author dt
 *@date 2018年1月9日 下午2:43:08
 *@marker
 */
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;


import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class MailUtils {


/**
* 邮件服务器地址
*/
private String host = "smtp.163.com";
/**
* 发件人用户名
*/
private String userName = "ttqqmimibao@163.com";
/**
* 发件人密码
*/
private String password = "520395";
/**
* 发件人昵称
*/
private String nick = "";
/**
* 是否需要权限验证
*/
private boolean auth = true;

/**
* 是否需要代理
*/
private boolean isProxy = false;
/**
* 代理服务器地址
*/
private String socksProxyHost;
/**
* 代理服务器端口
*/
private String socksProxyPort;



private MimeMessage message;


/**
* 构造函数一(使用默认发送设置)
*/
public MailUtils() {

}
/**
* 构造函数二(指定发送设置)
* @param host
* @param userName
* @param password
*/
public MailUtils(String host, String userName, String password) {
this.host = host;
this.userName = userName;
this.password = password;
}
/**
* 构造函数三(指定发送设置)

* @param host
* @param userName
* @param password
* @param nick
* @param auth
*/
public MailUtils(String host, String userName, String password,
String nick, boolean auth) {
this.host = host;
this.userName = userName;
this.password = password;
this.nick = nick;
this.auth = auth;
}
/**
* 设置邮件代理服务器
* @param socksProxyHost
* @param socksProxyPort
*/
public void setProxy(String socksProxyHost, String socksProxyPort){
this.isProxy = true;
this.socksProxyHost = socksProxyHost;
this.socksProxyPort = socksProxyPort;
}
/**
* 发送邮件(构造方法一)
* @param to
* @param title
* @param content
* @return
*/
public boolean send(String[] to, String title, String content) {
return send(to, title, content, null);
}
/**
* 发送邮件(构造方法二)
* @param to
* @param title
* @param content
* @param filePath
* @return
*/
public boolean send(String[] to, String title, String content, String[] filePath) {
boolean result = false;


try {
// 邮件发送设置(邮件服务器、发件人)
setMailInfo();

// 设置邮件标题
message.setSubject(title);

// 设置邮件内容体
message.setContent(getContent(content,filePath));

// 设置接收者(多个接收者)
Address[] address = new Address[to.length];
for (int i = 0; i < to.length; i++) {
address[i] = new InternetAddress(to[i]);
}
/* 正常(Message.RecipientType.TO)
* 抄送(Message.RecipientType.CC)
* 密送(Message.RecipientType.BCC)
*/
message.addRecipients(Message.RecipientType.TO, address);


// 发送邮件
Transport.send(message);


result = true;
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}


return result;
}

/**
* 邮件发送设置(邮件服务器、发件人)

* @throws AddressException
* @throws MessagingException
*/
private void setMailInfo() throws AddressException, MessagingException {
Properties props = new Properties();
if(this.isProxy){
props.setProperty("proxySet", "true");
props.setProperty("socksProxyHost", this.socksProxyHost);
       props.setProperty("socksProxyPort", this.socksProxyPort);
}
        
// 邮件服务器地址
props.put("mail.smtp.host", this.host);
// 发件人账号
props.put("mail.user", this.userName);
// 发件人密码
props.put("mail.password", this.password);
// 是否需要身份验证
props.put("mail.smtp.auth", auth ? "true" : "false");


// 创建Session实例
Session session = null;
if (auth) {
// 构建授权信息,进行SMTP身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
session = Session.getDefaultInstance(props, authenticator);
} else {
session = Session.getDefaultInstance(props);
}


// 创建邮件消息
message = new MimeMessage(session);


// 设置发件人
if (nick != null && !nick.equals("")) { // 如果昵称不为空,则设置昵称
// 设置昵称
try {
nick = MimeUtility.encodeText(nick);
message.setFrom(new InternetAddress(nick + " <" + this.userName + ">"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
// 设置发件人
message.setFrom(new InternetAddress(this.userName));
}

// 设置发送日期
message.setSentDate(new Date());
}

/**
* 构造邮件内容体
* @param content
* @param filePath
* @return
* @throws MessagingException
*/
private MimeMultipart getContent(String content, String[] filePath) throws MessagingException{
// 构造邮件内容体
MimeMultipart mmp = new MimeMultipart();
// 设置邮件的内容体
BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent("<meta http-equiv=Content-Type content=text/html; charset=UTF-8>" + content, "text/html;charset=UTF-8");
mmp.addBodyPart(bodyPart);

// 添加多个附件
if (filePath != null) {
FileDataSource fileds;
File file;
for (int i = 0; i < filePath.length; i++) {
bodyPart = new MimeBodyPart();
file = new File(filePath[i]);
if(!file.exists()){
continue;
}
fileds = new FileDataSource(file);
// 添加附件
bodyPart.setDataHandler(new DataHandler(fileds));
// 设置附件名称
bodyPart.setFileName(fileds.getName());
// 把附件添加到 MimeMultipart
mmp.addBodyPart(bodyPart);
}
}

return mmp;
}

/**
* 基本的测试(邮件设置配置死的,实用过程中可以从配置文件中读取)
*/
public static void test1 (){
/******************| 发送设置 |*****************/
// 实例化邮件发送工具类(有三个构造函数)
MailUtils mu = new MailUtils();

/******************| 邮件内容设置 |*****************/
// 1、设置收件人,必填项(数组,支持过个收件人)
String[] to = { "296813405@qq.com","2756547641@qq.com" };
// 2、设置邮件标题,必填项
String title = "这是邮件标题";
// 3、设置邮件内容,必填项(支持HTML格式与普通文本)
String content = "这是邮件内容";
// 4、发送邮件
if (mu.send(to, title, content)) {
System.out.println("发送成功~");
} else {
System.out.println("发送失败~");
}
}

/**
* 基本的测试(邮件设置配置死的,实用过程中可以从配置文件中读取)
* 包含附件
*/
public static void test2 (){
/******************| 发送设置 |*****************/
// 实例化邮件发送工具类(有三个构造函数)
MailUtils mu = new MailUtils();

/******************| 邮件内容设置 |*****************/
// 1、设置收件人,必填项(数组,支持过个收件人)
String[] to = { "aa@163.com","bb@163.com"};
// 2、设置邮件标题,必填项
String title = "这是邮件标题";
// 3、设置邮件内容,必填项(支持HTML格式与普通文本)
String content = "这是邮件内容";
// 4、设置附件,选填项(数组,支持多附件)
String[] filePath = {"/Users/qmxl/1.jpg","/Users/qmxl/2.jpg"};
// 5、发送邮件
if (mu.send(to, title, content,filePath)) {
System.out.println("发送成功~");
} else {
System.out.println("发送失败~");
}
}

/**
* 完整的邮件发送设置方法
*/
public static void test3 (){
/******************| 发送设置 |*****************/
// 邮件服务器地址
String host = "smtp.163.com";
// 发件人用户名
String userName = "ab@163.com";
// 发件人密码
String password = "123456";
// 发件人昵称
String nick = "XXX共享信息平台";
// 是否需要权限验证
boolean auth = true;
// 实例化邮件发送工具类(有三个构造函数)
MailUtils mu = new MailUtils(host, userName, password, nick, auth);

/******************| 设置代理 |*****************/
// 代理服务器地址
String socksProxyHost = "192.168.168.168";
// 代理服务器端口
String socksProxyPort = "1080";
mu.setProxy(socksProxyHost, socksProxyPort);


/******************| 邮件内容设置 |*****************/
// 1、设置收件人,必填项(数组,支持过个收件人)
String[] to = { "aa@163.com","bb@163.com" };
// 2、设置邮件标题,必填项
String title = "这是邮件标题";
// 3、设置邮件内容,必填项(支持HTML格式与普通文本)
String content = "这是邮件内容";
// 4、设置附件,选填项(数组,支持多附件)
String[] filePath = {"/Users/qmxl/1.jpg","/Users/qmxl/2.jpg"};
// 5、发送邮件
if (mu.send(to, title, content,filePath)) {
System.out.println("发送成功~");
} else {
System.out.println("发送失败~");
}
}


public static void main(String[] args) {
MailUtils.test1();
//MailUtils.test2();
//MailUtils.test3();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java邮件发送工具是JavaMail API。可以使用JavaMail API来发送文本邮件、HTML邮件、带附件的邮件等。 在Java中,可以使用MimeMessage类和MimeMessageHelper类来创建并发送邮件。首先,需要添加依赖项javax.mail到项目的pom.xml文件中: ``` <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> ``` 然后,可以使用以下代码来创建和发送邮件: ```java import javax.mail.*; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessageHelper; public class EmailSender { public static void sendEmail(String to, String subject, String content, String from) { // 创建Session对象 Session session = Session.getDefaultInstance(new Properties()); try { // 创建MimeMessage对象 MimeMessage message = new MimeMessage(session); // 设置发件人 message.setFrom(new InternetAddress(from)); // 设置收件人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置主题 message.setSubject(subject); // 设置内容 message.setContent(content, "text/html"); // 发送邮件 Transport.send(message); System.out.println("邮件发送成功"); } catch (MessagingException e) { System.err.println("邮件发送失败"); } } } ``` 以上是一个简单的Java邮件发送工具类的示例。使用该工具类可以发送包含HTML代码的邮件。你可以调用`sendEmail`方法来发送邮件,其中`to`参数是收件人的邮箱地址,`subject`参数是邮件的主题,`content`参数是邮件的内容,`from`参数是发件人的邮箱地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值