首页 > 代码库 > java mail
java mail
1 package com.lyz.mail; 2 3 import java.io.IOException; 4 import java.util.Date; 5 import java.util.Enumeration; 6 import java.util.Properties; 7 import java.util.Vector; 8 9 import javax.activation.DataHandler; 10 import javax.activation.FileDataSource; 11 import javax.mail.AuthenticationFailedException; 12 import javax.mail.Message; 13 import javax.mail.MessagingException; 14 import javax.mail.Multipart; 15 import javax.mail.Session; 16 import javax.mail.Transport; 17 import javax.mail.internet.InternetAddress; 18 import javax.mail.internet.MimeBodyPart; 19 import javax.mail.internet.MimeMessage; 20 import javax.mail.internet.MimeMultipart; 21 22 import org.apache.log4j.Logger; 23 24 25 /** 26 * <p>用于发送邮件的类</p> 27 * <p>调用Mail.mail(String subject, String content,String mailTo,String ccTo)发送邮件</p> 28 * <p>MailAuthenticator为鉴权配置</p> 29 * @author zhangshuang 30 * @date 2014/2/18 17:02 31 * 32 */ 33 public class Mail { 34 35 private static Logger logger = Logger.getLogger(Mail.class); 36 protected String mailTo = null; 37 protected String mailFrom = null; 38 protected String smtpHost = null; 39 protected boolean debug = false; 40 41 protected String messageBasePath = null; 42 protected String subject; 43 protected String msgContent; 44 45 protected Vector<?> attachedFileList; 46 protected String mailAccount = null; 47 protected String mailPass = null; 48 protected String messageContentMimeType = "text/html; charset=gb2312"; 49 50 protected String mailbccTo = null; 51 protected String mailccTo = null; 52 53 /** 54 * SendMailService 构造子注解。 55 */ 56 public Mail() { 57 58 } 59 60 private void fillMail(Session session, MimeMessage msg) throws IOException, 61 MessagingException { 62 63 String fileName = null; 64 Multipart mPart = new MimeMultipart(); 65 if (mailFrom != null) { 66 msg.setFrom(new InternetAddress(mailFrom)); 67 //System.out.println("发送人Mail地址:" + mailFrom); 68 } else { 69 //System.out.println("没有指定发送人邮件地址!"); 70 return; 71 } 72 if (mailTo != null) { 73 InternetAddress[] address = InternetAddress.parse(mailTo); 74 msg.setRecipients(Message.RecipientType.TO, address); 75 //System.out.println("收件人Mail地址:" + mailTo); 76 } else { 77 //System.out.println("没有指定收件人邮件地址!"); 78 return; 79 } 80 81 if (mailccTo != null) { 82 InternetAddress[] ccaddress = InternetAddress.parse(mailccTo); 83 //System.out.println("CCMail地址:" + mailccTo); 84 msg.setRecipients(Message.RecipientType.CC, ccaddress); 85 } 86 if (mailbccTo != null) { 87 InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo); 88 //System.out.println("BCCMail地址:" + mailbccTo); 89 msg.setRecipients(Message.RecipientType.BCC, bccaddress); 90 } 91 msg.setSubject(subject); 92 InternetAddress[] replyAddress = { new InternetAddress(mailFrom) }; 93 msg.setReplyTo(replyAddress); 94 // create and fill the first message part 95 MimeBodyPart mBodyContent = new MimeBodyPart(); 96 if (msgContent != null) 97 mBodyContent.setContent(msgContent, messageContentMimeType); 98 else 99 mBodyContent.setContent("", messageContentMimeType); 100 mPart.addBodyPart(mBodyContent); 101 // attach the file to the message 102 if (attachedFileList != null) { 103 for (Enumeration<?> fileList = attachedFileList.elements(); fileList 104 .hasMoreElements();) { 105 fileName = (String) fileList.nextElement(); 106 MimeBodyPart mBodyPart = new MimeBodyPart(); 107 108 // attach the file to the message 109 FileDataSource fds = new FileDataSource(messageBasePath 110 + fileName); 111 //System.out.println("Mail发送的附件为:" + messageBasePath + fileName); 112 mBodyPart.setDataHandler(new DataHandler(fds)); 113 mBodyPart.setFileName(fileName); 114 mPart.addBodyPart(mBodyPart); 115 } 116 } 117 msg.setContent(mPart); 118 msg.setSentDate(new Date()); 119 } 120 121 /** 122 * 此处插入方法说明。 123 */ 124 public void init() { 125 126 } 127 128 /** 129 * 发送e_mail,返回类型为int 当返回值为0时,说明邮件发送成功 当返回值为3时,说明邮件发送失败 130 */ 131 public int sendMail() throws IOException, MessagingException { 132 // int loopCount = 0; 133 Properties props = System.getProperties(); 134 props.put("mail.smtp.host", smtpHost); 135 props.put("mail.smtp.auth", "true"); 136 137 MailAuthenticator auth = new MailAuthenticator(); 138 139 Session session = Session.getInstance(props, auth); 140 session.setDebug(debug); 141 MimeMessage msg = new MimeMessage(session); 142 Transport trans = null; 143 try { 144 145 fillMail(session, msg); 146 // send the message 147 trans = session.getTransport("smtp"); 148 try { 149 trans.connect(smtpHost, MailAuthenticator.MAIL_USER, 150 MailAuthenticator.MAIL_PASSWORD);// , 151 // HUAWEI_MAIL_PASSWORD); 152 } catch (AuthenticationFailedException e) { 153 logger.error(e); 154 //System.out.println("连接邮件服务器错误:"); 155 return 3; 156 } catch (MessagingException e) { 157 //System.out.println("连接邮件服务器错误:"); 158 logger.error(e); 159 return 3; 160 } 161 162 Transport.send(msg); 163 trans.close(); 164 165 } catch (MessagingException mex) { 166 //System.out.println("发送邮件失败:"); 167 mex.printStackTrace(); 168 Exception ex = null; 169 if ((ex = mex.getNextException()) != null) { 170 //System.out.println(ex.toString()); 171 logger.error(ex); 172 } 173 return 3; 174 } finally { 175 try { 176 if (trans != null && trans.isConnected()) 177 trans.close(); 178 } catch (Exception e) { 179 logger.error(e); 180 } 181 } 182 //System.out.println("发送邮件成功!"); 183 return 0; 184 } 185 186 187 188 public static void mail(String subject, String content,String mailTo,String ccTo) { 189 Mail sm = new Mail(); 190 191 sm.setSMTPHost(MailAuthenticator.SMTP_HOST); 192 sm.setMailFrom(MailAuthenticator.MAIL_FROM); 193 sm.setMailTo(mailTo); 194 sm.setMailccTo(ccTo); 195 sm.setSubject(subject); 196 sm.setMsgContent(content); 197 198 try { 199 sm.sendMail(); 200 } catch (Exception e) { 201 logger.error(e); 202 } 203 sm = null; 204 } 205 206 public void setAttachedFileList(Vector<?> filelist) { 207 this.attachedFileList = filelist; 208 } 209 210 public void setDebug(boolean debugFlag) { 211 this.debug = debugFlag; 212 } 213 214 public void setMailAccount(String strAccount) { 215 this.mailAccount = strAccount; 216 } 217 218 public void setMailbccTo(String bccto) { 219 this.mailbccTo = bccto; 220 } 221 222 public void setMailccTo(String ccto) { 223 this.mailccTo = ccto; 224 } 225 226 public void setMailFrom(String from) { 227 this.mailFrom = from; 228 } 229 230 public void setMailPass(String strMailPass) { 231 this.mailPass = strMailPass; 232 } 233 234 public void setMailTo(String to) { 235 this.mailTo = to; 236 } 237 238 public void setMessageBasePath(String basePath) { 239 this.messageBasePath = basePath; 240 } 241 242 public void setMessageContentMimeType(String mimeType) { 243 this.messageContentMimeType = mimeType; 244 } 245 246 public void setMsgContent(String content) { 247 this.msgContent = content; 248 } 249 250 public void setSMTPHost(String host) { 251 this.smtpHost = host; 252 } 253 254 public void setSubject(String sub) { 255 this.subject = sub; 256 } 257 258 public static void main(String args[]){ 259 String mailto = "zhangshuang@qiyi.com,zhsh411@126.com"; 260 String ccto="zhsh411@qq.com"; 261 Mail.mail("hello", "hello", mailto, ccto); 262 } 263 }
auth
1 package com.lyz.mail; 2 3 import javax.mail.Authenticator; 4 import javax.mail.PasswordAuthentication; 5 6 /** 7 * <p>邮箱配置</p> 8 * <p>需继承自javax.mail.Authenticator</p> 9 * 10 * @author Dell 11 * @date 2014/2/18 17:07 12 */ 13 public class MailAuthenticator extends Authenticator { 14 // ****************************** 15 // 由于发送邮件的地方比较多, 16 // 下面统一定义用户名,口令. 17 // ****************************** 18 public static String MAIL_USER = "MBD-Data"; 19 public static String MAIL_PASSWORD = "ANALYSIS&all456"; 20 public static String MAIL_FROM = "MBD-Data@xxx.com"; 21 public static String SMTP_HOST = "smtp.mail.xxx.com"; 22 public MailAuthenticator(){ 23 24 } 25 26 protected PasswordAuthentication getPasswordAuthentication() { 27 return new PasswordAuthentication(MAIL_USER, MAIL_PASSWORD); 28 } 29 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。