邮件发送

来源:互联网 发布:监控网络拓扑图 编辑:程序博客网 时间:2024/05/22 18:09
/**
 * 邮件发送的身份验证器
 * @author  zm
 * @date 
 * @version 
 * @parameter
 * @since
 * @return
 */
public class MyAuthenticator extends Authenticator{

private String userName=null;     
    private String password=null;     
          
    public MyAuthenticator(){     
    }     
    public MyAuthenticator(String username, String password) {      
        this.userName = username;      
        this.password = password;      
    }      
    protected PasswordAuthentication getPasswordAuthentication(){     
        return new PasswordAuthentication(userName, password);     
    }     


}

/**
 * 一个简单的邮件发送,不加附件。
 * 
 * @author zm
 * @date 2017-07-20
 * @version
 * @parameter
 * @since
 * @return
 */
public class MailSenderUtil {


//发送邮件服务器的IP和端口
private static String mailServerHost = "smtp.163.com";
private static String mailServerPort = "25";


//身份是否验证
private static boolean validate = true;   
//添加附件,附件名
private String[] attachFileName;

public boolean isValidate() {
return validate;
}

public String[] getAttachFileName() {
return attachFileName;
}
public void setAttachFileName(String[] attachFileName) {
this.attachFileName = attachFileName;
}
/**
* 获取邮件会话属性
*/
public static Properties getProperties() {
Properties p = new Properties();      
   p.put("mail.smtp.host", mailServerHost);      
   p.put("mail.smtp.port", mailServerPort);      
   p.put("mail.smtp.auth", validate ? "true" : "false");      
return p;
}


/**
* 以文本格式发送邮件

* @param mailInfo
* 待发送的邮件信息
* @return
*/
public static boolean sendTextMail(String fromAddr, String password, String toAddr,
String subject, String content) {

// 是否要身份认证
MyAuthenticator authenticator = null;
Properties pro = getProperties();
if (validate) {
// 创建一个密码验证器
authenticator = new MyAuthenticator(fromAddr, password);
}
// 构建一个邮件发送的Session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
// 根据Session创建一个发送邮件的消息
Message mailMessage = new MimeMessage(sendMailSession);
try {
// 创建邮件发送者的地址
Address from = new InternetAddress(fromAddr);
try {
mailMessage.setFrom(from);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 接收者地址
Address to = new InternetAddress(toAddr);
try {
mailMessage.setRecipient(Message.RecipientType.TO, to);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 设置邮件消息的主题
try {
mailMessage.setSubject(subject);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mailMessage.setSentDate(new Date(0));
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mailMessage.setText(content);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 发送邮件
try {
Transport.send(mailMessage);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}


}

public class Mail {


public static void main(String[] args) {

String fromAddr = "------------@163.com";     //发送人
String password = "--------------";//密码验证
String toAddr = "----------------";//收件人地址
String subject = "-------------------";//主题
String content = "-------------------";//内容
MailSenderUtil.sendTextMail(fromAddr, password, toAddr, subject, content);
}

}