使用邮件服务器发送Email,代码如下: package org.codepackage.functions; //需要的jar包:mail.jar 、activation.jar import java.util.Properties; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; /* * 模块名称:信息通报模块 * 功能:发送邮件 * 输入参数说明: * String to:邮件接收者email地址 * String subject:邮件主题 * String txt:邮件内容 */ public class MailProgram { public static void sendMsg(String subject,String txt,String reciver) { String host="localhost";//smtp服务器 String user="admin";//用户名 String pwd="";//加密后的密码 String from="aaa@aaa.com";//发件人地址 String recivers=reciver;//收件人地址 Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props); session.setDebug(true); MimeMessage message = new MimeMessage(session); try { // 加载发件人地址 message.setFrom(new InternetAddress(from,user,"UTF-8")); // 加载收件人地址 String [] to=recivers.split(";"); for(int i=0;i<to.length;i++) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); } // 加载标题 message.setSubject(subject); // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件 Multipart multipart = new MimeMultipart(); // 设置邮件的文本内容 BodyPart contentPart = new MimeBodyPart(); contentPart.setText(txt); multipart.addBodyPart(contentPart); // 将multipart对象放到message中 message.setContent(multipart); // 保存邮件 message.saveChanges(); // 发送邮件 Transport transport = session.getTransport("smtp"); // 连接服务器的邮箱 transport.connect(host, user, pwd); // 把邮件发送出去 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } public static void main(String [] args){ new MailProgram().sendMsg("mail_test","测试邮件是否发送成功!","aaa@aaa.com"); } }