JAVA发送邮件
说明:所用架包mail.jar (v1.4),架包请自己下载package com.huaxia.common.util;
import java.util.Properties;
import org.springframework.mail.MailMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class SendMailUtil {
public static boolean sendmail(String senderMail,String senderMailPass,String mailTitle,String sendContent,String receiverMail){
boolean isSuccess =true;
try {
//创建邮件发送类 JavaMailSender
//用于发送基本的文本邮件信息(不能包括附件,及图片)
JavaMailSender sender = new JavaMailSenderImpl();
//发邮件服务器 mail.hxcomm.cn 端口 25
//收邮件服务器 mail.hxcomm.cn ssl 端口 993
//设置邮件服务主机
//((JavaMailSenderImpl)sender).setHost("mail.hxcomm.cn");
((JavaMailSenderImpl)sender).setHost(PropertyUtil.getPropertiesValue("mailHost"));
//发送者邮箱的用户名
//((JavaMailSenderImpl)sender).setUsername("****@hxcomm.cn");
((JavaMailSenderImpl)sender).setUsername(senderMail);
//发送者邮箱的密码
//((JavaMailSenderImpl)sender).setPassword("********");
((JavaMailSenderImpl)sender).setPassword(senderMailPass);
//配置文件,用于实例化java.mail.session
Properties pro = System.getProperties();
//登录SMTP服务器,需要获得授权,网易163邮箱新近注册的邮箱均不能授权。
//测试 sohu 的邮箱可以获得授权
pro.put("mail.smtp.auth", "true");
pro.put("mail.smtp.socketFactory.port", PropertyUtil.getPropertiesValue("port"));
pro.put("mail.smtp.socketFactory.fallback", "false");
//通过文件获取信息
((JavaMailSenderImpl)sender).setJavaMailProperties(pro);
//创建基本邮件信息
MailMessage mailMessage = new SimpleMailMessage();
//发送者地址,必须填写正确的邮件格式,否者会发送失败
// mailMessage.setFrom("****@hxcomm.cn");
mailMessage.setFrom(senderMail);
//邮件主题
//mailMessage.setSubject("测试邮件");
mailMessage.setSubject(mailTitle);
//邮件内容,简单的邮件信息只能添加文本信息
mailMessage.setText(sendContent);
//邮件接收者的邮箱地址
// mailMessage.setTo("****@hxcomm.cn");
mailMessage.setTo(receiverMail);
//发送邮件,参数可以是数组
//sender.send(SimpleMailMessage[])
sender.send((SimpleMailMessage)mailMessage);
//String senderMail,String senderMailPass,String mailTitle,String sendContent,String receiverMail
System.out.println("邮件发送完毕!"+"发送者:"+senderMail+",接收者:"+receiverMail+",发送内容:"+sendContent);
} catch (Exception e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
public static void main(String[] args) {
//sendmail();
boolean isSuccess = sendmail("发送者的邮箱", "发送者的邮箱密码","主题", "内容", "接受者邮箱");
System.out.println("isSuccess==>"+isSuccess);
}
}
package com.huaxia.common.util;
import java.util.Properties;
import org.springframework.mail.MailMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class SendMailUtil {
public static boolean sendmail(String senderMail,String senderMailPass,String mailTitle,String sendContent,String receiverMail){
boolean isSuccess =true;
try {
//创建邮件发送类 JavaMailSender
//用于发送基本的文本邮件信息(不能包括附件,及图片)
JavaMailSender sender = new JavaMailSenderImpl();
//发邮件服务器 mail.hxcomm.cn 端口 25
//收邮件服务器 mail.hxcomm.cn ssl 端口 993
//设置邮件服务主机
//((JavaMailSenderImpl)sender).setHost("mail.hxcomm.cn");
((JavaMailSenderImpl)sender).setHost(PropertyUtil.getPropertiesValue("mailHost"));
//发送者邮箱的用户名
//((JavaMailSenderImpl)sender).setUsername("****@hxcomm.cn");
((JavaMailSenderImpl)sender).setUsername(senderMail);
//发送者邮箱的密码
//((JavaMailSenderImpl)sender).setPassword("********");
((JavaMailSenderImpl)sender).setPassword(senderMailPass);
//配置文件,用于实例化java.mail.session
Properties pro = System.getProperties();
//登录SMTP服务器,需要获得授权,网易163邮箱新近注册的邮箱均不能授权。
//测试 sohu 的邮箱可以获得授权
pro.put("mail.smtp.auth", "true");
pro.put("mail.smtp.socketFactory.port", PropertyUtil.getPropertiesValue("port"));
pro.put("mail.smtp.socketFactory.fallback", "false");
//通过文件获取信息
((JavaMailSenderImpl)sender).setJavaMailProperties(pro);
//创建基本邮件信息
MailMessage mailMessage = new SimpleMailMessage();
//发送者地址,必须填写正确的邮件格式,否者会发送失败
// mailMessage.setFrom("****@hxcomm.cn");
mailMessage.setFrom(senderMail);
//邮件主题
//mailMessage.setSubject("测试邮件");
mailMessage.setSubject(mailTitle);
//邮件内容,简单的邮件信息只能添加文本信息
mailMessage.setText(sendContent);
//邮件接收者的邮箱地址
// mailMessage.setTo("****@hxcomm.cn");
mailMessage.setTo(receiverMail);
//发送邮件,参数可以是数组
//sender.send(SimpleMailMessage[])
sender.send((SimpleMailMessage)mailMessage);
//String senderMail,String senderMailPass,String mailTitle,String sendContent,String receiverMail
System.out.println("邮件发送完毕!"+"发送者:"+senderMail+",接收者:"+receiverMail+",发送内容:"+sendContent);
} catch (Exception e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
public static void main(String[] args) {
//sendmail();
boolean isSuccess = sendmail("发送者的邮箱", "发送者的邮箱密码","主题", "内容", "接受者邮箱");
System.out.println("isSuccess==>"+isSuccess);
}
}