首页 > 代码库 > JAVA中发送电子邮件的方法
JAVA中发送电子邮件的方法
JAVA中发送邮件的方法不复杂,使用sun的JavaMail的架包就可以实现。
一、下载JavaMail的架包,并导入项目中,如下:
二、附上代码例子,如下:
1、在main函数中对各项参数进行赋值(参数说明已进行备注),即可通过send函数进行发送邮件操作。
1 public class TestEmail { 2 3 private final static String TIMEOUT_MS = "20000"; 4 5 public static void main(String[] args) { 6 String host = "smtp.exmail.qq.com"; 7 String user = "xxxxxx@qq.com"; 8 String password = "xxxxxx"; 9 String recipients = "xxxxxx@qq.com";10 String cc = "";11 String subject = "邮件发送测试";12 String content = "邮件正文:<br>你好!";13 //方式1:通过URL获取附件14 // byte[] attachment = FileUtil.getFileByUrl("http://127.0.0.1/project/test.pdf");15 //方式2:通过本地路径获取附件16 byte[] attachment = FileUtil.getFileByPath("c://fujian.pdf");17 18 String attachmentName = "";19 try {20 attachmentName = MimeUtility.encodeWord("这是附件.pdf");21 send(host, user, password, recipients, cc, subject, content, attachment, attachmentName);22 } catch (Exception e) {23 e.printStackTrace();24 }25 }26 27 /**28 * @param host 邮件服务器主机名29 * @param user 用户名30 * @param password 密码31 * @param recipients 收件人32 * @param cc 抄送人33 * @param subject 主题34 * @param content 内容35 * @param attachment 附件 [没有传 null]36 * @param attachmentName 附件名称 [没有传 null]37 * @throws Exception38 */39 public static void send(final String host, final String user, final String password,40 final String recipients, final String cc, final String subject, final String content,41 final byte[] attachment,final String attachmentName) throws Exception {42 Properties props = new Properties();43 props.put("mail.smtp.host", host);44 props.put("mail.smtp.auth", "true");45 props.put("mail.smtp.timeout", TIMEOUT_MS);46 47 Authenticator auth = new Authenticator() {48 @Override49 protected PasswordAuthentication getPasswordAuthentication() {50 return new PasswordAuthentication(user, password);51 }52 };53 Session session = Session.getInstance(props, auth);54 MimeMessage msg = new MimeMessage(session);55 msg.setFrom(new InternetAddress(user));56 msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));57 if (cc != null && cc.length() > 0) {58 msg.setRecipients(Message.RecipientType.CC, cc);59 }60 msg.setSubject(subject);61 // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件62 Multipart multipart = new MimeMultipart();63 // 添加邮件正文64 BodyPart contentPart = new MimeBodyPart();65 contentPart.setContent(content, "text/html;charset=UTF-8");66 multipart.addBodyPart(contentPart);67 // 添加附件的内容68 if (attachment!=null) {69 BodyPart attachmentBodyPart = new MimeBodyPart();70 DataSource source = new ByteArrayDataSource(attachment,"application/octet-stream");71 attachmentBodyPart.setDataHandler(new DataHandler(source));72 //MimeUtility.encodeWord可以避免文件名乱码73 attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachmentName));74 multipart.addBodyPart(attachmentBodyPart);75 }76 // 将multipart对象放到message中77 msg.setContent(multipart);78 // 保存邮件79 msg.saveChanges();80 Transport.send(msg, msg.getAllRecipients());81 }82 }
2、上面的例子中,如果有附件,可对附件进行设置。附件的获取这里举2个例子,方式1通过网址获取,方式2通过本地获取。附上获取附件文件的方法,如下:
1 public class FileUtil { 2 3 public static byte[] getFileByUrl(String urlStr){ 4 try { 5 URL url = new URL(urlStr); 6 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 7 InputStream is = conn.getInputStream(); 8 BufferedInputStream bis = new BufferedInputStream(is); 9 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 10 final int BUFFER_SIZE = 2048;11 final int EOF = -1; 12 int c; 13 byte[] buf = new byte[BUFFER_SIZE]; 14 while (true) { 15 c = bis.read(buf); 16 if (c == EOF) 17 break; 18 baos.write(buf, 0, c); 19 } 20 conn.disconnect(); 21 is.close(); 22 23 byte[] data =http://www.mamicode.com/ baos.toByteArray(); 24 baos.flush();25 return data;26 27 } catch (Exception e) {28 e.printStackTrace();29 }30 return null;31 }32 33 public static byte[] getFileByPath(String pathStr){34 File file = new File(pathStr);35 try {36 FileInputStream fis = new FileInputStream(file);37 ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);38 byte[] b = new byte[1000];39 int n;40 while ((n = fis.read(b)) != -1) {41 bos.write(b, 0, n);42 }43 fis.close();44 byte[] data =http://www.mamicode.com/ bos.toByteArray();45 bos.close();46 return data;47 } catch (Exception e) {48 e.printStackTrace();49 }50 return null;51 }52 }
JAVA中发送电子邮件的方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。