终于知道了什么叫难了不会,会了不难。经过不断地查阅,不断的测试,终于成功了,所以就写了一个简单的例子给大家分享。这个代码部分有借鉴别人,然后我做了一些修改,更简单,更实用。不要忘记设置发送者账号和密码,并且在src目录下放个notice.jpg文件,发送邮件后这张图片不仅在邮箱正文,而且还会成为一个附件。
package com.util.mail; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; 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 SendQQMail { private Session session ; private MimeMessage message ; private Properties properties = new Properties(); private MimeBodyPart bodyText ;//邮件正文的对象 private String formAddress = "发送者@qq.com"; private String password = "发送者密码"; private String host = "smtp.qq.com"; private String title = "标题:这是用javamail发送的邮件"; private String text = "正文:这是系统发来的邮件。请不要回复。"; private String encoding = "text/html;charset=gb2312"; public SendQQMail(){ properties.put("mail.smtp.host", host);// 设置smtp主机 properties.put("mail.smtp.auth", "true");// 使用smtp身份验证 session = Session.getInstance(properties,new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(formAddress,password); } }); bodyText = new MimeBodyPart(); } public static void main(String[] args){ SendQQMail sqm = new SendQQMail(); sqm.sendMailWithImgAsAttachment("260383473@qq.com","src//notice.jpg"); } public void sendMailWithImgAsAttachment(String toAddress,String filePath){ message = new MimeMessage(session); try { message.setFrom(new InternetAddress(formAddress));//设置邮件的发件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));//设置邮件的收件人 message.setSubject(title);//设置邮件的主题
// setContent(“邮件的正文内容”,”设置邮件内容的编码方式”) bodyText.setContent(text+"</br><img src='cid:image'>",encoding); //创建一个BodyPart对象,用于存放附件 MimeBodyPart attch = new MimeBodyPart(); DataHandler dataHandler = new DataHandler(new FileDataSource(filePath)); attch.setDataHandler(dataHandler); String filename = dataHandler.getName(); // MimeUtility 是一个工具类,encodeText()用于处理附件名称编码 attch.setFileName(MimeUtility.encodeText(filename));//指定该BodyPart对象代表的内容为附件 //创建一个BodyPart对象,用于存放正文图片 MimeBodyPart attch2 = new MimeBodyPart(); DataHandler dataHandler2 = new DataHandler(new FileDataSource(filePath)); attch2.setDataHandler(dataHandler2); attch2.setContentID("image"); //将各个BodyPart添加到MimeMultipart MimeMultipart mimeMultipart = new MimeMultipart(); mimeMultipart.addBodyPart(bodyText); mimeMultipart.addBodyPart(attch); mimeMultipart.addBodyPart(attch2); mimeMultipart.setSubType("related");//设置正文与附件之间的关系,不过没发现和mixed有什么区别 //mimeMultipart.setSubType("mixed"); message.setContent(mimeMultipart);//设置信息的内容为MimeMultipart message.saveChanges(); //保存修改 Transport.send(message);//发送邮件 System.out.println("发送成功"); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }