阿里云服务器25端口禁用之如何使用Java发邮件(解决25端口禁用问题)
通常我们在本地使用Java发送邮件,通常是没有问题的,但是部署到服务器的话,就可能遇到问题。当然了,这与运营商也有关系。比如我之前在咖啡主机上购买虚拟机,然后将个人网站部署上去,通常是没有问题的,没有那么多限制。
但是在阿里云上限制就比较多,比如如果端口的入口方向不开的话,可能导致你在服务器上安装的某款软件,例如常用的Tomcat,会无法使用。
一来端口没有开放,二来白名单设置问题。
今天说说阿里云服务器25端口禁用之如何解决它并成功发送邮件,首先贴完整代码,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| package com.blog.springboot.utils;
import java.util.Properties;
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 javax.mail.internet.MimeMessage.RecipientType;
public class MailUtils {
public static void sendMail(String email, String emailMsg)
throws AddressException, MessagingException {
// 1.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");
props.setProperty("mail.host", "smtp.163.com");
props.setProperty("mail.smtp.auth", "true");// 指定验证为true
props.put("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", "465");
// 创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("test@163.com", "test");
}
};
Session session = Session.getInstance(props, auth);
// 2.创建一个Message,它相当于是邮件内容
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("test@163.com")); // 设置发送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
/* String random
String passwd = CustomeEncryptionUtil.bcryptPwd(pwd)*/
String content ="注册成功";
message.setSubject(content);
message.setContent(emailMsg, "text/html;charset=utf-8");
// 3.创建 Transport用于将邮件发送
Transport.send(message);
}
public static void main(String[] args) {
try {
MailUtils.sendMail("dev@163.com", "你好,欢迎来到我的博客,在这里你将会获得前所未有的幸福");
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("失败");
}
}
}
|
关键核心在于这:
1 2 3 4 5 6 7 8
| props.put("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", "465");
|
我使用mail包依赖如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <!-- 发邮件 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.2</version>
<scope>provided</scope>
</dependency>
|
另外说一下,如果你想确认你服务器上,端口是否可用,可用通过该命令(这样一来省的自己部署到服务器来测试,节约时间,提高效率):
1 2
| telnet smtp.163.com 465
|
如果服务器返回是这样的结果,如下(一般是表示可用的):
1 2 3 4 5 6 7 8
| Trying 220.181.12.11...
Connected to smtp.163.com.
Escape character is '^]'.
Connection closed by foreign host.
|
如果是这样会的结果,通常表示不可用(通常会一直卡在Trying,然后服务器可能会再次尝试Trying,不过还是没用):
1 2
| Trying 220.181.12.16...
|