javamail发送邮件解决方案

前一段想学一下javamail,结果在网上找到很多例子,都是写的不够清楚,而且发送带附件的和不带附件这两个例子在一块的也基本没有,试了好长时间没有试出来,后来经过我同事指点一下,终于成功了,呵呵,现在贴出来供大家一起学习。由于不能传附件,所以就没有把javamail.jar传上来,大家可以自己去SUN网站下载。
大家可以转载,但希望转载时注上作者名称:于红磊,如有问题,请发邮件: txyhl@126.com
 

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("kftxyhl@163.com","txyhl@126.com","于红磊测试!",strMMHF,"c://test.txt");
  sm.sendMailWithNone("kftxyhl@163.com","txyhl@126.com","于红磊测试!",strMMHF);

 }
}
该代码经测试成功,和大家一起学习。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值