1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
| package com.umojo.tcm.utils;
import java.util.Date; import java.util.Properties;
import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import com.umojo.tcm.exception.BusinessException;
public class EmailUtils { private static Logger log = Logger.getLogger(EmailUtils.class); public final static String MY_EMAIL_COUNT = "xxx@sohu.com"; public final static String MY_EMAIL_PASSWORD = "xxx"; public final static String MY_EMAIL_SMTP_HOST = "smtp.sohu.com"; public final static Integer OUT_TIME = 1000; public final static String PROTOCOL_SMTP = "smtp"; public final static String PROTOCOL_SMTPS = "smtps"; public final static Integer SMTP_DEFAULT_PORT = 25; public final static Integer SMTPS_DEFAULT_PORT = 465;
public static void sendEmailSmtp(String subject,String text,String toEmail){ sendEmailSmtp(subject, text, new String[]{toEmail}, null, null, MY_EMAIL_COUNT, MY_EMAIL_PASSWORD, MY_EMAIL_SMTP_HOST); }
public static void sendEmailSmtps(String subject,String text,String toEmail){ sendEmailSmtps(subject, text, new String[] {toEmail}, null, null, MY_EMAIL_COUNT, MY_EMAIL_PASSWORD, MY_EMAIL_SMTP_HOST); }
public static void sendEmailSmtps(String subject,String text,String[] toEmail,String[] ccEmail,String[] bccEmail) { sendEmailSmtps(subject, text, toEmail, ccEmail, bccEmail, MY_EMAIL_COUNT, MY_EMAIL_PASSWORD, MY_EMAIL_SMTP_HOST); }
public static void sendEmailSmtp(String subject,String text,String[] toEmail,String[] ccEmail,String[] bccEmail) { sendEmailSmtp(subject, text, toEmail, ccEmail, bccEmail, MY_EMAIL_COUNT, MY_EMAIL_PASSWORD, MY_EMAIL_SMTP_HOST); }
public static void sendEmailSmtps(String subject,String text,String[] toEmail,String[] ccEmail,String[] bccEmail,String fromEmail,String password,String smtpHost) { sendEmail(subject, text, toEmail,ccEmail,bccEmail, fromEmail, password, PROTOCOL_SMTPS, smtpHost, SMTPS_DEFAULT_PORT, OUT_TIME, false); }
public static void sendEmailSmtp(String subject,String text,String[] toEmail,String[] ccEmail,String[] bccEmail,String fromEmail,String password,String smtpHost) { sendEmail(subject, text, toEmail,ccEmail,bccEmail,fromEmail, password, PROTOCOL_SMTP, smtpHost, SMTP_DEFAULT_PORT, OUT_TIME, false); }
public static void sendEmail(String subject,String text,String[] toEmail,String[] ccEmail,String[] bccEmail,String fromEmail,String password,String protocol,String smtpHost,Integer port,Integer outTime,Boolean debug) { try { log.info("主题:"+ subject); log.info("正文:"+text); log.info("toEmail:"+toEmail); log.info("fromEmail:"+fromEmail); Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", protocol); props.put("mail.smtp.connectiontimeout", outTime); props.put("mail.debug", debug); Session session = Session.getDefaultInstance(props); session.setDebug(debug); Message msg = createMimeMessage(session, subject, text, fromEmail, toEmail, ccEmail, bccEmail); Transport transport = session.getTransport(); transport.connect(smtpHost, port, fromEmail, password); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); } catch (Exception e) { log.info("发送邮件失败..."); e.printStackTrace(); } }
public static MimeMessage createMimeMessage(Session session, String subject, String text, String fromEmail, String[] toEmail, String[] ccEmail, String[] bccEmail) throws Exception { MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
if (toEmail != null && toEmail.length > 0) { for (int i = 0; i < toEmail.length; i++) { if(i == 0){ message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail[i])); }else{ message.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toEmail[i])); } } } else { throw new BusinessException("收件人为空"); }
if (ccEmail != null && ccEmail.length > 0) { for (int i = 0; i < ccEmail.length; i++) { if(i == 0){ message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(ccEmail[i])); }else{ message.addRecipient(MimeMessage.RecipientType.CC, new InternetAddress(ccEmail[i])); } } }
if (bccEmail != null && bccEmail.length > 0) { for (int i = 0; i < bccEmail.length; i++) { if(i == 0){ message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(bccEmail[i])); }else{ message.addRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(bccEmail[i])); } } }
message.setSubject(subject, "UTF-8");
message.setContent(text, "text/html;charset=UTF-8");
message.setSentDate(new Date());
return message; } public static void main(String[] args) throws MessagingException { String subject = "测试多邮件发送,抄送,密送"; String text = "这里是正文"; String singleToEmail = "renjie@bluedeer.com.cn"; String[] toEmail = {"renjie@bluedeer.com.cn","zhouhuadong@bluedeer.com.cn"}; String[] ccEmail = {"REN_ZHEN_YU@163.com"}; String[] bccEmail = {"chenzhongwen@bluedeer.com.cn"}; String fromEmail = "xxx@sohu.com"; String password = "xxx"; String protocol = "smtp"; String smtpHost = "smtp.sohu.com"; Integer port = 25; Boolean debug = true; sendEmail(subject, text, toEmail, ccEmail, bccEmail, fromEmail, password, protocol, smtpHost, port, OUT_TIME, debug); sendEmailSmtps(subject, text, singleToEmail); sendEmailSmtp(subject, text, singleToEmail); sendEmailSmtp(subject, text, toEmail,ccEmail, bccEmail, fromEmail, password, smtpHost); sendEmailSmtps(subject, text, toEmail,ccEmail, bccEmail, fromEmail, password, smtpHost); } }
|