package com.nsoft.beans;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class EMailBean { public static int CONTENTTYPE_TEXT=1;
public static int CONTENTTYPE_HTML=2;
private String SMTPHost="";
private String userName="";
private String userPass="";
private String mailSender="";
private String subject="";
private String content="";
private int contenType=1;
private String[] receptionAddresses=null;
/*设置,得到SMTP主机地址*/
public void setSMTPHost(String SMTPHost)
{
this.SMTPHost=SMTPHost;
}
public String getSMTPHost()
{
return this.SMTPHost;
}
/*设置,得到SMTP主机用户名称和密码*/
public void setUserName(String userName)
{
this.userName=userName;
}
public String getUserName()
{
return this.userName;
}
public void setUserPass(String userPass)
{
this.userPass=userPass;
}
public String getUserPass()
{
return this.userPass;
}
/*设置,得到邮件发送地址*/
public void setMailSender(String mailSender)
{
this.mailSender=mailSender;
}
public String getMailSender()
{
return this.mailSender;
} /*设置,得到邮件主题*/
public void setSubject(String subject)
{
this.subject=subject;
}
public String getSubject()
{
return this.subject;
}
/*设置,得到邮件内容*/
public void setContent(String content)
{
this.content=content;
}
public String getContent()
{
return this.content;
}
/*设置,得到邮件的格式*/
public void setContentType(int contentType)
{
this.contenType=contentType;
}
public String getContentType()
{
if(this.contenType==1)
{
return "text/plain;charset=GBK";
}
else
{
return "text/html;charset=GBK";
}
}
/*设置,得到邮件接受地址*/
public void setReceptionAddresses(String[] receptionAddresses)
{
this.receptionAddresses=receptionAddresses;
}
public String[] getReceptionAddresses()
{
return this.receptionAddresses;
}
/*接受者地址解析*/
private InternetAddress[] parse()
{
InternetAddress[] internetAddress=new InternetAddress[this.receptionAddresses.length];
for(int i=0;i<receptionAddresses.length;i++)
{
try{
internetAddress[i]=new InternetAddress(receptionAddresses[i]);
}
catch(AddressException ae)
{
System.out.println(receptionAddresses[i]+" is not valid");
}
}
return internetAddress;
}
/*文本内容的类型转换*/
private void contentTypeChange()
{
StringBuffer sb=new StringBuffer();
if(this.contenType==2)
{
sb.append("<HTML>\n");
sb.append("<HEAD>\n");
sb.append("</HEAD>\n");
sb.append("<BODY>\n");
sb.append("<DIV align='center'>\n");
sb.append(this.content);
sb.append("</DIV>\n");
sb.append("</BODY>\n");
sb.append("</HTML>");
this.setContent(sb.toString());
}
}
/*邮件发送*/
private void smtp() throws MessagingException,AddressException
{
if(this.SMTPHost=="") throw new MessagingException("SMTPHost not found");
if(this.userName=="") throw new MessagingException("userName is empty");
if(this.userPass=="") throw new MessagingException("userPass is empty");
Properties props=System.getProperties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host",this.SMTPHost);
props.put("mail.smtp.auth", "true");//使用smtp身份验证
Session session=Session.getDefaultInstance(props,null);
MimeMessage mimeMsg = new MimeMessage(session);//创建MIME邮件对象
if(this.mailSender=="")
{
throw new MessagingException("mailSender is empty");
}
else
{
mimeMsg.setFrom(new InternetAddress(this.mailSender));
}
if(this.receptionAddresses==null)
{
throw new MessagingException("receptionAddresses is null");
}
else
{
mimeMsg.setRecipients(Message.RecipientType.BCC,this.parse());
}
if(this.subject=="")
{
throw new MessagingException("subject is empty");
}
else
{
mimeMsg.setSubject(this.subject,"GBK");
}
this.contentTypeChange();
mimeMsg.setContent(this.content,this.getContentType());
Transport transport = session.getTransport("smtp");
transport.connect(this.SMTPHost,this.userName,this.userPass);
transport.sendMessage(mimeMsg,this.parse());
transport.close();
}
public boolean sendMail()//供外部程序调用
{
try{
this.smtp();
return true;
}
catch(Exception e)
{
System.out.println(e.toString());
return false;
}
}
}
EMailBean mb=new EMailBean();
String[] ReceptionAddr={"[email protected]","[email protected]"};
mb.setSMTPHost("smtp.163.com");
mb.setUserName("xxx");
mb.setUserPass("xxx");
mb.setSubject("我是xx");
mb.setContent("hello world");
mb.setMailSender("[email protected]");
mb.setReceptionAddresses(ReceptionAddr);
mb.sendMail();
就行了啊,我已经发送成功了