| 本帖最后由 海岸的声音 于 2010-10-11 15:44 编辑 
 
 源文件下载:    提醒:使用smtp实现Java发送邮件源码,整个一个项目工程,导入工程就能使用
 
 
 复制代码
package com.in.smtp;
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
public class EmailForSmtp {
String host = "";
String username = "";
String password = "";
public void setHost(String host) {
  this.host = host;
}
public void setUserInformaition(String username,String password) {
  this.username = username;
  this.password = password;
}
public void sendEmail(String sendpeople,String arrviedpeople,String emailname,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 msg = new MimeMessage(mailSession);
   msg.setFrom(new InternetAddress(sendpeople));
   msg.addRecipient(Message.RecipientType.TO,new InternetAddress(arrviedpeople));//收件人
   msg.setSubject(emailname);//邮件主题
   msg.setText(content);//邮件内容
   msg.saveChanges();
   Transport transport = mailSession.getTransport("smtp");
   transport.connect(host, username, password);
   transport.sendMessage(msg, msg.getAllRecipients());
   transport.close();
  } catch (AddressException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchProviderException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (MessagingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
}
}
------------------------------------------------------------------------------------------------------------------
Client
import com.in.smtp.EmailForSmtp;
public class Client {
/**
  * @param args
  */
public static void main(String[] args) {
  // TODO Auto-generated method stub
  EmailForSmtp efs = new EmailForSmtp();
  
  efs.setHost("smtp.163.com");
  efs.setUserInformaition("manytao", "wangtao521");
  efs.sendEmail("
}
ssss@163.com", "bbb_bbb@163.com", "aaaa", "bbbbb");
  
  
}
本文来自计算机协会,转载请标明出处:http://www.czzyca.com/
 |