1 package com.smtp; 2 3 import java.util.Vector; 4 5 public class MailBean { 6 private String to; // 收件人 7 private String from; // 发件人 8 private String host; // SMTP主机 9 private String username; // 发件人的用户名 10 private String password; // 发件人的密码 11 private String subject; // 邮件主题 12 private String content; // 邮件正文 13 Vector<String> file; // 多个附件 14 private String filename; // 附件的文件名 15 16 public String getTo() { 17 return to; 18 } 19 20 public void setTo(String to) { 21 this.to = to; 22 } 23 24 public String getFrom() { 25 return from; 26 } 27 28 public void setFrom(String from) { 29 this.from = from; 30 } 31 32 public String getHost() { 33 return host; 34 } 35 36 public void setHost(String host) { 37 this.host = host; 38 } 39 40 public String getUsername() { 41 return username; 42 } 43 44 public void setUsername(String username) { 45 this.username = username; 46 } 47 48 public String getPassword() { 49 return password; 50 } 51 52 public void setPassword(String password) { 53 this.password = password; 54 } 55 56 public String getSubject() { 57 return subject; 58 } 59 60 public void setSubject(String subject) { 61 this.subject = subject; 62 } 63 64 public String getContent() { 65 return content; 66 } 67 68 public void setContent(String content) { 69 this.content = content; 70 } 71 72 public String getFilename() { 73 return filename; 74 } 75 76 public void setFilename(String filename) { 77 this.filename = filename; 78 } 79 80 public Vector<String> getFile() { 81 return file; 82 } 83 84 public void attachFile(String fileName) { 85 if (file == null){ 86 file = new Vector<String>(); 87 } 88 file.addElement(fileName); 89 } 90 }
1 package com.smtp; 2 3 import java.util.Date; 4 import java.util.Enumeration; 5 import java.util.Properties; 6 import java.util.Vector; 7 8 import javax.activation.DataHandler; 9 import javax.activation.FileDataSource; 10 import javax.mail.Authenticator; 11 import javax.mail.Message; 12 import javax.mail.MessagingException; 13 import javax.mail.Multipart; 14 import javax.mail.PasswordAuthentication; 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 import javax.mail.internet.MimeUtility; 22 23 public class SendMail { 24 25 public String toChinese(String text) { 26 try { 27 text = MimeUtility.encodeText(new String(text.getBytes(), "GB2312"), "GB2312", "B"); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 return text; 32 } 33 34 public boolean sendMail(MailBean mb) { 35 String host = mb.getHost(); 36 final String username = mb.getUsername(); 37 final String password = mb.getPassword(); 38 String from = mb.getFrom(); 39 String to = mb.getTo(); 40 String subject = mb.getSubject(); 41 String content = mb.getContent(); 42 String fileName = mb.getFilename(); 43 Vector<String> file = mb.getFile(); 44 45 Properties props = System.getProperties(); 46 props.put("mail.smtp.host", host); // 设置SMTP的主机 47 props.put("mail.smtp.auth", "true"); // 需要经过验证 48 49 Session session = Session.getInstance(props, new Authenticator() { 50 public PasswordAuthentication getPasswordAuthentication() { 51 return new PasswordAuthentication(username, password); 52 } 53 }); 54 55 try { 56 MimeMessage msg = new MimeMessage(session); 57 msg.setFrom(new InternetAddress(from)); 58 InternetAddress[] address = { new InternetAddress(to) }; 59 msg.setRecipients(Message.RecipientType.TO, address); 60 msg.setSubject(toChinese(subject)); 61 62 Multipart mp = new MimeMultipart(); 63 MimeBodyPart mbpContent = new MimeBodyPart(); 64 mbpContent.setText(content); 65 mp.addBodyPart(mbpContent); 66 67 /* 往邮件中添加附件 */ 68 if (file != null) { 69 Enumeration<String> efile = file.elements(); 70 while (efile.hasMoreElements()) { 71 MimeBodyPart mbpFile = new MimeBodyPart(); 72 fileName = efile.nextElement().toString(); 73 FileDataSource fds = new FileDataSource(fileName); 74 mbpFile.setDataHandler(new DataHandler(fds)); 75 mbpFile.setFileName(toChinese(fds.getName())); 76 mp.addBodyPart(mbpFile); 77 } 78 System.out.println("添加成功"); 79 } 80 81 msg.setContent(mp); 82 msg.setSentDate(new Date()); 83 Transport.send(msg); 84 85 } catch (MessagingException me) { 86 me.printStackTrace(); 87 return false; 88 } 89 return true; 90 } 91 92 }
1 package com.smtp; 2 3 public class TestJavaMail { 4 5 public static void main(String[] args) { 6 7 MailBean mb = new MailBean(); 8 mb.setHost("smtp.126.com"); // 设置SMTP主机(163),若用126,则设为:smtp.126.com 9 mb.setUsername("邮箱地址"); // 设置发件人邮箱的用户名 10 mb.setPassword("邮箱密码"); // 设置发件人邮箱的密码,需将*号改成正确的密码 11 mb.setFrom("设置发件人的邮箱"); // 设置发件人的邮箱 12 mb.setTo("设置收件人的邮箱"); // 设置收件人的邮箱 13 mb.setSubject("测试_JavaMail"); // 设置邮件的主题 14 mb.setContent("本邮件中包含三个附件,请检查!"); // 设置邮件的正文 15 16 mb.attachFile("往邮件中添加附件"); // 往邮件中添加附件 17 mb.attachFile("往邮件中添加附件"); 18 mb.attachFile("往邮件中添加附件"); 19 20 SendMail sm = new SendMail(); 21 System.out.println("正在发送邮件..."); 22 // 发送邮件 23 if (sm.sendMail(mb)){ 24 System.out.println("发送成功!"); 25 }else{ 26 System.out.println("发送失败!"); 27 } 28 } 29 }