java代码如何发送一封带图片的邮件

  • Post author:
  • Post category:java


本章记录三个,1.java 发送普通的邮件  2.java发送带附件的邮件 3.java发送带图片的邮件

1.java发送普通邮件–首先要设置如下的几个具体的参数:

public static String username = "***@163.com"; // 服务邮箱(from邮箱)
public static String password = "*****"; // 邮箱密码
public static String senderNick = "***"; // 发件人昵称
private Properties props; // 系统属性----下面会讲一下具体的配置。
private Session session; // 邮件会话对象
private MimeMessage mimeMsg; // MIME邮件对象
private MimeMultipart mp; // Multipart对象,邮件内容,标题,附件,图片等内容均添加到其中后再生成MimeMessage对象

2.现在看一下系统属性Properties的属性应该如何配置:
 props = System.getProperties();
props.put("mail.smtp.auth", "true"); //mail.smtp.auth属性或提供SMTP传输与连接到SMTP服务器的用户名和密码--如果需要用到用户验证即为TRUE
props.put("mail.transport.protocol", "smtp");//协议
props.put("mail.smtp.host", "*****");//传输邮件协议的服务器
props.put("mail.smtp.port", "25");//邮件服务器的端口号
props.put("username", username);//发出邮箱地址
props.put("password", password);//发出邮箱密码
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "*****");

3.接下来是配置seesion
--对用户进行验证--然后将验证放入到session中,验证的方法如下
MyAuthenticator myauth = new MyAuthenticator(senderNick, password);
session = Session.getDefaultInstance(props, myauth);

4.用户验证的方法:---
class MyAuthenticator extends Authenticator {
String userName = "";
String password = "";

public MyAuthenticator() {
}

public MyAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
增加了3,4中的用户的验证,可以避免邮箱发送邮件,报Error:535 5.7.3 Authentication unsuccessful的问题。

5.接下来是填充邮件正文和图片方面:

mimeMsg = new MimeMessage(session);
mp = new MimeMultipart("related");

// 自定义发件人昵称
String nick = "";
try {
nick = javax.mail.internet.MimeUtility.encodeText(senderNick);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 设置发件人
mimeMsg.setFrom(new InternetAddress(from, nick));--from 发件人的地址,昵称
// 设置收件人
if (to != null && to.length() > 0) {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
}
// 设置抄送人
if (cc != null && cc.length() > 0) {
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
// 设置主题
mimeMsg.setSubject(subject);

//设置图片
MimeBodyPart text = new MimeBodyPart();
text.setContent("此邮件为系统自动发送<img src='cid:a'><img src='cid:b'>","text/html;charset=gb2312");
MimeBodyPart img = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("D://Chrysanthemum.jpg"));//图片路径
img.setDataHandler(dh);
img.setContentID("a");

MimeBodyPart img2 = new MimeBodyPart();
DataHandler dh2 = new DataHandler(new FileDataSource("d://Chrysanthemum.jpg"));//第二张图片路径
img2.setDataHandler(dh2);
img2.setContentID("b");

MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(img);
mm.setSubType("related");// 设置正文与图片之间的关系
// 图班与正文的 body
MimeBodyPart all = new MimeBodyPart();
all.setContent(mm);
MimeMultipart mm2 = new MimeMultipart();
mm2.addBodyPart(all);
mm2.addBodyPart(img2);
mm2.setSubType("mixed");



//设置附件:
                if (errorList != null && errorList.size() > 0) {
for (String file : errorList) {
contentPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(file);
contentPart.setDataHandler(new DataHandler(source));
String[] imgname = file.replace(File.separator, ",").split(",");
int num = imgname.length - 1;
contentPart.setFileName(imgname[num]);
multipart.addBodyPart(contentPart);
}
}
                mimeMsg.setContent(mm2);
mimeMsg.saveChanges();
// 发送邮件
if (props.get("mail.smtp.auth").equals("true")) {
Transport transport = session.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"), (String)props.get("username"), (String)props.get("password"));
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
transport.close();
} else {
Transport.send(mimeMsg);
}
System.out.println("邮件发送成功");



转载于:https://www.cnblogs.com/lch-1-lucky/p/7527180.html