在实现邮件发送的时候首先需要用到mail.jar开发包,有关mail.jar的下载可以去百度自行下载
下面是邮件发送核心代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.yysj.lhb.action; 2 3 import javax.activation.DataHandler; 4 import javax.activation.FileDataSource; 5 import javax.mail.Address; 6 import javax.mail.BodyPart; 7 import javax.mail.Message; 8 import javax.mail.Multipart; 9 import javax.mail.internet.InternetAddress; 10 import javax.mail.internet.MimeBodyPart; 11 import javax.mail.internet.MimeMessage; 12 import javax.mail.internet.MimeMultipart; 13 import javax.mail.internet.MimeUtility; 14 import javax.mail.Session; 15 import javax.mail.Transport; 16 17 import java.util.List; 18 import java.util.Properties; 19 20 /** 21 * 发送邮箱信息 22 * @author lhb 23 * 24 */ 25 public class EmailSender { 26 27 private static final long serialVersionUID = 1L; 28 private MimeMessage mimeMessage;//MiME邮件对象 29 private Session session;//邮件会话对象; 30 private Properties properties;//系统属性 31 private boolean needAuth = false;//smtp是否需要认证 32 private String username="";//stmp认证用户和密码 33 private String password=""; 34 private Multipart multipart;//Multipart对象,邮件内容,标题附件等内容添加到其中后再生成 35 private String log; 36 37 public EmailSender() { 38 39 } 40 41 public EmailSender(String smtp) { 42 setSmtpHost(smtp); 43 createMimeMessage(); 44 } 45 46 /** 47 * 设置系统属性 48 * @param hostName 主机名 49 */ 50 public void setSmtpHost(String hostName) { 51 System.out.println("系统属性:mail.smtp.host="+hostName); 52 if(properties == null) { 53 properties = System.getProperties();//获取系统属性对象 54 } 55 properties.put("mail.smtp.host", hostName); 56 properties.put("mail smtp.localhost", "localHostAddress"); 57 } 58 59 /** 60 * 创建Mime信息 61 * @return 成功返回true;否则返回false 62 */ 63 public boolean createMimeMessage() { 64 try { 65 System.out.println("准备获取邮件会话对象"); 66 session = Session.getDefaultInstance(properties, null);//获取右键会话对象 67 } catch (Exception e) { 68 log = "获取邮件会话对象时发生错误!"+e.toString(); 69 System.err.println(log); 70 return false; 71 } 72 try { 73 mimeMessage = new MimeMessage(session);//创建MIME邮件对象 74 multipart = new MimeMultipart();// 75 //Multipart is a container that holds multiple body parts 76 return true; 77 } catch (Exception e) { 78 log = "创建MIME邮件对象失败!"+e.toString(); 79 System.err.println(log); 80 return false; 81 } 82 } 83 84 /** 85 * 设置身份认证 86 * @param need 87 */ 88 public void setNeedAuth(boolean need) { 89 needAuth = need; 90 System.out.println("设置smtp身份认证:mail.smtp.auth="+need); 91 if(properties == null) { 92 properties = System.getProperties(); 93 } 94 if(needAuth) { 95 properties.put("mail.smtp.auth", "true"); 96 }else { 97 properties.put("mail.smtp.auth", "false"); 98 } 99 } 100 /** 101 * 设置用户名和密码 102 * @param name 103 * @param pass 104 */ 105 public void setNamePass(String name,String pass) { 106 System.out.println("得到用户名和密码"); 107 username = name; 108 password = pass; 109 } 110 111 /** 112 * 设置邮件主题 113 * @param mailSubject 114 * @return 115 */ 116 public boolean setSubject(String mailSubject) { 117 System.out.println("设置邮件主题"); 118 try { 119 mimeMessage.setSubject(MimeUtility.encodeText(mailSubject, "utf-8", "B")); 120 return true; 121 } catch (Exception e) { 122 log = "设置邮件主题发生错误!"+ e; 123 System.err.println(log); 124 return false; 125 } 126 } 127 128 /** 129 * 设置邮件正文 130 * @param mailBody 正文内容 131 * @return 设置成功返回true;否则返回false 132 */ 133 public boolean setBody(String mailBody) { 134 try { 135 System.out.println("设置邮件体格式"); 136 BodyPart bPart = new MimeBodyPart(); 137 //转换中文格式 138 bPart.setContent("<meta http-equiv=Content-Type content=text/html;charset=utf-8>"+mailBody, "text/html;charset=utf-8"); 139 multipart.addBodyPart(bPart); 140 return true; 141 } catch (Exception e) { 142 log = "设置邮件正文发生错误!"+e; 143 System.err.println(log); 144 return false; 145 } 146 } 147 /** 148 * 设置附件 149 * @param files 150 * @return 151 */ 152 public boolean setFiles(List<String> files) { 153 try { 154 for(String s : files) { 155 MimeBodyPart mimeBodyPart = new MimeBodyPart(); 156 FileDataSource fileDataSource = new FileDataSource(s);//得到数据源 157 mimeBodyPart.setDataHandler(new DataHandler(fileDataSource));//得到附件本身并植入BodyPart 158 mimeBodyPart.setFileName(fileDataSource.getName());//得到文件名同样植入BodyPart 159 multipart.addBodyPart(mimeBodyPart); 160 } 161 return true; 162 } catch (Exception e) { 163 log = "增加附件时出错!"+e; 164 System.err.println(log); 165 return false; 166 } 167 } 168 169 /** 170 * 按路径添加附件 171 * @param path 172 * @param name 173 * @return 174 */ 175 public boolean addFile(String path,String name) { 176 177 try { 178 MimeBodyPart mimeBodyPart = new MimeBodyPart(); 179 FileDataSource fileDataSource = new FileDataSource(path);//得到数据源 180 mimeBodyPart.setDataHandler(new DataHandler(fileDataSource));//得到附件本身并注入BodyPart 181 mimeBodyPart.setFileName(MimeUtility.encodeText(name,"utf-8","B")); 182 multipart.addBodyPart(mimeBodyPart); 183 return true; 184 } catch (Exception e) { 185 log = "增加附件出错"+e; 186 System.err.println(log); 187 return false; 188 } 189 190 } 191 192 /** 193 * 设置发信人 194 * @param from 发信人名称 195 * @return 196 */ 197 public boolean setFrom(String from) { 198 System.out.println("设置发信人"); 199 try { 200 mimeMessage.setFrom(new InternetAddress(from));//设置发信人 201 return true; 202 } catch (Exception e) { 203 log = "设置发言人出错"+e; 204 return false; 205 } 206 } 207 208 /** 209 * 设置收件人 210 * @param to 211 * @return 212 */ 213 public boolean setTo(String to) { 214 System.out.println("设置收件人"); 215 if(to == null) { 216 return false; 217 } 218 try { 219 mimeMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 220 return true; 221 } catch (Exception e) { 222 log = "设置收件人错误"+e; 223 System.err.println(log); 224 return false; 225 } 226 } 227 228 /** 229 * 230 * @param copyto 231 * @return 232 */ 233 public boolean setCopyTo(String copyto) { 234 if(copyto == null) { 235 return false; 236 } 237 try { 238 mimeMessage.setRecipients(Message.RecipientType.CC, (Address[])InternetAddress.parse(copyto)); 239 return true; 240 } catch (Exception e) { 241 e.printStackTrace(); 242 return false; 243 } 244 } 245 246 /** 247 * 发送邮件 248 * @return 249 */ 250 public boolean sendout() { 251 try { 252 mimeMessage.setContent(multipart); 253 mimeMessage.saveChanges(); 254 System.out.println("正在发送邮件..."); 255 Session mailSession = Session.getInstance(properties,null); 256 Transport transport = mailSession.getTransport("smtp"); 257 transport.connect((String)properties.get("mail.smtp.host"), username, password); 258 transport.sendMessage(mimeMessage, mimeMessage.getRecipients(Message.RecipientType.TO)); 259 System.out.println("发送邮件成功!"); 260 transport.close(); 261 return true; 262 } catch (Exception e) { 263 log = "邮件发送失败!"+e; 264 System.err.println(log); 265 return false; 266 } 267 } 268 269 public String getLog() { 270 return log; 271 } 272 273 274 }
action中的代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
jsp代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <div class="question_form_1"> 2 <form action="questionForm" method="get" name="form1"> 3 <div class="question_form_1_" style="color: #6e6e6e;"> 4 <h3>有什么需要解惑? </h3> 5 <textarea class="question_form_youquestion" rows="10" cols="100" id="question_Text" name="question_Text" placeholder="请描述您的问题,比如:心脏前负荷和后负荷有什么区别?动脉血气如何分析? 什么是膜性肾病?"></textarea> 6 7 <div class="question_form_1_" style="color: #6e6e6e;"> 8 <h3>回复的邮箱或者手机号码</h3> 9 <input class="question_form_contactyou" id="contact_Customer" name="contact_Customer" type="text" placeholder="请输入您的联系方式" /> 10 </div> 11 <div class="btn_div"> 12 <input type="submit" id="submit" value="发 送" class="btn" /> 13 </div> 14 </form> 15 </div>
页面