熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

使用JavaMail 實現郵件發送

2022-06-13   來源: Java核心技術 
 

  import javaio*;
import javautil*;
import javaxmail*;
import javaxmailinternet*;
import javaxactivation*;

  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 fromString toString usernameString password){
  thisfrom=from;
  thisto=to;
  thisusername=username;
  thispassword=password;
 }
 public String getFrom() {
  return from;
 }
 public void setFrom(String from) {
  thisfrom = from;
 }
 public String getTo() {
  return to;
 }
 public void setTo(String to) {
  thisto = to;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  thisusername = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  thispassword = password;
 }
 public String getSubject() {
  return subject;
 }
 public void setSubject(String subject) {
  thissubject = subject;
 }
 public String getText() {
  return text;
 }
 public void setText(String text) {
  thistext = text;
 }
 public File getFile() {
  return file;
 }
 public void setFile(File file) {
  thisfile = file;
 }
 
 public boolean send(){
  Properties props=new Properties();
  propsput(mailsmtphost );
  //propsput(mailsmtphost localhost);
  propsput(mailsmtpauth true);
  
  Session mailSession=SessiongetDefaultInstance(propsnull);
  
  try{
   Transport trans=mailSessiongetTransport(smtp);
   nnect(usernamepassword);
  // nnect();
   Message newMessage=new MimeMessage(mailSession);
   
   
   newMessagesetSubject(subject);
   newMessagesetFrom(new InternetAddress(from));
   
   /*
    * 上傳一個文件
    * */
   BodyPart fileBodyPart=new MimeBodyPart();
   FileDataSource fds=new FileDataSource(file);
   fileBodyPartsetDataHandler(new DataHandler(fds));
   fileBodyPartsetFileName(awav);
   
   Address addressTo[] = { new InternetAddress()};
   newMessagesetRecipients(MessageRecipientTypeTO addressTo);
   newMessagesetText(text);
   
   /*
    * 將文件保存到Message中
    * */
   MimeMultipart multi = new MimeMultipart();
   multiaddBodyPart(fileBodyPart);
   newMessagesetContent(multi);

  newMessagesaveChanges();
 
   transsendMessage(newMessage newMessagegetRecipients(MessageRecipientTypeTO)); //發送郵件  
   transclose();
   
   return true;
  }catch(Exception ex){
   return false;
  }
 }
 
 
 public static void main(String[] args){
  SendMail sendmail=new SendMail( zxctch );
  //SendMail sendmail=new SendMail();
  sendmailsetSubject(測試);
  sendmailsetText(測試代碼);
  File file=new File(E:\\workspace\\applet\\bin\\awav);
  sendmailsetFile(file);
  if(sendmailsend()){
   Systemoutprintln(send success);
  }
  else
   Systemoutprintln(send failed);
  
 }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26077.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.