package util;
import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.Properties;
import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.MessagingException; 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; import javax.mail.internet.MimeUtility;
public class MailSend { public void sendMail(String subject, String from, String[] to, String text, String[] filenames, String mimeType) throws MessagingException, UnsupportedEncodingException { Properties p = new Properties();
String smtp = "smtp.163.com"; String servername = "xxxx@163.com"; String serverpwd = "xxxxxxxxxxx"; Session session = null; MimeMessage mimeMessage = null;
p = System.getProperties(); p.put("mail.smtp.host", smtp); // 设置SMTP主机 p.put("mail.smtp.auth", "true"); // 是否到服务器用户名和密码验证
// 到服务器验证发送的用户名和密码是否正确 SmtpAuthenticator sa = new SmtpAuthenticator(servername, serverpwd); // 设置邮件会话 注意这里将认证信息放进了Session的创建参数里 session = javax.mail.Session.getInstance(p, (Authenticator) sa); // 设置邮件会话 // 注意这里将认证信息放进了Session的创建参数里 Transport transport = session.getTransport("smtp"); // 设置传输协议 // 设置from、to等信息 mimeMessage = new javax.mail.internet.MimeMessage(session); if (null != from && !"".equals(from)) { InternetAddress sentFrom = new InternetAddress(from); mimeMessage.setFrom(sentFrom); // 设置发送人地址 }
InternetAddress[] sendTo = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) { System.out.println("发送到:" + to[i]); sendTo[i] = new InternetAddress(to[i]); } mimeMessage.setRecipients( javax.mail.internet.MimeMessage.RecipientType.TO, sendTo); mimeMessage.setSubject(subject, "gb2312");
MimeBodyPart messageBodyPart1 = new MimeBodyPart(); messageBodyPart1.setContent(text, mimeType);
Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart1);
// for (int i = 0; i < filenames.length; i++) { // MimeBodyPart messageBodyPart2 = new MimeBodyPart(); // String filename = filenames[i].split(";")[0]; // String displayname = filenames[i].split(";")[1]; // // 得到数据源 // FileDataSource fds = new FileDataSource(filename); // // BodyPart添加附件本身 // messageBodyPart2.setDataHandler(new DataHandler(fds)); // // BodyPart添加附件文件名 // messageBodyPart2.setFileName(MimeUtility.encodeText(displayname)); // multipart.addBodyPart(messageBodyPart2); // } mimeMessage.setContent(multipart); // 设置信件头的发送日期 mimeMessage.setSentDate(new Date()); mimeMessage.saveChanges(); // 发送邮件 transport.send(mimeMessage); transport.close(); System.out.println("发送到成功!!!"); }
public static void main(String[] args) throws Exception { String title = "title";// 所发送邮件的标题 String from = "xxxx@163.com";// 从那里发送 String sendTo[] = { "xxxx@qq.com" };// 发送到那里 // 邮件的文本内容,可以包含html标记则显示为html页面 String content = "test java send mail !!!!!!<br><a href=\"http://www.jz123.cn/">J站长家园欢迎你!</a>"; // 所包含的附件,及附件的重新命名 String fileNames[] = { "d:\\test\\test.png;test.png" }; //String fileNames[] = { ";" }; MailSend test = new MailSend(); try { // MailSender mailsender = new MailSender(); test.sendMail(title, from, sendTo, content, fileNames,"text/html;charset=gb2312"); } catch (Exception ex) { ex.printStackTrace(); } } }
|