第一步:
先导入两个包:mail.jar和activation.jar
第二部:
代码实现
1 package util.email; 2 3 import java.util.*; 4 import java.io.*; 5 import javax.mail.*; 6 import javax.mail.internet.*; 7 import javax.activation.*; 8 9 import util.FinalUtil; 10 11 public class SendMail { 12 //收件人邮箱地址 13 private String to; 14 //发件人邮箱地址 15 private static String from = ""; 16 //SMTP服务器地址 17 private static String smtpServer = ""; 18 //登录SMTP服务器的用户名 19 private static String username = ""; 20 //登录SMTP服务器的密码 21 private static String password =""; 22 //邮件主题 23 private String subject; 24 //邮件正文 25 private String content; 26 //记录所有附件文件的集合 27 List<String> attachments 28 = new ArrayList<String>(); 29 //无参数的构造器 30 public SendMail(){ 31 } 32 33 { 34 Properties p = new Properties(); 35 InputStream is; 36 try { 37 String path = SendMail.class.getResource("/email.Properties").toURI().getPath(); ; 38 System.out.println(path); 39 is = new FileInputStream(new File(path)); 40 p.load(is); 41 SendMail.username = p.getProperty("username").toString().trim(); //获取用户名 42 SendMail.password = p.getProperty("password").toString().trim(); //获取密码 43 SendMail.smtpServer = p.getProperty("smtpServer").toString().trim(); //获取SMTP 44 SendMail.from = p.getProperty("from").toString().trim(); //获取form来源 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 49 } 50 //把邮件主题转换为中文 51 public String transferChinese(String strText){ 52 try{ 53 strText = MimeUtility.encodeText( 54 new String(strText.getBytes() 55 , "GB2312"), "GB2312", "B"); 56 } 57 catch(Exception e){ 58 e.printStackTrace(); 59 } 60 return strText; 61 } 62 //增加附件,将附件文件名添加的List集合中 63 public void attachfile(String fname){ 64 attachments.add(fname); 65 } 66 67 //发送邮件 68 public boolean send(){ 69 System.out.println(SendMail.from+"--"+SendMail.password+"-"+SendMail.smtpServer+"--"+SendMail.username); 70 //创建邮件Session所需的Properties对象 71 Properties props = new Properties(); 72 props.put("mail.smtp.host" , smtpServer); 73 props.put("mail.smtp.auth" , "true"); 74 //创建Session对象 75 Session session = Session.getDefaultInstance(props 76 //以匿名内部类的形式创建登录服务器的认证对象 77 , new Authenticator() 78 { 79 public PasswordAuthentication getPasswordAuthentication() 80 { 81 return new PasswordAuthentication(username,password); 82 } 83 }); 84 try{ 85 //构造MimeMessage并设置相关属性值 86 MimeMessage msg = new MimeMessage(session); 87 //设置发件人 88 msg.setFrom(new InternetAddress(from)); 89 //设置收件人 90 InternetAddress[] addresses = {new InternetAddress(to)}; 91 msg.setRecipients(Message.RecipientType.TO , addresses); 92 //设置邮件主题 93 subject = transferChinese(subject); 94 msg.setSubject(subject); 95 //构造Multipart 96 Multipart mp = new MimeMultipart(); 97 //向Multipart添加正文 98 MimeBodyPart mbpContent = new MimeBodyPart(); 99 mbpContent.setText(content); 100 //将BodyPart添加到MultiPart中 101 mp.addBodyPart(mbpContent); 102 //向Multipart添加附件 103 //遍历附件列表,将所有文件添加到邮件消息里 104 for(String efile : attachments) 105 { 106 MimeBodyPart mbpFile = new MimeBodyPart(); 107 //以文件名创建FileDataSource对象 108 FileDataSource fds = new FileDataSource(efile); 109 //处理附件 110 mbpFile.setDataHandler(new DataHandler(fds)); 111 mbpFile.setFileName(fds.getName()); 112 //将BodyPart添加到MultiPart中 113 mp.addBodyPart(mbpFile); 114 } 115 //清空附件列表 116 attachments.clear(); 117 //向Multipart添加MimeMessage 118 msg.setContent(mp); 119 //设置发送日期 120 msg.setSentDate(new Date()); 121 //发送邮件 122 Transport.send(msg); 123 } 124 catch (MessagingException mex){ 125 mex.printStackTrace(); 126 return false; 127 } 128 return true; 129 } 130 131 public static void main(String[] args) { 132 133 //给总助发送邮箱 134 String content = FinalUtil.tg_content("[email protected]", "xxxxxx"); 135 SendMail sendMail = new SendMail(); 136 sendMail.setContent(content); 137 sendMail.setSubject(FinalUtil.EMAIL_SUJECT); 138 sendMail.setTo("[email protected]"); 139 140 if(sendMail.send()){ 141 System.out.println("成功发送邮件"); 142 } 143 } 144 public String getTo() { 145 return to; 146 } 147 public void setTo(String to) { 148 this.to = to; 149 } 150 public static String getFrom() { 151 return from; 152 } 153 public static String getSmtpServer() { 154 return smtpServer; 155 } 156 public static String getUsername() { 157 return username; 158 } 159 public static String getPassword() { 160 return password; 161 } 162 public String getSubject() { 163 return subject; 164 } 165 public void setSubject(String subject) { 166 this.subject = subject; 167 } 168 public String getContent() { 169 return content; 170 } 171 public void setContent(String content) { 172 this.content = content; 173 } 174 175 }
时间: 2024-11-06 12:41:56