JavaMail简单客户端邮件发送

时间:2021-04-12 18:09:24

使用qq邮箱服务器,实现简单邮件发送功能。

一、设置qq邮箱账号

该账号作为邮件的发送账号。设置操作:进入到QQ邮箱,设置--账号,找到POP3/SMTP服务进行开启。

JavaMail简单客户端邮件发送

二、下载javamail.jar

下载地址:http://www.oracle.com/technetwork/java/javamail/index.html

三、编写代码

String host = "smtp.qq.com";
String from = "123456@qq.com";
String to = "654321@qq.com";//多个收信地址用逗号分隔
String subject = "邮件发送测试";

String user = "123456";
String password = "password";

String content = "<h2><font color=red>hello kitty 是hello kitty么?</font></h2>";


Properties props = new Properties();
props.put("mail.smtp.host", host); // 指定SMTP服务器
props.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证

try {

Session mailSession = Session.getDefaultInstance(props);

Message message = new MimeMessage(mailSession);

//设置发信人
InternetAddress isFrom = new InternetAddress(from);
isFrom.setPersonal("中国海军");
message.setFrom(isFrom);

//设置收信人
InternetAddress[] isTo = InternetAddress.parse(to);
message.setRecipients(Message.RecipientType.TO, isTo);

//邮件主题
message.setSubject(subject);

//设置邮件的内容格式为html显示
message.setContent(content, "text/html;charset=GBK");

message.saveChanges();

Transport transport = mailSession.getTransport("smtp");

transport.connect(host, user, password);

transport.sendMessage(message, message.getAllRecipients());

transport.close();

} catch(Exception e) {

System.out.println(e);

}

三、发送附件功能

public class MailTest 
{
public static void main(String[] args)
{
String host = "smtp.qq.com";
String from = "123456@qq.com";
String to = "654321@qq.com,987654@qq.com";
String subject = "邮件发送测试";

String user = "123456";
String password = "password";

String content = "<h2><font color=red>hello kitty 是hello kitty么?</font></h2>";

String attachmentFileName = "test211.txt";
File attachment = new File("C:\\Users\\pc\\Desktop\\mail\\test211.txt");

Properties props = new Properties();
props.put("mail.smtp.host", host); // 指定SMTP服务器
props.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证

try {

Session mailSession = Session.getDefaultInstance(props);

Message message = new MimeMessage(mailSession);

//设置发信人
InternetAddress isFrom = new InternetAddress(from);
isFrom.setPersonal("中国海军");
message.setFrom(isFrom);

//设置收信人
InternetAddress[] isTo = InternetAddress.parse(to);
message.setRecipients(Message.RecipientType.TO, isTo);

//邮件主题
message.setSubject(subject);

//设置邮件的内容格式为html显示
message.setContent(getContent(content, attachmentFileName, attachment), "text/html;charset=GBK");

message.saveChanges();

Transport transport = mailSession.getTransport("smtp");

transport.connect(host, user, password);

transport.sendMessage(message, message.getAllRecipients());

transport.close();

} catch(Exception e) {

System.out.println(e);

}
}

public static MimeMultipart getContent(String content, String attachmentFileName, File attachment) throws Exception {
MimeMultipart mp = new MimeMultipart();

BodyPart mdpText = new MimeBodyPart();

mdpText.setContent(content, "text/html;charset=GBK");

mp.addBodyPart(mdpText);

if (null != attachmentFileName && null != attachment) {
try {

String fileName = attachmentFileName;
fileName = new String(fileName.getBytes("GBK"), "ISO8859-1");
MimeBodyPart mbpAtt = new MimeBodyPart();
mbpAtt.setFileName(fileName);
mbpAtt.setDataHandler(new DataHandler(new FileDataSource(attachment)));

mp.addBodyPart(mbpAtt);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}

return mp;
}
}


四、发送图片

要在邮件内容中描述:图片放在哪里

要在邮件的数据中描述:图片在哪里

<h2><font color=red>hello kitty 是hello kitty么?</font></h2><br><img src='cid:1.jpg'></img>
MimeBodyPart image = new MimeBodyPart();          image.setDataHandler(new DataHandler(new FileDataSource("C:\\Users\\pc\\Desktop\\mail\\1.jpg")));        image.setContentID("1.jpg");        mp.addBodyPart(image);