jmail认证非认证两种方式使用

无认证方式发送
///限于无须认证的邮件服务器,
  String host = "smtp.sss.com"; //邮件服务器
  String from = " xxxxx@163.com";              //发送人
  String to = " xxx@163.com";               //收信人
//     得到系统属性
  Properties props = System.getProperties();
  
//     设置发送服务器
  props.put("mail.smtp.host", host);

// 建立session   
  Session session = Session.getDefaultInstance(props, null);
// 定义message
  MimeMessage message = new MimeMessage(session);
 
  //设置收信人,多个收信人也可以用数组表示
  message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  message.setSubject("subject");//设置显示的主题
  message.setFrom(new InternetAddress(from));//设置显示的发送人
  message.setText("text field");//设置邮件内容
  message.setSentDate(new Date());//设置收送日期
//  发送
  Transport.send(message);
 
 
认证方式发信
 
认证类
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
 
public class MyPasswordAuthentication extends Authenticator{
 
 public MyPasswordAuthentication() {
  
  // TODO Auto-generated constructor stub
 }
 

 String username = null;
 String password = null;
 
 PasswordAuthentication pa = null;
 
 public static MyPasswordAuthentication instance = null;
 
 public static synchronized MyPasswordAuthentication getInstance(String user,String pass)
 {
  if(instance == null)
  {
   instance = new MyPasswordAuthentication(user,pass);
  }
  return instance;
 }
 
 
 public MyPasswordAuthentication(String user,String pass)
 {
  username = user;
  password = pass;
 }
  
 public PasswordAuthentication getPasswordAuthentication()
 {
  
  if(pa == null)
  {
   pa = new PasswordAuthentication(username, password);
  }
  
  return pa;
 }
}
 
 
 
//发送类
import java.util.Date;
import java.util.Properties;
 
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
import com.sun.mail.smtp.SMTPTransport;
 
 
 
public class SendMail{
 
 public SendMail()
 {
   try {
    send();
   } catch (MessagingException e) {
    e.printStackTrace();
   }
 }
 
 public void send() throws MessagingException
 {
  String host = "mail5.ujs.edu.cn"; //邮件服务器
  String name = "xxx";          //用户名
  String password ="xxxxx";       //密码
  String from = " xxx@163.com";                           //发送人
  String to = " xxxxx@163.com";               //收信人
//     Get system properties
  Properties props = System.getProperties();
  
//     Setup mail server
  props.put("mail.smtp.host", host);
 
 
  props.put("mail.smtp.auth","true"); 
  
  Authenticator auth =  new MyPasswordAuthentication().getInstance(name,password);
  Session session = Session.getDefaultInstance(props, auth);// 创建session
   
  MimeMessage message = new MimeMessage(session); //创建message对象
 
  Address address = new InternetAddress(from); //发件人地址
  Address toAddress = new InternetAddress(to);  
  Address[] toAddList = new Address[5];
  
  toAddList[0] = new InternetAddress(" xxx1@163.com");
  toAddList[1] = new InternetAddress(" ddsds@163.com");
  toAddList[2] = new InternetAddress(" sadsf@163.com");
  toAddList[3] = new InternetAddress(" asdf@163.com");
  toAddList[4] = new InternetAddress(" asdf@163.com");
  
  message.setFrom(address); //设置发件人
  message.setRecipients(MimeMessage.RecipientType.TO, toAddList);
  message.setSubject("hahahahahaha", "GB2312"); //设置主题
  message.setSentDate(new Date()); //设置日期
  message.setText("hahahahaha", "GB2312"); //设置邮件内容
  message.setFrom(address);
 
   //发送
//     Transport transport = session.getTransport("smtp");
//  Transport.send(message,toAddList);  
  SMTPTransport t = (SMTPTransport)session.getTransport("smtp");
  t.connect(host,name,password);
  t.sendMessage(message,message.getAllRecipients());
  
  
  
  
  
 }
 
 
 public static void main(String[] args)
 {
  
   
  new SendMail();
  
 }
 
 
}
 
 
 
 
 
 
 
 
 
 

 
 
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值