123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package com.yihu.utils;
- /**
- *
- */
- import java.io.File;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.BodyPart;
- import javax.mail.Message;
- 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;
- /**
- * @author 林小辉
- * @company
- * 2017-05-26
- */
- public class MailUtil {
- public static class MyAuthenticator extends Authenticator{
- String name;
- String password;
- public MyAuthenticator(String name,String password){
- this.name = name;
- this.password = password;
- this.getPasswordAuthentication();
- }
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
-
- return new PasswordAuthentication(name,password);
- }
- }
-
- /**
- *
- * @param subject 主题
- * @param context 邮件内容
- * @param emails 收件人(多人)
- * @param attachment 附件 (null标识不发送邮件)
- * @throws Exception
- */
- public static void send(String subject,String context,String[] emails) throws Exception{
- //发送邮箱的用户名和密码
- String name = "mailmaster@jkzl.com";
- String password = "qazwsx321$1";
- Properties props = System.getProperties();
- //设置smtp主机
- props.put("mail.smtp.host", "smtp.263.net");
- //设置smtp验证属性
- props.put("mail.smtp.auth","true");
-
- MyAuthenticator auth = new MyAuthenticator(name,password);
- //获得邮件会话对象
- Session session = Session.getDefaultInstance(props, auth);
- //创建MIME邮件对象
- MimeMessage message = new MimeMessage(session);
- subject = MimeUtility.encodeWord(subject, "UTF-8", "Q");
- message.setSubject(subject);
- //设置邮件发送人
- message.setFrom(new InternetAddress("mailmaster@jkzl.com"));
- //设置邮件收件人
- InternetAddress[] as = new InternetAddress[emails.length];
- for (int i = 0;i<emails.length;i++) {
- as[i]= new InternetAddress();
- as[i].setAddress(emails[i]);
- }
- message.setRecipients(Message.RecipientType.TO ,as);
- //设置邮件主题
- message.setSubject(subject);
- //设置上传的正文
- BodyPart bpContent = new MimeBodyPart();
- bpContent.setContent(context, "text/html;charset=UTF-8");
- MimeMultipart mp = new MimeMultipart();
- mp.addBodyPart(bpContent);
-
-
-
- message.setContent(mp);
- message.saveChanges();
- //创建SMTP邮件协议发送对象
- Transport transport = session.getTransport("smtp");
- //取得与邮件服务器的连接
- transport.connect((String)props.get("mail.smtp.host"), name, password);
- //通过邮件服务器发送邮件
- transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
-
- transport.close();
-
- }
-
-
- public static void main(String[] args) {
- String[] arry ={"906335852@qq.com"};
- try {
- MailUtil.send("BOOS故障单", "Test", arry);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
|