前言
今天开发伙伴找我修改邮件服务smtp发现网站用邮箱找回密码失败了!于是我开始了简单得操作!
首先我扫描一下!
发现25端口没有连接服务。
看一下报错!
之后telnet一下 telnet smtp.163.com 25发现找不到!
之后上网找一些资料发现阿里的售后很早之前发过这样的帖子 说25端口禁用了,之后又有查询资料发现465端口ssl可以!
那么问题找到了 那就需要将25端口换到465端口 采用SSL协议传输邮件。
好了贴代码时间到:
下面这段代码是采用SSL协议发送邮件,可向多人单人发送邮
public static boolean sslSend(MessageInfo msg1, EmailAccount emailAccount)
throws AddressException, MessagingException, IOException{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = new Properties();
props.setProperty("mail.smtp.host", emailAccount.getPlace());
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
final String username = emailAccount.getUsername();
final String password = emailAccount.getPassword();
Session session = Session.getDefaultInstance(props, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}});
Message msg = new MimeMessage(session);
// 设置发件人和收件人
msg.setFrom(new InternetAddress(emailAccount.getUsername()));
List<String> tos = msg1.getTo();
Address to[] = new InternetAddress[tos.size()];
for(int i=0;i<tos.size();i++){
to[i] = new InternetAddress(tos.get(i));
}
// 多个收件人地址
msg.setRecipients(Message.RecipientType.TO, to);
msg.setSubject(msg1.getSubject()); // 标题
msg.setText(msg1.getMsg());// 内容
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("EmailUtil ssl协议邮件发送打印" +msg.toString());
return true;
}
EmailAccout 字段
// 邮箱用户
private String username;
// 邮箱密码
private String password;
// 邮箱服务器
private String place;
MessageInfo 字段
@NameCN("发件人地址")
private String from;
@NameCN("收件人地址")
private List<String> to;
@NameCN("发送时间")
private Date sendDate;
@NameCN("邮件主题")
private String subject;
@NameCN("消息正文")
private String msg;
将需要的信息,写入这两个Bean,传入最上面的方法即可 使用。
下面是25端口 非SSl协议传输代码块
public void doSend(MessageInfo msg, EmailAccount emailAccount) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(msg.getFrom());// 设置发送者地址
List<String> tos = msg.getTo();
String to[] = new String[tos.size()];
tos.toArray(to);
simpleMailMessage.setTo(to);// 设置接受者地址,可多个
simpleMailMessage.setSubject(msg.getSubject());
simpleMailMessage.setSentDate(msg.getSendDate());
simpleMailMessage.setText(msg.getMsg());
simpleMailMessage.setFrom(emailAccount.getUsername());
System.out.println("EmailUtil 邮件发送打印 1111" + simpleMailMessage.toString());
JavaMailSenderImpl s = new JavaMailSenderImpl();
s.setHost(emailAccount.getPlace());
// s.setPort(25);
s.setUsername(emailAccount.getUsername());
s.setPassword(emailAccount.getPassword());
s.send(simpleMailMessage);
}
这是参考的引入
import java.io.IOException;
import java.security.Security;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import 项目包名.MessageInfo;
import 项目包名.EmailAccount;
javamail是spring里的javamail jar包。
OK当我们使用465端口SSL协议的时候,顺利的在服务器上发出邮件了。
注意!需要在阿里云的安全组把465端口写上!
自后在扫描一下看看是否支持465端口!已经open了!
看一下邮箱是否收到验证码!
输入邮箱找回密码!
总结!勤动脑多喝六个核桃!