JavaMail 实现邮件发送

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

 

public class SendMail {
 private String from;
 private String to;
 private String username;
 private String password;
 private String subject;
 private String text;
 private File file;
 
 public SendMail(){
  
 }
 
 public SendMail(String from,String to,String username,String password){
  this.from=from;
  this.to=to;
  this.username=username;
  this.password=password;
 }
 public String getFrom() {
  return from;
 }
 public void setFrom(String from) {
  this.from = from;
 }
 public String getTo() {
  return to;
 }
 public void setTo(String to) {
  this.to = to;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 public String getSubject() {
  return subject;
 }
 public void setSubject(String subject) {
  this.subject = subject;
 }
 public String getText() {
  return text;
 }
 public void setText(String text) {
  this.text = text;
 }
 public File getFile() {
  return file;
 }
 public void setFile(File file) {
  this.file = file;
 }
 
 public boolean send(){
  Properties props=new Properties();
  props.put("mail.smtp.host", "smtp.163.com");
  //props.put("mail.smtp.host", "localhost");
  props.put("mail.smtp.auth", true);
  
  Session mailSession=Session.getDefaultInstance(props,null);
  
  try{
   Transport trans=mailSession.getTransport("smtp");
   trans.connect("smtp.163.com",username,password);
  // trans.connect();
   Message newMessage=new MimeMessage(mailSession);
   
   
   newMessage.setSubject(subject);
   newMessage.setFrom(new InternetAddress(from));
   
   /*
    * 上传一个文件
    * */
   BodyPart fileBodyPart=new MimeBodyPart();
   FileDataSource fds=new FileDataSource(file);
   fileBodyPart.setDataHandler(new DataHandler(fds));
   fileBodyPart.setFileName("a.wav");
   
   Address addressTo[] = { new InternetAddress("406560484@qq.com")};
   newMessage.setRecipients(Message.RecipientType.TO, addressTo);
   newMessage.setText(text);
   
   /*
    * 将文件保存到Message中
    * */
   MimeMultipart multi = new MimeMultipart();
   multi.addBodyPart(fileBodyPart);
   newMessage.setContent(multi);


   
   newMessage.saveChanges();
 
   trans.sendMessage(newMessage, newMessage.getRecipients(Message.RecipientType.TO)); //发送邮件  
   trans.close();
   
   return true;
  }catch(Exception ex){
   return false;
  }
 }
 
 
 public static void main(String[] args){
  SendMail sendmail=new SendMail("zxctch@163.com", "406560484@qq.com", "zxctch", "406560484");
  //SendMail sendmail=new SendMail();
  sendmail.setSubject("测试");
  sendmail.setText("测试代码");
  File file=new File("E://workspace//applet//bin//a.wav");
  sendmail.setFile(file);
  if(sendmail.send()){
   System.out.println("send success");
  }
  else
   System.out.println("send failed");
  
 }

}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值