首页>代码>java发送邮件代码,即实现纯代码客户端发送邮件>/MailSend/src/com/test/mail/SimpleMailSender.java
package com.test.mail;

import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * 建立简单邮件发送器
 */
public class SimpleMailSender {
	

		 public boolean sendTextMail(MailSenderInfo mailInfo) {
		   // 判断是否需要身份认证
		   MyAuthenticator authenticator = null;
		   Properties pro = mailInfo.getProperties();

		   if (mailInfo.isValidate()) {
		   // 如果需要身份认证,则创建一个密码验证器
		  authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		   }
		   // 根据邮件会话属性和密码验证器构造一个发送邮件的session
		   Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
		   
		   sendMailSession.setDebug(true);//监控邮件发送全过程,相当于打开了调试日志
		   
		   
		   
		   try {
		   // 根据session创建一个邮件消息
		   Message mailMessage = new MimeMessage(sendMailSession);
		   // 创建邮件发送者地址
		   Address from = new InternetAddress(mailInfo.getFromAddress());
		   // 设置邮件消息的发送者
		   mailMessage.setFrom(from);
		   // 创建邮件的接收者地址,并设置到邮件消息中
		   
		// 创建邮件的接收者地址,并设置到邮件消息中,一般用数组,设置多人收件
		   Address[] tos=null;
		   String[] receives=mailInfo.getToAddress();
		   tos=new Address[receives.length];
		   for(int i=0;i<receives.length;i++)
		   {
			   System.out.println("当前的receives是:"+i+":"+receives[i]);
			   tos[i]=new InternetAddress(receives[i]);
		   }
		   
//		   Address to = new InternetAddress(mailInfo.getToAddress());
//		   mailMessage.setRecipients(Message.RecipientType.TO,tos);//收件人
		   mailMessage.setRecipients(Message.RecipientType.BCC,tos);//密送
		   // 设置邮件消息的主题
		   mailMessage.setSubject(mailInfo.getSubject());
		   // 设置邮件消息发送的时间
		   mailMessage.setSentDate(new Date());
		   // 设置邮件消息的主要内容
		   String mailContent = mailInfo.getContent();
		   mailMessage.setText(mailContent);
		   System.out.println("发送前打印下mailMessage:"+mailMessage.toString());
		   // 发送邮件
		   Transport.send(mailMessage);
		   System.out.println("发送完,打印");
		   return true;
		   } catch (MessagingException ex) {
		    ex.printStackTrace();
		   }
		   return false;
		 }
		 
		 
		 public static boolean sendHtmlMail(MailSenderInfo mailInfo){
		   // 判断是否需要身份认证
		   MyAuthenticator authenticator = null;
		   Properties pro = mailInfo.getProperties();
		   //如果需要身份认证,则创建一个密码验证器 
		   if (mailInfo.isValidate()) {
		  authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		   }
		   // 根据邮件会话属性和密码验证器构造一个发送邮件的session
		   Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
		   try {
		   // 根据session创建一个邮件消息
		   Message mailMessage = new MimeMessage(sendMailSession);
		   // 创建邮件发送者地址
		   Address from = new InternetAddress(mailInfo.getFromAddress());
		   // 设置邮件消息的发送者
		   mailMessage.setFrom(from);
		   // 创建邮件的接收者地址,并设置到邮件消息中,一般用数组,设置多人收件
		   Address[] tos=null;
		   String[] receives=mailInfo.getToAddress();
		   tos=new Address[receives.length];
		   for(int i=0;i<receives.length;i++)
		   {
			   tos[i]=new InternetAddress(receives[i]);
		   }
		   //下面这句是发给一个人的
//		   Address to = new InternetAddress(mailInfo.getToAddress());
		   // Message.RecipientType.TO属性表示接收者的类型为TO  
		  
		   mailMessage.setRecipients(Message.RecipientType.TO,tos);
		   // 设置邮件消息的主题
		   mailMessage.setSubject(mailInfo.getSubject()+"我是HTML测试");
		   // 设置邮件消息发送的时间
		   mailMessage.setSentDate(new Date());
		   // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
		   Multipart mainPart = new MimeMultipart();
		   // 创建一个包含HTML内容的MimeBodyPart
		   BodyPart html = new MimeBodyPart();
		   // 设置HTML内容
		   html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
		   mainPart.addBodyPart(html);
		   
		   
		   Vector file=mailInfo.getFile();
		   String filename;
		   //发送附件
		   if(!file.isEmpty()){//有附件  
               Enumeration efile=file.elements();  
               while(efile.hasMoreElements()){   
            	   html=new MimeBodyPart();  
                   filename=efile.nextElement().toString(); //选择出每一个附件名  
                   FileDataSource fds=new FileDataSource(filename); //得到数据源  
                   html.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart  
                   html.setFileName(fds.getName());  //得到文件名同样至入BodyPart  
                   mainPart.addBodyPart(html);  
               }    
               file.removeAllElements();      
           }   
		   // 将MiniMultipart对象设置为邮件内容
		   mailMessage.setContent(mainPart);
		   // 发送邮件
		   Transport.send(mailMessage);
		   return true;
		   } catch (MessagingException ex) {
		    ex.printStackTrace();
		   }
		   return false;
		 }
		}


最近下载更多
hbsoft2008  LV16 2023年10月19日
annazhang  LV29 2023年4月8日
chanxyer  LV6 2021年7月13日
luesjim  LV11 2021年6月18日
zhaojialiang  LV7 2021年5月14日
whdbhm  LV3 2021年2月23日
pxqtsht  LV15 2021年1月28日
xuexizhuanyong23  LV16 2021年1月23日
咕噜红  LV4 2020年9月4日
yxq330521  LV11 2020年7月30日
最近浏览更多
lzx602  LV3 4月15日
gao123456789  LV6 3月7日
hbsoft2008  LV16 2023年10月19日
ZhangGb 2023年9月2日
暂无贡献等级
luoxici 2023年5月16日
暂无贡献等级
annazhang  LV29 2023年4月7日
Tuxxxxx  LV3 2023年1月3日
1358849392  LV21 2022年12月27日
微信网友_6040315240812544  LV8 2022年11月21日
kahvia  LV1 2022年5月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友