SendMail.java
上传用户:liangcc
上传日期:2019-05-24
资源大小:4412k
文件大小:3k
- package com.softeem.webmail.send;
- import java.util.Date;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- 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 com.softeem.struts.form.WriteForm;
- public class SendMail {
- public void sendmail(WriteForm wf, String name, String password) {
- Properties p = new Properties();
- p.setProperty("mail.smtp.auth", "true");
- p.setProperty("mail.transport.protocol", "smtp");
- p.setProperty("maildebug", "true");
- p.setProperty("mail.host", "smtp.sina.com");
- p.setProperty("mail.from", name);
- final String j = name;
- final String k = password;
- Session s = Session.getDefaultInstance(p, new Authenticator() {
- @Override
- protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
- // TODO Auto-generated method stub
- return new PasswordAuthentication(j, k);
- }
- });
- if (wf != null) {
- try {
- Message message = new MimeMessage(s);
- message.setFrom();
- message.setSubject(wf.getSubject());
- message.setText(wf.getRichedit());
- message.setSentDate(new Date());
- message.addRecipients(Message.RecipientType.TO,
- new InternetAddress[] { new InternetAddress(wf
- .getToUser()) });
- if (wf.getCopyTo() != null && wf.getCopyTo().length() > 0) {
- System.out.println(wf.getCopyTo().length());
- String str[] = wf.getCopyTo().split(",");
- for (String stri : str) {
- System.out.println(stri);
- message.addRecipients(Message.RecipientType.CC,
- new InternetAddress[] { new InternetAddress(
- stri) });
- }
- }
- if (wf.getSecretTo() != null && wf.getSecretTo().length() > 0) {
- String str[] = wf.getCopyTo().split(",");
- for (String stri : str) {
- message.addRecipients(Message.RecipientType.BCC,
- new InternetAddress[] { new InternetAddress(
- stri) });
- }
- }
- // 发附件
- if (wf.getFiles() != null) {
- String[] file = wf.getFiles().split("&&");
- if (file.length > 0) {// 有附件
- for (int i = 0; i < file.length; i++) {
- if (file[i] != null && file[i].length() > 0) {
- System.out.println(file[i]);
- MimeBodyPart mbp = new MimeBodyPart();
- Multipart mp = new MimeMultipart();
- FileDataSource fds = new FileDataSource(file[i]); // 得到数据源
- mbp.setDataHandler(new DataHandler(fds)); // 得到附件本身并至入BodyPart
- sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
- String fileName = "=?GBK?B?"
- + enc.encode(fds.getName().getBytes())
- + "?=";
- mbp.setFileName(fileName); // 得到文件名同样至入BodyPart
- mp.addBodyPart(mbp);
- }
- }
- file = null;
- }
- }
- Transport tran = s.getTransport();
- tran.connect("smtp.sina.com", 25, name, password);
- tran.send(message);
- tran.close();
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- }
- }
- }