javamail发送邮件解决方案

package maildemo;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;

public class SendMail {

 String host = "";

 String user = "";

 String password = "";

 public void setHost(String host) {
  this.host = host;
 }

 public void setAccount(String user, String password) {
  this.user = user;
  this.password = password;
 }
 //不带附件的邮件
 public void sendMailWithNone(String from, String to, String subject,
   String content) {
  Properties props = new Properties();
  props.put("mail.smtp.host", host);// 指定SMTP服务器
  props.put("mail.smtp.auth", "true");// 指定是否需要SMTP验证
  try {
   Session mailSession = Session.getDefaultInstance(props);

   mailSession.setDebug(true);// 是否在控制台显示debug信息

   Message message = new MimeMessage(mailSession);
   message.setFrom(new InternetAddress(from));// 发件人
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(
     to));// 收件人

   message.setSubject(subject);// 邮件主题
   message.setContent(content,"text/html; charset=GB2312");// 邮件内容
   message.saveChanges();

   Transport transport = mailSession.getTransport("smtp");
   transport.connect(host, user, password);
   transport.sendMessage(message, message.getAllRecipients());
   transport.close();
  } catch (Exception e) {
   System.out.println(e);
  }
 }
 //发送带附件的邮件
 public void sendMailWithFJ(String from, String to, String subject,
   String content, String filepath) {
  Properties props = new Properties();
  props.put("mail.smtp.host", host);// 指定SMTP服务器
  props.put("mail.smtp.auth", "true");// 指定是否需要SMTP验证
  try {
   Session mailSession = Session.getDefaultInstance(props);

   mailSession.setDebug(true);// 是否在控制台显示debug信息

   MimeMessage msg = new MimeMessage(mailSession);
   msg.setSentDate(new Date());
   msg.setSubject(subject);

   MimeBodyPart textBodyPart = new MimeBodyPart();
   textBodyPart.setText(content);
   // 设置字符集
   //msg.setContent(content, "text/html; charset=GB2312");
   msg.setFrom(new InternetAddress(from));
   msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

   MimeBodyPart fileBodyPart = new MimeBodyPart();
   FileDataSource fds = new FileDataSource(filepath);// 要发送的附件
   fileBodyPart.setDataHandler(new DataHandler(fds));
   fileBodyPart.setFileName(MimeUtility.encodeWord(fds.getName(),"GB2312",null));
   Multipart container = new MimeMultipart();
   container.addBodyPart(textBodyPart);
   container.addBodyPart(fileBodyPart);
   msg.setContent(container);
   Transport transport = mailSession.getTransport("smtp");
   transport.connect(host, user, password);
   transport.sendMessage(msg, msg.getAllRecipients());
   transport.close();
  } catch (Exception e) {
   System.out.println(e);
  }
 }

 public static void main(String args[]) {
  SendMail sm=new SendMail();
  //邮件内容
  String strMMHF="yhltest";
  //邮件服务器
  sm.setHost("smtp.163.com");
  sm.setAccount("kftxyhl","******");//*号处为密码
  //sm.sendMailWithFJ("[email protected]","[email protected]","aaaaaaaaa!",strMMHF,"c:\\test.txt");
  sm.sendMailWithNone("[email protected]","[email protected]","aaaaaaaaa!",strMMHF);

 }
}

你可能感兴趣的:(html)