javaMail普通邮件发送、带附件邮件、Html格式邮件

本文提供了一个使用Java发送带附件的电子邮件的示例代码。该示例展示了如何配置SMTP服务器,进行身份验证,并通过MIME消息格式来发送纯文本或HTML格式的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.hp.mail;


import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
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;
import javax.mail.internet.MimeUtility;

public class MailSender {
private static final String charset = "utf-8";
private static final String defaultMimetype = "text/plain";

/**
* 单个邮件发送
*
* @param receiver
* 收件人
* @param subject
* 标题
* @param mailContent
* 邮件内容
* @param mimetype
* 内容类型 默认为text/plain,如果要发送HTML内容,应设置为text/html
*/
public static void send(Properties properties, String receiver,
String subject, String mailContent, String mimetype) {
if (receiver != null && !receiver.equals("")) {
send(properties, new String[] { receiver }, subject, mailContent,
mimetype);
}

}

/**
* 批量邮件发送
*
* @param receivers
* 收件人
* @param subject
* 标题
* @param mailContent
* 邮件内容
* @param mimetype
* 内容类型 默认为text/plain,如果要发0送HTML内容,应设置为text/html
*/
public static void send(Properties properties, String[] receivers,
String subject, String mailContent, String mimetype) {
send(properties, receivers, subject, mailContent, null, mimetype);
}

/**
* 批量邮件发送,添加附件
*
* @param receivers
* 收件人
* @param subject
* 标题
* @param mailContent
* 邮件内容
* @param attachements
* 附件
* @param mimetype
* 内容类型 默认为text/plain,如果要发送HTML内容,应设置为text/html
*/
public static void send(final Properties properties, String[] receivers,
String subject, String mailContent, File[] attachements,
String mimetype) {
String smtpHost=(String) properties.get("email.hostname");
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);// smtp服务器地址
props.put("mail.smtp.auth", "true");// 需要校验
//针对gmail加密设置
if(smtpHost.indexOf("smtp.gmail.com")>=0){
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
}
Session session = Session.getDefaultInstance(props,
new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(properties
.getProperty("email.loginname"), properties
.getProperty("email.loginpwd"));// 登录用户名/密码
}
});
session.setDebug(true);
try {
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(properties
.getProperty("email.personal")));// 发件人邮件

InternetAddress[] toAddress = new InternetAddress[receivers.length];
for (int i = 0; i < receivers.length; i++) {
toAddress[i] = new InternetAddress(receivers[i]);
}
mimeMessage.setRecipients(Message.RecipientType.TO, toAddress);// 收件人邮件
mimeMessage.setSubject(subject, charset);

Multipart multipart = new MimeMultipart();
// 正文
MimeBodyPart body = new MimeBodyPart();
// body.setText(message, charset);不支持html
body.setContent(mailContent, (mimetype != null
&& !"".equals(mimetype) ? mimetype : defaultMimetype)
+ ";charset=" + charset);
multipart.addBodyPart(body);// 发件内容
// 附件
if (attachements != null) {
for (File attachement : attachements) {
MimeBodyPart attache = new MimeBodyPart();
attache.setDataHandler(new DataHandler(new FileDataSource(
attachement)));
String fileName = getLastName(attachement.getName());
attache.setFileName(MimeUtility.encodeText(fileName,
charset, null));
multipart.addBodyPart(attache);
}
}
mimeMessage.setContent(multipart);
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
}

private static String getLastName(String fileName) {
int pos = fileName.lastIndexOf("\\");
if (pos > -1) {
fileName = fileName.substring(pos + 1);
}
pos = fileName.lastIndexOf("/");
if (pos > -1) {
fileName = fileName.substring(pos + 1);
}
return fileName;
}

public static void main(String[] args) {
File[] file=new File[1];
file[0]=new File("c://1234-IaaS-Storage.xml");

Properties properties = new Properties();
properties.put("email.hostname", "smtp3.hp.com");
properties.put("email.loginname", "dong.xu2@hp.com");
properties.put("email.loginpwd", "fit.map.ate-666");
properties.put("email.personal", "dong.xu2@hp.com");
MailSender.send(properties, new String[]{"itadm@g4t0606g.houston.hp.com"}, "IaaS Data Upload", "Test",file,"text/html");

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值