使用java通过smtp完成发送邮件源码

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");
 
 
 }

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/manytao/archive/2010/10/03/5920080.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值