javaMail

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

import javax.mail.internet.*;

public class SendMail {
 
 private String  user = "";
    private String password = " ";
    private String host = " "
   
    public void setHost() {
  this.host = "smtp.neusoft.com";
 }

 public void setAccount() {
  this.user = "defaultName";
  this.password = "defaultPassword";
 }
   
 public void setHost(String host) {
  this.host = host;
 }

 public void setAccount(String user, String password) {
  this.user = user;
  this.password = password;
 }
 public boolean send(String from, String to, String subject, String content) {
  Properties props = new Properties();
  props.put("mail.smtp.host", host);
  props.put("mail.smtp.auth", "true");
  try {
   Session mailSession = Session.getDefaultInstance(props);
   mailSession.setDebug(true);
   Message message = new MimeMessage(mailSession);
   message.setFrom(new InternetAddress(from));
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(
     to));
   message.setSubject(subject);
   message.setText(content);
   message.saveChanges();
   Transport transport = mailSession.getTransport("smtp");
   transport.connect(host, user, password);
   transport.sendMessage(message, message.getAllRecipients());
   transport.close();
   return true;
  } catch (Exception e) {
   System.out.println(e);
   return false;
   
  }
 }

}

 

你可能感兴趣的:(javaMail)